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 | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 520 | closeSessions(activeDesc, true /*activeOnly*/); |
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; |
| 791 | 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 | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 794 | if (attr != NULL) { |
| 795 | if (!isValidAttributes(attr)) { |
| 796 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 797 | attr->usage, attr->content_type, attr->flags, |
| 798 | attr->tags); |
| 799 | return BAD_VALUE; |
| 800 | } |
| 801 | attributes = *attr; |
| 802 | } else { |
| 803 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 804 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 805 | return BAD_VALUE; |
| 806 | } |
| 807 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 808 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 809 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 810 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 811 | " session %d selectedDeviceId %d", |
| 812 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 813 | session, *selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 814 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 815 | // TODO: check for existing client for this port ID |
| 816 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 817 | *portId = AudioPort::getNextUniqueId(); |
| 818 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 819 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 820 | // First check for explicit routing (eg. setPreferredDevice) |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 821 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 822 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 823 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(*selectedDeviceId); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 824 | } else { |
| 825 | // If no explict route, is there a matching dynamic policy that applies? |
| 826 | sp<SwAudioOutputDescriptor> desc; |
| 827 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
| 828 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 829 | if (!audio_has_proportional_frames(config->format)) { |
| 830 | return BAD_VALUE; |
| 831 | } |
| 832 | *stream = streamTypefromAttributesInt(&attributes); |
| 833 | *output = desc->mIoHandle; |
| 834 | ALOGV("getOutputForAttr() returns output %d", *output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 835 | goto exit; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // Virtual sources must always be dynamicaly or explicitly routed |
| 839 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 840 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 841 | return BAD_VALUE; |
| 842 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 843 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 844 | |
| 845 | // Virtual sources must always be dynamicaly or explicitly routed |
| 846 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 847 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 848 | return BAD_VALUE; |
| 849 | } |
| 850 | |
| 851 | *stream = streamTypefromAttributesInt(&attributes); |
| 852 | |
| 853 | // TODO: Should this happen only if an explicit route is active? |
| 854 | // the previous code structure meant that this would always happen which |
| 855 | // would appear to result in adding a null deviceDesc when not using an |
| 856 | // explicit route. Is that the intended and necessary behavior? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 857 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 858 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 859 | strategy = (routing_strategy) getStrategyForAttr(&attributes); |
| 860 | device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 861 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 862 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 863 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 866 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 867 | // chosen by the engine if not. |
| 868 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 869 | // to getOutputForDevice. |
| 870 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
| 871 | *stream == AUDIO_STREAM_MUSIC && |
| 872 | audio_is_linear_pcm(config->format) && |
| 873 | isInCall()) { |
| 874 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 875 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 876 | } else { |
| 877 | device = mEngine->getDeviceForStrategy(strategy); |
| 878 | } |
| 879 | } |
| 880 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 881 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 882 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 883 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 884 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 885 | *output = AUDIO_IO_HANDLE_NONE; |
| 886 | if (msdDevice != AUDIO_DEVICE_NONE) { |
| 887 | *output = getOutputForDevice(msdDevice, session, *stream, config, flags); |
| 888 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(device) == NO_ERROR) { |
| 889 | ALOGV("%s() Using MSD device 0x%x instead of device 0x%x", |
| 890 | __func__, msdDevice, device); |
| 891 | device = msdDevice; |
| 892 | } else { |
| 893 | *output = AUDIO_IO_HANDLE_NONE; |
| 894 | } |
| 895 | } |
| 896 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 897 | *output = getOutputForDevice(device, session, *stream, config, flags); |
| 898 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 899 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 900 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 901 | return INVALID_OPERATION; |
| 902 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 903 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 904 | outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 905 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 906 | : AUDIO_PORT_HANDLE_NONE; |
| 907 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 908 | exit: |
| 909 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 910 | .format = config->format, |
| 911 | .channel_mask = config->channel_mask }; |
| 912 | sp<TrackClientDescriptor> clientDesc = |
| 913 | new TrackClientDescriptor(*portId, uid, session, |
| 914 | attributes, clientConfig, requestedDeviceId, *stream, *flags); |
| 915 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
| 916 | outputDesc->clients().emplace(*portId, clientDesc); |
| 917 | |
| 918 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d for port ID %d", |
| 919 | *output, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 920 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 921 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 925 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 926 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 927 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 928 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 929 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 930 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 931 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 932 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 933 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 934 | // open a direct output if required by specified parameters |
| 935 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 936 | // and all common behaviors are driven by checking only the direct flag |
| 937 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 938 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 939 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 941 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 942 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 943 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 944 | // only allow deep buffering for music stream type |
| 945 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 946 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 947 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 948 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 949 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 950 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 951 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 952 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 953 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 954 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 955 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 956 | audio_is_linear_pcm(config->format)) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 957 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 958 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 959 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 960 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 961 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 962 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 963 | sp<IOProfile> profile; |
| 964 | |
| 965 | // 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] | 966 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 967 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 968 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 969 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 970 | goto non_direct_output; |
| 971 | } |
| 972 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 973 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 974 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 975 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 976 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 977 | // This may prevent offloading in rare situations where effects are left active by apps |
| 978 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 979 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 980 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 981 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 982 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 983 | config->sample_rate, |
| 984 | config->format, |
| 985 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 986 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 989 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 990 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 991 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 992 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 993 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 994 | // reuse direct output if currently open by the same client |
| 995 | // and configured with same parameters |
| 996 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 997 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 998 | (config->channel_mask == desc->mChannelMask) && |
| 999 | (session == desc->mDirectClientSession)) { |
| 1000 | desc->mDirectOpenCount++; |
| 1001 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 1002 | mOutputs.keyAt(i), session); |
| 1003 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1007 | |
| 1008 | if (!profile->canOpenNewIo()) { |
| 1009 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1010 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1011 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1012 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1013 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1014 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1015 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1016 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 1017 | : String8(""); |
| 1018 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1019 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1020 | |
| 1021 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1022 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1023 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1024 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1025 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1026 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1027 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1028 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1029 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1030 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1031 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1032 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1033 | // 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] | 1034 | if (audio_is_linear_pcm(config->format) && |
| 1035 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1036 | goto non_direct_output; |
| 1037 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1038 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1039 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1040 | outputDesc->mRefCount[stream] = 0; |
| 1041 | outputDesc->mStopTime[stream] = 0; |
| 1042 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1043 | outputDesc->mDirectClientSession = session; |
| 1044 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1045 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1046 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1047 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1048 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | return output; |
| 1050 | } |
| 1051 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1052 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1053 | |
| 1054 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1055 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1056 | 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] | 1057 | return AUDIO_IO_HANDLE_NONE; |
| 1058 | } |
| 1059 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | // ignoring channel mask due to downmix capability in mixer |
| 1061 | |
| 1062 | // open a non direct output |
| 1063 | |
| 1064 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1065 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1066 | // get which output is suitable for the specified stream. The actual |
| 1067 | // routing change will happen when startOutput() will be called |
| 1068 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1069 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1070 | // 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] | 1071 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1072 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1074 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1075 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1076 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1077 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1078 | return output; |
| 1079 | } |
| 1080 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1081 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
| 1082 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1083 | if (msdModule != 0) { |
| 1084 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1085 | msdModule->getHandle()); |
| 1086 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1087 | } |
| 1088 | return 0; |
| 1089 | } |
| 1090 | |
| 1091 | audio_devices_t AudioPolicyManager::getMsdAudioOutDeviceTypes() const { |
| 1092 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1093 | if (msdModule != 0) { |
| 1094 | return mAvailableOutputDevices.getDeviceTypesFromHwModule(msdModule->getHandle()); |
| 1095 | } |
| 1096 | return AUDIO_DEVICE_NONE; |
| 1097 | } |
| 1098 | |
| 1099 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1100 | AudioPatchCollection msdPatches; |
| 1101 | audio_module_handle_t msdModuleHandle = mHwModules.getModuleFromName( |
| 1102 | AUDIO_HARDWARE_MODULE_ID_MSD)->getHandle(); |
| 1103 | if (msdModuleHandle == AUDIO_MODULE_HANDLE_NONE) return msdPatches; |
| 1104 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1105 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1106 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1107 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1108 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1109 | source->ext.device.hw_module == msdModuleHandle) { |
| 1110 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1111 | } |
| 1112 | } |
| 1113 | } |
| 1114 | return msdPatches; |
| 1115 | } |
| 1116 | |
| 1117 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1118 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1119 | { |
| 1120 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1121 | if (msdModule == nullptr) { |
| 1122 | ALOGE("%s() unable to get MSD module", __func__); |
| 1123 | return NO_INIT; |
| 1124 | } |
| 1125 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1126 | if (deviceModule == nullptr) { |
| 1127 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1128 | return NO_INIT; |
| 1129 | } |
| 1130 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1131 | if (inputProfiles.isEmpty()) { |
| 1132 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1133 | return NO_INIT; |
| 1134 | } |
| 1135 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1136 | if (outputProfiles.isEmpty()) { |
| 1137 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1138 | return NO_INIT; |
| 1139 | } |
| 1140 | AudioProfileVector msdProfiles; |
| 1141 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1142 | for (const auto &inProfile : inputProfiles) { |
| 1143 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1144 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1145 | } |
| 1146 | } |
| 1147 | AudioProfileVector deviceProfiles; |
| 1148 | for (const auto &outProfile : outputProfiles) { |
| 1149 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1150 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1151 | } |
| 1152 | } |
| 1153 | struct audio_config_base bestSinkConfig; |
| 1154 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1155 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1156 | &bestSinkConfig); |
| 1157 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1158 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1159 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1160 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1161 | } |
| 1162 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1163 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1164 | sinkConfig->format = bestSinkConfig.format; |
| 1165 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1166 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1167 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1168 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1169 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1170 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1171 | sourceConfig->format = bestSinkConfig.format; |
| 1172 | // Copy input stream directly without any processing (e.g. resampling). |
| 1173 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1174 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1175 | if (hwAvSync) { |
| 1176 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1177 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1178 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1179 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1180 | } |
| 1181 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1182 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1183 | sinkConfig->config_mask |= config_mask; |
| 1184 | sourceConfig->config_mask |= config_mask; |
| 1185 | return NO_ERROR; |
| 1186 | } |
| 1187 | |
| 1188 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1189 | { |
| 1190 | PatchBuilder patchBuilder; |
| 1191 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1192 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1193 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1194 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1195 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1196 | // For now, we just forcefully try with HwAvSync first. |
| 1197 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1198 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1199 | getBestMsdAudioProfileFor( |
| 1200 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1201 | if (res == NO_ERROR) { |
| 1202 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1203 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1204 | } |
| 1205 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1206 | " supporting PCM format conversion.", __func__); |
| 1207 | return patchBuilder; |
| 1208 | } |
| 1209 | |
| 1210 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1211 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1212 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1213 | // Use media strategy for unspecified output device. This should only |
| 1214 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1215 | // therefore invalidate explicit routing requests. |
| 1216 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1217 | } |
| 1218 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1219 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1220 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1221 | if (!msdPatches.isEmpty()) { |
| 1222 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1223 | "The current MSD prototype only supports one output patch"); |
| 1224 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1225 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1226 | return NO_ERROR; |
| 1227 | } |
| 1228 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1229 | } |
| 1230 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1231 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1232 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1233 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1234 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1235 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1236 | return status; |
| 1237 | } |
| 1238 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1239 | 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] | 1240 | audio_output_flags_t flags, |
| 1241 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1242 | { |
| 1243 | // select one output among several that provide a path to a particular device or set of |
| 1244 | // devices (the list was previously build by getOutputsForDevice()). |
| 1245 | // The priority is as follows: |
| 1246 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1247 | // 2: the output with the bit depth the closest to the requested one |
| 1248 | // 3: the primary output |
| 1249 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1250 | |
| 1251 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1252 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1253 | } |
| 1254 | if (outputs.size() == 1) { |
| 1255 | return outputs[0]; |
| 1256 | } |
| 1257 | |
| 1258 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1259 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1260 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1261 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1262 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1263 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1264 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1265 | for (audio_io_handle_t output : outputs) { |
| 1266 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1267 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1268 | // if a valid format is specified, skip output if not compatible |
| 1269 | if (format != AUDIO_FORMAT_INVALID) { |
| 1270 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1271 | if (format != outputDesc->mFormat) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1272 | continue; |
| 1273 | } |
| 1274 | } else if (!audio_is_linear_pcm(format)) { |
| 1275 | continue; |
| 1276 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1277 | if (AudioPort::isBetterFormatMatch( |
| 1278 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1279 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1280 | bestFormat = outputDesc->mFormat; |
| 1281 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1284 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1285 | if (commonFlags >= maxCommonFlags) { |
| 1286 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1287 | if (format != AUDIO_FORMAT_INVALID |
| 1288 | && AudioPort::isBetterFormatMatch( |
| 1289 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1290 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1291 | bestFormatForFlags = outputDesc->mFormat; |
| 1292 | } |
| 1293 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1294 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1295 | maxCommonFlags = commonFlags; |
| 1296 | bestFormatForFlags = outputDesc->mFormat; |
| 1297 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1298 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1300 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1301 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | } |
| 1305 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1306 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1307 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1308 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1309 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1310 | return outputForFormat; |
| 1311 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1312 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1313 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | return outputs[0]; |
| 1317 | } |
| 1318 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1319 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1320 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1321 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1322 | |
| 1323 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1324 | if (outputDesc == 0) { |
| 1325 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1326 | return BAD_VALUE; |
| 1327 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1328 | sp<TrackClientDescriptor> client = outputDesc->clients()[portId]; |
| 1329 | audio_stream_type_t stream = client->stream(); |
| 1330 | audio_session_t session = client->session(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1331 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1332 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1333 | outputDesc->mIoHandle, stream, session); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1334 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1335 | status_t status = outputDesc->start(); |
| 1336 | if (status != NO_ERROR) { |
| 1337 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1338 | } |
| 1339 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1340 | // Routing? |
| 1341 | mOutputRoutes.incRouteActivity(session); |
| 1342 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1343 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1344 | AudioMix *policyMix = NULL; |
| 1345 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1346 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1347 | policyMix = outputDesc->mPolicyMix; |
| 1348 | address = policyMix->mDeviceAddress.string(); |
| 1349 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1350 | newDevice = policyMix->mDeviceType; |
| 1351 | } else { |
| 1352 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1353 | } |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 1354 | } else if (mOutputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1355 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1356 | if (newDevice != outputDesc->device()) { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1357 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1358 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1359 | } else { |
| 1360 | newDevice = AUDIO_DEVICE_NONE; |
| 1361 | } |
| 1362 | |
| 1363 | uint32_t delayMs = 0; |
| 1364 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1365 | status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1366 | |
| 1367 | if (status != NO_ERROR) { |
| 1368 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1369 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1370 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1371 | } |
| 1372 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1373 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1374 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1375 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1376 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1377 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1378 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1379 | "remote-submix"); |
| 1380 | } |
| 1381 | |
| 1382 | if (delayMs != 0) { |
| 1383 | usleep(delayMs * 1000); |
| 1384 | } |
| 1385 | |
| 1386 | return status; |
| 1387 | } |
| 1388 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1389 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1390 | audio_stream_type_t stream, |
| 1391 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1392 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1393 | uint32_t *delayMs) |
| 1394 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1395 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1396 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1397 | |
| 1398 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1399 | if (stream == AUDIO_STREAM_TTS) { |
| 1400 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1401 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1402 | return INVALID_OPERATION; |
| 1403 | } else { |
| 1404 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1405 | } |
| 1406 | } else { |
| 1407 | // some playback other than beacon starts |
| 1408 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1409 | } |
| 1410 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1411 | // 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] | 1412 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1413 | bool force = !outputDesc->isActive() && |
| 1414 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1415 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1416 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1417 | // It covers a common case when there is no materially active audio |
| 1418 | // and muting would result in unnecessary delay and dropped audio. |
| 1419 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1420 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1421 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1422 | // increment usage count for this stream on the requested output: |
| 1423 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1424 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1425 | outputDesc->changeRefCount(stream, 1); |
| 1426 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1427 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1428 | selectOutputForMusicEffects(); |
| 1429 | } |
| 1430 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1431 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1432 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1433 | if (device == AUDIO_DEVICE_NONE) { |
| 1434 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1435 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1436 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1437 | routing_strategy strategy = getStrategy(stream); |
| 1438 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1439 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1440 | (beaconMuteLatency > 0); |
| 1441 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1442 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1443 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1445 | // An output has a shared device if |
| 1446 | // - managed by the same hw module |
| 1447 | // - supports the currently selected device |
| 1448 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1449 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1450 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1451 | // force a device change if any other output is: |
| 1452 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1453 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1454 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1455 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1456 | // 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] | 1457 | // change the device currently selected by the other output. |
| 1458 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1459 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1460 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1461 | force = true; |
| 1462 | } |
| 1463 | // 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] | 1464 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1465 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1466 | const uint32_t latencyMs = desc->latency(); |
| 1467 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1468 | |
| 1469 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1470 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1471 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Require mute check if another output is on a shared device |
| 1474 | // and currently active to have proper drain and avoid pops. |
| 1475 | // Note restoring AudioTracks onto this output needs to invoke |
| 1476 | // a volume ramp if there is no mute. |
| 1477 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1480 | |
| 1481 | const uint32_t muteWaitMs = |
| 1482 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1483 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1484 | // apply volume rules for current stream and device if necessary |
| 1485 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1486 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1487 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1488 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | |
| 1490 | // update the outputs if starting an output with a stream that can affect notification |
| 1491 | // routing |
| 1492 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1493 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1494 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1495 | if (strategy == STRATEGY_SONIFICATION) { |
| 1496 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1497 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1498 | |
| 1499 | if (waitMs > muteWaitMs) { |
| 1500 | *delayMs = waitMs - muteWaitMs; |
| 1501 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1502 | |
| 1503 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1504 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1505 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1506 | // change occurs after the MixerThread starts and causes a stream volume |
| 1507 | // glitch. |
| 1508 | // |
| 1509 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1510 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1511 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1512 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1513 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1514 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1515 | } |
| 1516 | |
Tomoharu Kasahara | a81f098 | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1517 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1518 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1519 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1520 | } |
| 1521 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1522 | return NO_ERROR; |
| 1523 | } |
| 1524 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1525 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1526 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1527 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1528 | |
| 1529 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1530 | if (outputDesc == 0) { |
| 1531 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1532 | return BAD_VALUE; |
| 1533 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1534 | sp<TrackClientDescriptor> client = outputDesc->clients()[portId]; |
| 1535 | audio_stream_type_t stream = client->stream(); |
| 1536 | audio_session_t session = client->session(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1537 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1538 | ALOGV("stopOutput() output %d, stream %d, session %d", outputDesc->mIoHandle, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1539 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1540 | if (outputDesc->mRefCount[stream] == 1) { |
| 1541 | // Automatically disable the remote submix input when output is stopped on a |
| 1542 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1543 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1544 | outputDesc->mPolicyMix != NULL && |
| 1545 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1546 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1547 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1548 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1549 | "remote-submix"); |
| 1550 | } |
| 1551 | } |
| 1552 | |
| 1553 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1554 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1555 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1556 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1557 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1558 | |
| 1559 | if (forceDeviceUpdate) { |
| 1560 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1561 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1564 | status_t status = stopSource(outputDesc, stream, forceDeviceUpdate); |
| 1565 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1566 | if (status == NO_ERROR ) { |
| 1567 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1568 | } |
| 1569 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1572 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1573 | audio_stream_type_t stream, |
| 1574 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1575 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1576 | // always handle stream stop, check which stream type is stopping |
| 1577 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1578 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1579 | if (outputDesc->mRefCount[stream] > 0) { |
| 1580 | // decrement usage count of this stream on the output |
| 1581 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1582 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1583 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1584 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1585 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1586 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1587 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1588 | // the track stop() command is received and at that time the audio track buffer can |
| 1589 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1590 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1591 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1592 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1593 | |
| 1594 | // force restoring the device selection on other active outputs if it differs from the |
| 1595 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1596 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1597 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1598 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1599 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1600 | desc->isActive() && |
| 1601 | outputDesc->sharesHwModuleWith(desc) && |
| 1602 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1603 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1604 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1605 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1606 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1607 | newDevice2, |
| 1608 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1609 | delayMs); |
| 1610 | // re-apply device specific volume if not done by setOutputDevice() |
| 1611 | if (!force) { |
| 1612 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1613 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1614 | } |
| 1615 | } |
| 1616 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1617 | handleNotificationRoutingForStream(stream); |
| 1618 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1619 | |
| 1620 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1621 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1622 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1623 | } |
| 1624 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1625 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1626 | selectOutputForMusicEffects(); |
| 1627 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1628 | return NO_ERROR; |
| 1629 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1630 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1631 | return INVALID_OPERATION; |
| 1632 | } |
| 1633 | } |
| 1634 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1635 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1636 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1637 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1638 | |
| 1639 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1640 | if (outputDesc == 0) { |
| 1641 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1642 | return; |
| 1643 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1644 | sp<TrackClientDescriptor> client = outputDesc->clients()[portId]; |
| 1645 | audio_session_t session = client->session(); |
| 1646 | |
| 1647 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1648 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1649 | // Routing |
| 1650 | mOutputRoutes.removeRoute(session); |
| 1651 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1652 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1653 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1654 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1655 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1656 | return; |
| 1657 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1658 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1659 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1660 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1663 | outputDesc->clients().erase(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1667 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1668 | audio_io_handle_t *input, |
| 1669 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1670 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1671 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1672 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1673 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1674 | input_type_t *inputType, |
| 1675 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1676 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1677 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1678 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1679 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1680 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1681 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1682 | // handle legacy remote submix case where the address was not always specified |
| 1683 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1684 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1685 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1686 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1687 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1688 | sp<AudioInputDescriptor> inputDesc; |
| 1689 | sp<RecordClientDescriptor> clientDesc; |
| 1690 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1691 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1692 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1693 | inputSource = AUDIO_SOURCE_MIC; |
| 1694 | } |
| 1695 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1696 | // Explicit routing? |
| 1697 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1698 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1699 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1700 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1701 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1702 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1703 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1704 | // possible |
| 1705 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1706 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1707 | ssize_t index = mInputs.indexOfKey(*input); |
| 1708 | if (index < 0) { |
| 1709 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1710 | status = BAD_VALUE; |
| 1711 | goto error; |
| 1712 | } |
| 1713 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1714 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1715 | if (audioSession == 0) { |
| 1716 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1717 | status = BAD_VALUE; |
| 1718 | goto error; |
| 1719 | } |
| 1720 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1721 | // 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] | 1722 | // corresponds to a new client and is only permitted from the same UID. |
| 1723 | // If the first UID is silenced, allow a new UID connection and replace with new UID |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1724 | if (audioSession->openCount() == 1) { |
| 1725 | audioSession->setUid(uid); |
| 1726 | } else if (audioSession->uid() != uid) { |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1727 | if (!audioSession->isSilenced()) { |
| 1728 | ALOGW("getInputForAttr() bad uid %d for session %d uid %d", |
| 1729 | uid, session, audioSession->uid()); |
| 1730 | status = INVALID_OPERATION; |
| 1731 | goto error; |
| 1732 | } |
| 1733 | audioSession->setUid(uid); |
| 1734 | audioSession->setSilenced(false); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1735 | } |
| 1736 | audioSession->changeOpenCount(1); |
| 1737 | *inputType = API_INPUT_LEGACY; |
| 1738 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1739 | *portId = AudioPort::getNextUniqueId(); |
| 1740 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1741 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(inputDesc->mDevice); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1742 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1743 | : AUDIO_PORT_HANDLE_NONE; |
| 1744 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
| 1745 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1746 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | *input = AUDIO_IO_HANDLE_NONE; |
| 1750 | *inputType = API_INPUT_INVALID; |
| 1751 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1752 | halInputSource = inputSource; |
| 1753 | |
| 1754 | // TODO: check for existing client for this port ID |
| 1755 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1756 | *portId = AudioPort::getNextUniqueId(); |
| 1757 | } |
| 1758 | |
| 1759 | audio_devices_t device; |
| 1760 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1761 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1762 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1763 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1764 | if (status != NO_ERROR) { |
| 1765 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1766 | } |
| 1767 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1768 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1769 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1770 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1771 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1772 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1773 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1774 | status = BAD_VALUE; |
| 1775 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1776 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1777 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1778 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1779 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1780 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1781 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1782 | // know about it and is therefore considered "legacy" |
| 1783 | *inputType = API_INPUT_LEGACY; |
| 1784 | } else { |
| 1785 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1786 | // an external policy |
| 1787 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1788 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1789 | } else if (audio_is_remote_submix_device(device)) { |
| 1790 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1791 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1792 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1793 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1794 | } else { |
| 1795 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1796 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1797 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1801 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1802 | policyMix); |
| 1803 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1804 | status = INVALID_OPERATION; |
| 1805 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1806 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1807 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1808 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1809 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1810 | : AUDIO_PORT_HANDLE_NONE; |
| 1811 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1812 | exit: |
| 1813 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
| 1814 | *attr, *config, requestedDeviceId, inputSource, flags); |
| 1815 | inputDesc = mInputs.valueFor(*input); |
| 1816 | inputDesc->clients().emplace(*portId, clientDesc); |
| 1817 | |
| 1818 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1819 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1820 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1821 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1822 | |
| 1823 | error: |
| 1824 | mInputRoutes.removeRoute(session); |
| 1825 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | |
| 1829 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1830 | String8 address, |
| 1831 | audio_session_t session, |
| 1832 | uid_t uid, |
| 1833 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1834 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1835 | audio_input_flags_t flags, |
| 1836 | AudioMix *policyMix) |
| 1837 | { |
| 1838 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1839 | audio_source_t halInputSource = inputSource; |
| 1840 | bool isSoundTrigger = false; |
| 1841 | |
| 1842 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1843 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1844 | if (index >= 0) { |
| 1845 | input = mSoundTriggerSessions.valueFor(session); |
| 1846 | isSoundTrigger = true; |
| 1847 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1848 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1849 | } else { |
| 1850 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1851 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1852 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1853 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1854 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1857 | // find a compatible input profile (not necessarily identical in parameters) |
| 1858 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1859 | // sampling rate and flags may be updated by getInputProfile |
| 1860 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1861 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1862 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1863 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1864 | audio_input_flags_t profileFlags = flags; |
| 1865 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1866 | 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] | 1867 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1868 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1869 | profileFlags); |
| 1870 | if (profile != 0) { |
| 1871 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1872 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1873 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1874 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1875 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1876 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1877 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1878 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1879 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1880 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1881 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1882 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1883 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1884 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1885 | if (samplingRate == 0) { |
| 1886 | samplingRate = profileSamplingRate; |
| 1887 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1888 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1889 | if (profile->getModuleHandle() == 0) { |
| 1890 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1891 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1892 | } |
| 1893 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1894 | sp<AudioSession> audioSession = new AudioSession(session, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1895 | inputSource, |
| 1896 | config->format, |
| 1897 | samplingRate, |
| 1898 | config->channel_mask, |
| 1899 | flags, |
| 1900 | uid, |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 1901 | isSoundTrigger); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1902 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1903 | if (!profile->canOpenNewIo()) { |
| 1904 | return AUDIO_IO_HANDLE_NONE; |
| 1905 | } |
| 1906 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1907 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1908 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1909 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1910 | lConfig.sample_rate = profileSamplingRate; |
| 1911 | lConfig.channel_mask = profileChannelMask; |
| 1912 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1913 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1914 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1915 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1916 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1917 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1918 | } |
| 1919 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1920 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1921 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1922 | |
| 1923 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1924 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1925 | (profileSamplingRate != lConfig.sample_rate) || |
| 1926 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1927 | (profileChannelMask != lConfig.channel_mask)) { |
| 1928 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1929 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1930 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1931 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1932 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1933 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1934 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1937 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1938 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1939 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1940 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1941 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1942 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1943 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1946 | //static |
| 1947 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1948 | { |
| 1949 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1950 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1951 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1952 | } |
| 1953 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1954 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1955 | const sp<AudioSession>& audioSession) |
| 1956 | { |
| 1957 | // Do not allow capture if an active voice call is using a software patch and |
| 1958 | // the call TX source device is on the same HW module. |
| 1959 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1960 | // call TX device but this information is not in the audio patch |
| 1961 | if (mCallTxPatch != 0 && |
| 1962 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
| 1966 | // starting concurrent capture is enabled if: |
| 1967 | // 1) capturing for re-routing |
| 1968 | // 2) capturing for HOTWORD source |
| 1969 | // 3) capturing for FM TUNER source |
| 1970 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1971 | |
| 1972 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1973 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1974 | return true; |
| 1975 | } |
| 1976 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1977 | for (const auto& activeInput : mInputs.getActiveInputs()) { |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1978 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1979 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1980 | return false; |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | return true; |
| 1985 | } |
| 1986 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1987 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1988 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1989 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1990 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1991 | unsigned int numModules = 0; |
| 1992 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1993 | |
| 1994 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1995 | if (status == NO_ERROR && numModules != 0) { |
| 1996 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1997 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1998 | if (nModules == NULL) { |
| 1999 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 2000 | // ram the next time this function is called. |
| 2001 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 2002 | return false; |
| 2003 | } |
| 2004 | |
| 2005 | status = SoundTrigger::listModules(nModules, &numModules); |
| 2006 | if (status == NO_ERROR) { |
| 2007 | soundTriggerSupportsConcurrentCapture = true; |
| 2008 | for (size_t i = 0; i < numModules; ++i) { |
| 2009 | soundTriggerSupportsConcurrentCapture &= |
| 2010 | nModules[i].properties.concurrent_capture; |
| 2011 | } |
| 2012 | } |
| 2013 | free(nModules); |
| 2014 | } |
| 2015 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 2016 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 2017 | } |
| 2018 | return mSoundTriggerSupportsConcurrentCapture; |
| 2019 | } |
| 2020 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2021 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2022 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2023 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2024 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2025 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2026 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
| 2027 | |
| 2028 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2029 | |
| 2030 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2031 | if (inputDesc == 0) { |
| 2032 | ALOGW("startInput() no input for client %d", portId); |
| 2033 | return BAD_VALUE; |
| 2034 | } |
| 2035 | sp<RecordClientDescriptor> client = inputDesc->clients()[portId]; |
| 2036 | audio_session_t session = client->session(); |
| 2037 | audio_io_handle_t input = inputDesc->mIoHandle; |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2038 | |
| 2039 | ALOGV("AudioPolicyManager::startInput(input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 2040 | input, session, silenced, *concurrency); |
| 2041 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2042 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 2043 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2044 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 2045 | return BAD_VALUE; |
| 2046 | } |
| 2047 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2048 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 2049 | if (mCallTxPatch != 0 && |
| 2050 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 2051 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2052 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2053 | return INVALID_OPERATION; |
| 2054 | } |
| 2055 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2056 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2057 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2058 | // If a UID is idle and records silence and another not silenced recording starts |
| 2059 | // from another UID (idle or active) we stop the current idle UID recording in |
| 2060 | // favor of the new one - "There can be only one" TM |
| 2061 | if (!silenced) { |
| 2062 | for (const auto& activeDesc : activeInputs) { |
| 2063 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 2064 | activeDesc->getId() == inputDesc->getId()) { |
| 2065 | continue; |
| 2066 | } |
| 2067 | |
| 2068 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions( |
| 2069 | true /*activeOnly*/); |
| 2070 | sp<AudioSession> activeSession = activeSessions.valueAt(0); |
| 2071 | if (activeSession->isSilenced()) { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2072 | closeSession(activeDesc, activeSession); |
| 2073 | ALOGV("startInput() session %d stopping silenced session %d", session, activeSession->session()); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2074 | activeInputs = mInputs.getActiveInputs(); |
| 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | |
| 2079 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 2080 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 2081 | activeDesc->getId() == inputDesc->getId()) { |
| 2082 | continue; |
| 2083 | } |
| 2084 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2085 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 2086 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 2087 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2088 | if (activeDesc->hasPreemptedSession(session)) { |
| 2089 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 2090 | "other input %d already started for HOTWORD", |
| 2091 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2092 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2093 | return INVALID_OPERATION; |
| 2094 | } |
| 2095 | } else { |
| 2096 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 2097 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2098 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2099 | return INVALID_OPERATION; |
| 2100 | } |
| 2101 | } else { |
| 2102 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 2103 | ALOGW("startInput(%d) failed: other input %d already started", |
| 2104 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2105 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2106 | return INVALID_OPERATION; |
| 2107 | } |
| 2108 | } |
| 2109 | } |
| 2110 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2111 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 2112 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 2113 | // that's running. |
| 2114 | const bool allowConcurrentWithSoundTrigger = |
| 2115 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 2116 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2117 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2118 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2119 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 2120 | continue; |
| 2121 | } |
| 2122 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2123 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 2124 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2125 | AudioSessionCollection activeSessions = |
| 2126 | activeDesc->getAudioSessions(true /*activeOnly*/); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2127 | sp<AudioSession> activeSession = activeSessions[0]; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2128 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2129 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2130 | sessions.add(activeSession->session()); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2131 | inputDesc->setPreemptedSessions(sessions); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2132 | closeSession(inputDesc, activeSession); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2133 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 2134 | input, activeDesc->mIoHandle); |
| 2135 | } |
| 2136 | } |
| 2137 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2138 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2139 | // Make sure we start with the correct silence state |
| 2140 | audioSession->setSilenced(silenced); |
| 2141 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2142 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2143 | // are considered for device selection |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 2144 | inputDesc->changeRefCount(session, 1); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2145 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2146 | // Routing? |
| 2147 | mInputRoutes.incRouteActivity(session); |
| 2148 | |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 2149 | if (audioSession->activeCount() == 1 || mInputRoutes.getAndClearRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2150 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2151 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2152 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 2153 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2154 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2155 | status_t status = inputDesc->start(); |
| 2156 | if (status != NO_ERROR) { |
| 2157 | mInputRoutes.decRouteActivity(session); |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 2158 | inputDesc->changeRefCount(session, -1); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2159 | return status; |
| 2160 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2161 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2162 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2163 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2164 | if ((inputDesc->mPolicyMix != NULL) |
| 2165 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2166 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2167 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2168 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2169 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2170 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2171 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 2172 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2173 | SoundTrigger::setCaptureState(true); |
| 2174 | } |
| 2175 | |
| 2176 | // automatically enable the remote submix output when input is started if not |
| 2177 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2178 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2179 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2180 | String8 address = String8(""); |
| 2181 | if (inputDesc->mPolicyMix == NULL) { |
| 2182 | address = String8("0"); |
| 2183 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2184 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2185 | } |
| 2186 | if (address != "") { |
| 2187 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2188 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2189 | address, "remote-submix"); |
| 2190 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2191 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2192 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2193 | } |
| 2194 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2195 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2196 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2197 | return NO_ERROR; |
| 2198 | } |
| 2199 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2200 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2201 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2202 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2203 | |
| 2204 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2205 | if (inputDesc == 0) { |
| 2206 | ALOGW("stopInput() no input for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2207 | return BAD_VALUE; |
| 2208 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2209 | sp<RecordClientDescriptor> client = inputDesc->clients()[portId]; |
| 2210 | audio_session_t session = client->session(); |
| 2211 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2212 | |
| 2213 | ALOGV("stopInput() input %d", input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2214 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2215 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2216 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2217 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2218 | ALOGW("stopInput() input %d already stopped", input); |
| 2219 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Eric Laurent | a18dced | 2018-07-20 15:14:54 -0700 | [diff] [blame] | 2222 | inputDesc->changeRefCount(session, -1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2223 | |
| 2224 | // Routing? |
| 2225 | mInputRoutes.decRouteActivity(session); |
| 2226 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2227 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2228 | inputDesc->stop(); |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2229 | if (inputDesc->isActive()) { |
| 2230 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2231 | } else { |
| 2232 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2233 | if ((inputDesc->mPolicyMix != NULL) |
| 2234 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2235 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2236 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2237 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2238 | |
| 2239 | // automatically disable the remote submix output when input is stopped if not |
| 2240 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2241 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2242 | String8 address = String8(""); |
| 2243 | if (inputDesc->mPolicyMix == NULL) { |
| 2244 | address = String8("0"); |
| 2245 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2246 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2247 | } |
| 2248 | if (address != "") { |
| 2249 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2250 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2251 | address, "remote-submix"); |
| 2252 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2253 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2254 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2255 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2256 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2257 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2258 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2259 | // primary HW module |
| 2260 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2261 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2262 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2263 | SoundTrigger::setCaptureState(false); |
| 2264 | } |
| 2265 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2266 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2267 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2268 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2269 | } |
| 2270 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2271 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2272 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2273 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2274 | |
| 2275 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2276 | if (inputDesc == 0) { |
| 2277 | ALOGW("releaseInput() no input for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2278 | return; |
| 2279 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2280 | sp<RecordClientDescriptor> client = inputDesc->clients()[portId]; |
| 2281 | audio_session_t session = client->session(); |
| 2282 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2283 | |
| 2284 | ALOGV("releaseInput() %d", input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2285 | |
| 2286 | // Routing |
| 2287 | mInputRoutes.removeRoute(session); |
| 2288 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2289 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2290 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2291 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2292 | return; |
| 2293 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2294 | |
| 2295 | if (audioSession->openCount() == 0) { |
| 2296 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2297 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2298 | return; |
| 2299 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2300 | |
| 2301 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2302 | inputDesc->removeAudioSession(session); |
| 2303 | } |
| 2304 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2305 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2306 | ALOGV("releaseInput() exit > 0"); |
| 2307 | return; |
| 2308 | } |
| 2309 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2310 | closeInput(input); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2311 | inputDesc->clients().erase(portId); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2312 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | ALOGV("releaseInput() exit"); |
| 2314 | } |
| 2315 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2316 | void AudioPolicyManager::closeSessions(const sp<AudioInputDescriptor>& input, bool activeOnly) |
| 2317 | { |
| 2318 | AudioSessionCollection sessions = input->getAudioSessions(activeOnly /*activeOnly*/); |
| 2319 | for (size_t i = 0; i < sessions.size(); i++) { |
| 2320 | closeSession(input, sessions[i]); |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | void AudioPolicyManager::closeSession(const sp<AudioInputDescriptor>& input, |
| 2325 | const sp<AudioSession>& session) |
| 2326 | { |
| 2327 | RecordClientVector clients = input->getClientsForSession(session->session()); |
| 2328 | |
| 2329 | for (const auto& client : clients) { |
| 2330 | stopInput(client->portId()); |
| 2331 | releaseInput(client->portId()); |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2336 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2337 | bool patchRemoved = false; |
| 2338 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2339 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2340 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2341 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2342 | if (patch_index >= 0) { |
| 2343 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2344 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2345 | mAudioPatches.removeItemsAt(patch_index); |
| 2346 | patchRemoved = true; |
| 2347 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2348 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2349 | } |
jiabin | 829a00b | 2018-04-04 16:28:32 -0700 | [diff] [blame] | 2350 | mInputRoutes.clear(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2351 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2352 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2353 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2354 | |
| 2355 | if (patchRemoved) { |
| 2356 | mpClientInterface->onAudioPatchListUpdate(); |
| 2357 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2360 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2361 | int indexMin, |
| 2362 | int indexMax) |
| 2363 | { |
| 2364 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2365 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2366 | |
| 2367 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2368 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2369 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2370 | continue; |
| 2371 | } |
| 2372 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2373 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2376 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2377 | int index, |
| 2378 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2379 | { |
| 2380 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2381 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2382 | // app that has MODIFY_PHONE_STATE permission. |
| 2383 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2384 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2385 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2386 | return BAD_VALUE; |
| 2387 | } |
| 2388 | if (!audio_is_output_device(device)) { |
| 2389 | return BAD_VALUE; |
| 2390 | } |
| 2391 | |
| 2392 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2393 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2394 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2395 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2396 | stream, device, index); |
| 2397 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2398 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2399 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2400 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2401 | continue; |
| 2402 | } |
| 2403 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2404 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2405 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2406 | // update volume on all outputs and streams matching the following: |
| 2407 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2408 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2409 | // the requested device |
| 2410 | // - For non default requested device, currently selected device on the output is either the |
| 2411 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2412 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2413 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2414 | status_t status = NO_ERROR; |
| 2415 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2416 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2417 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2418 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2419 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2420 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2421 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2422 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2423 | continue; |
| 2424 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2425 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2426 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2427 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2428 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2429 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2430 | continue; |
| 2431 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2432 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2433 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2434 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2435 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2436 | } else { |
| 2437 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2438 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2439 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2440 | // rescale index before applying to curStream as ranges may be different for |
| 2441 | // stream and curStream |
| 2442 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2443 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2444 | //FIXME: workaround for truncated touch sounds |
| 2445 | // delayed volume change for system stream to be removed when the problem is |
| 2446 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2447 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2448 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2449 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2450 | if (volStatus != NO_ERROR) { |
| 2451 | status = volStatus; |
| 2452 | } |
| 2453 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2454 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2455 | } |
| 2456 | return status; |
| 2457 | } |
| 2458 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2459 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2460 | int *index, |
| 2461 | audio_devices_t device) |
| 2462 | { |
| 2463 | if (index == NULL) { |
| 2464 | return BAD_VALUE; |
| 2465 | } |
| 2466 | if (!audio_is_output_device(device)) { |
| 2467 | return BAD_VALUE; |
| 2468 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2469 | // 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] | 2470 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2471 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2472 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2473 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2474 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2475 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2476 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2477 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2478 | return NO_ERROR; |
| 2479 | } |
| 2480 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2481 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2482 | { |
| 2483 | // select one output among several suitable for global effects. |
| 2484 | // The priority is as follows: |
| 2485 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2486 | // AudioFlinger will invalidate the track and the offloaded output |
| 2487 | // will be closed causing the effect to be moved to a PCM output. |
| 2488 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2489 | // 3: The primary output |
| 2490 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2491 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2492 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2493 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2494 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2495 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2496 | if (outputs.size() == 0) { |
| 2497 | return AUDIO_IO_HANDLE_NONE; |
| 2498 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2499 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2500 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2501 | bool activeOnly = true; |
| 2502 | |
| 2503 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2504 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2505 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2506 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2507 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2508 | for (audio_io_handle_t output : outputs) { |
| 2509 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2510 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2511 | continue; |
| 2512 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2513 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2514 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2515 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2516 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2517 | } |
| 2518 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2519 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2520 | } |
| 2521 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2522 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2526 | output = outputOffloaded; |
| 2527 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2528 | output = outputDeepBuffer; |
| 2529 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2530 | output = outputPrimary; |
| 2531 | } else { |
| 2532 | output = outputs[0]; |
| 2533 | } |
| 2534 | activeOnly = false; |
| 2535 | } |
| 2536 | |
| 2537 | if (output != mMusicEffectOutput) { |
| 2538 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2539 | mMusicEffectOutput = output; |
| 2540 | } |
| 2541 | |
| 2542 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2543 | return output; |
| 2544 | } |
| 2545 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2546 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2547 | { |
| 2548 | return selectOutputForMusicEffects(); |
| 2549 | } |
| 2550 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2551 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2552 | audio_io_handle_t io, |
| 2553 | uint32_t strategy, |
| 2554 | int session, |
| 2555 | int id) |
| 2556 | { |
| 2557 | ssize_t index = mOutputs.indexOfKey(io); |
| 2558 | if (index < 0) { |
| 2559 | index = mInputs.indexOfKey(io); |
| 2560 | if (index < 0) { |
| 2561 | ALOGW("registerEffect() unknown io %d", io); |
| 2562 | return INVALID_OPERATION; |
| 2563 | } |
| 2564 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2565 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2568 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2569 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2570 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2571 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2572 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2573 | continue; |
| 2574 | } |
| 2575 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2576 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2577 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2581 | { |
| 2582 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2583 | } |
| 2584 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2585 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2586 | { |
| 2587 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2588 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2589 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2590 | return true; |
| 2591 | } |
| 2592 | } |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2596 | // Register a list of custom mixes with their attributes and format. |
| 2597 | // When a mix is registered, corresponding input and output profiles are |
| 2598 | // added to the remote submix hw module. The profile contains only the |
| 2599 | // parameters (sampling rate, format...) specified by the mix. |
| 2600 | // The corresponding input remote submix device is also connected. |
| 2601 | // |
| 2602 | // When a remote submix device is connected, the address is checked to select the |
| 2603 | // appropriate profile and the corresponding input or output stream is opened. |
| 2604 | // |
| 2605 | // When capture starts, getInputForAttr() will: |
| 2606 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2607 | // - 2 if none found, getDeviceForInputSource() will: |
| 2608 | // - 2.1 look for a mix matching the attributes source |
| 2609 | // - 2.2 if none found, default to device selection by policy rules |
| 2610 | // At this time, the corresponding output remote submix device is also connected |
| 2611 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2612 | // after AudioTracks are invalidated |
| 2613 | // |
| 2614 | // When playback starts, getOutputForAttr() will: |
| 2615 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2616 | // - 2 if none found, look for a mix matching the attributes usage |
| 2617 | // - 3 if none found, default to device and output selection by policy rules. |
| 2618 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2619 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2620 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2621 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2622 | status_t res = NO_ERROR; |
| 2623 | |
| 2624 | sp<HwModule> rSubmixModule; |
| 2625 | // examine each mix's route type |
| 2626 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2627 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2628 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2629 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2630 | break; |
| 2631 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2632 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2633 | 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] | 2634 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2635 | rSubmixModule = mHwModules.getModuleFromName( |
| 2636 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2637 | if (rSubmixModule == 0) { |
| 2638 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2639 | i); |
| 2640 | res = INVALID_OPERATION; |
| 2641 | break; |
| 2642 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2643 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2644 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2645 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2646 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2647 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2648 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2649 | res = INVALID_OPERATION; |
| 2650 | break; |
| 2651 | } |
| 2652 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2653 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2654 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2655 | // stereo and let audio flinger do the channel conversion if needed. |
| 2656 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2657 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2658 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2659 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2660 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2661 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2662 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2663 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2664 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2665 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2666 | address.string(), "remote-submix"); |
| 2667 | } else { |
| 2668 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2669 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2670 | address.string(), "remote-submix"); |
| 2671 | } |
| 2672 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2673 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2674 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2675 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2676 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2677 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2678 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2679 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2680 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2681 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2682 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2683 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2684 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2685 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2686 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2687 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2688 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2689 | } else { |
| 2690 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2691 | } |
| 2692 | break; |
| 2693 | } |
| 2694 | } |
| 2695 | |
| 2696 | if (res != NO_ERROR) { |
| 2697 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2698 | i, device, address.string()); |
| 2699 | res = INVALID_OPERATION; |
| 2700 | break; |
| 2701 | } else if (!foundOutput) { |
| 2702 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2703 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2704 | res = INVALID_OPERATION; |
| 2705 | break; |
| 2706 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2707 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2708 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2709 | if (res != NO_ERROR) { |
| 2710 | unregisterPolicyMixes(mixes); |
| 2711 | } |
| 2712 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2716 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2717 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2718 | status_t res = NO_ERROR; |
| 2719 | sp<HwModule> rSubmixModule; |
| 2720 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2721 | for (const auto& mix : mixes) { |
| 2722 | 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] | 2723 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2724 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2725 | rSubmixModule = mHwModules.getModuleFromName( |
| 2726 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2727 | if (rSubmixModule == 0) { |
| 2728 | res = INVALID_OPERATION; |
| 2729 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2730 | } |
| 2731 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2732 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2733 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2734 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2735 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2736 | res = INVALID_OPERATION; |
| 2737 | continue; |
| 2738 | } |
| 2739 | |
| 2740 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2741 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2742 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2743 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2744 | address.string(), "remote-submix"); |
| 2745 | } |
| 2746 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2747 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2748 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2749 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2750 | address.string(), "remote-submix"); |
| 2751 | } |
| 2752 | rSubmixModule->removeOutputProfile(address); |
| 2753 | rSubmixModule->removeInputProfile(address); |
| 2754 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2755 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2756 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2757 | res = INVALID_OPERATION; |
| 2758 | continue; |
| 2759 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2760 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2761 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2762 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2763 | } |
| 2764 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2765 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2766 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2767 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2768 | String8 result; |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2769 | result.appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2770 | result.appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2771 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2772 | std::string stateLiteral; |
| 2773 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2774 | result.appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
| 2775 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2776 | "communications", "media", "record", "dock", "system", |
| 2777 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2778 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2779 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
| 2780 | result.appendFormat(" Force use for %s: %d\n", |
| 2781 | forceUses[i], mEngine->getForceUse(i)); |
| 2782 | } |
| 2783 | result.appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2784 | result.appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2785 | result.appendFormat(" Config source: %s\n", getConfig().getSource().c_str()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2786 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2787 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2788 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2789 | mAvailableInputDevices.dump(fd, String8("Available input")); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2790 | mHwModulesAll.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2791 | mOutputs.dump(fd); |
| 2792 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2793 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2794 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2795 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2796 | mPolicyMixes.dump(fd); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 2797 | mAudioSources.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2798 | |
| 2799 | return NO_ERROR; |
| 2800 | } |
| 2801 | |
| 2802 | // This function checks for the parameters which can be offloaded. |
| 2803 | // This can be enhanced depending on the capability of the DSP and policy |
| 2804 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2805 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2806 | { |
| 2807 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2808 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2809 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2810 | offloadInfo.format, |
| 2811 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2812 | offloadInfo.has_video); |
| 2813 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2814 | if (mMasterMono) { |
| 2815 | return false; // no offloading if mono is set. |
| 2816 | } |
| 2817 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2818 | // Check if offload has been disabled |
| 2819 | char propValue[PROPERTY_VALUE_MAX]; |
| 2820 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2821 | if (atoi(propValue) != 0) { |
| 2822 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2823 | return false; |
| 2824 | } |
| 2825 | } |
| 2826 | |
| 2827 | // Check if stream type is music, then only allow offload as of now. |
| 2828 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2829 | { |
| 2830 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2831 | return false; |
| 2832 | } |
| 2833 | |
| 2834 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2835 | const bool allowOffloadWithVideo = |
| 2836 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2837 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2838 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2839 | return false; |
| 2840 | } |
| 2841 | |
| 2842 | //If duration is less than minimum value defined in property, return false |
| 2843 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2844 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2845 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2846 | return false; |
| 2847 | } |
| 2848 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2849 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2850 | return false; |
| 2851 | } |
| 2852 | |
| 2853 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2854 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2855 | // detects there is an active non offloadable effect. |
| 2856 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2857 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2858 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2859 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2860 | return false; |
| 2861 | } |
| 2862 | |
| 2863 | // See if there is a profile to support this. |
| 2864 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2865 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2866 | offloadInfo.sample_rate, |
| 2867 | offloadInfo.format, |
| 2868 | offloadInfo.channel_mask, |
| 2869 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2870 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2871 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2874 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2875 | audio_port_type_t type, |
| 2876 | unsigned int *num_ports, |
| 2877 | struct audio_port *ports, |
| 2878 | unsigned int *generation) |
| 2879 | { |
| 2880 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2881 | generation == NULL) { |
| 2882 | return BAD_VALUE; |
| 2883 | } |
| 2884 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2885 | if (ports == NULL) { |
| 2886 | *num_ports = 0; |
| 2887 | } |
| 2888 | |
| 2889 | size_t portsWritten = 0; |
| 2890 | size_t portsMax = *num_ports; |
| 2891 | *num_ports = 0; |
| 2892 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2893 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2894 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2895 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2896 | for (const auto& dev : mAvailableOutputDevices) { |
| 2897 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2898 | continue; |
| 2899 | } |
| 2900 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2901 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2902 | } |
| 2903 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2904 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2905 | } |
| 2906 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2907 | for (const auto& dev : mAvailableInputDevices) { |
| 2908 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2909 | continue; |
| 2910 | } |
| 2911 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2912 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2913 | } |
| 2914 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2915 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2919 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2920 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2921 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2922 | } |
| 2923 | *num_ports += mInputs.size(); |
| 2924 | } |
| 2925 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2926 | size_t numOutputs = 0; |
| 2927 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2928 | if (!mOutputs[i]->isDuplicated()) { |
| 2929 | numOutputs++; |
| 2930 | if (portsWritten < portsMax) { |
| 2931 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2932 | } |
| 2933 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2934 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2935 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2936 | } |
| 2937 | } |
| 2938 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2939 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2940 | return NO_ERROR; |
| 2941 | } |
| 2942 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2943 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2944 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2945 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2946 | return BAD_VALUE; |
| 2947 | } |
| 2948 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2949 | if (dev != 0) { |
| 2950 | dev->toAudioPort(port); |
| 2951 | return NO_ERROR; |
| 2952 | } |
| 2953 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2954 | if (dev != 0) { |
| 2955 | dev->toAudioPort(port); |
| 2956 | return NO_ERROR; |
| 2957 | } |
| 2958 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2959 | if (out != 0) { |
| 2960 | out->toAudioPort(port); |
| 2961 | return NO_ERROR; |
| 2962 | } |
| 2963 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2964 | if (in != 0) { |
| 2965 | in->toAudioPort(port); |
| 2966 | return NO_ERROR; |
| 2967 | } |
| 2968 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2969 | } |
| 2970 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2971 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2972 | audio_patch_handle_t *handle, |
| 2973 | uid_t uid) |
| 2974 | { |
| 2975 | ALOGV("createAudioPatch()"); |
| 2976 | |
| 2977 | if (handle == NULL || patch == NULL) { |
| 2978 | return BAD_VALUE; |
| 2979 | } |
| 2980 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2981 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2982 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2983 | return BAD_VALUE; |
| 2984 | } |
| 2985 | // only one source per audio patch supported for now |
| 2986 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2987 | return INVALID_OPERATION; |
| 2988 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2989 | |
| 2990 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2991 | return INVALID_OPERATION; |
| 2992 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2993 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2994 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2995 | return INVALID_OPERATION; |
| 2996 | } |
| 2997 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2998 | |
| 2999 | sp<AudioPatch> patchDesc; |
| 3000 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 3001 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3002 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 3003 | patch->sources[0].role, |
| 3004 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3005 | #if LOG_NDEBUG == 0 |
| 3006 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3007 | 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] | 3008 | patch->sinks[i].role, |
| 3009 | patch->sinks[i].type); |
| 3010 | } |
| 3011 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3012 | |
| 3013 | if (index >= 0) { |
| 3014 | patchDesc = mAudioPatches.valueAt(index); |
| 3015 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3016 | mUidCached, patchDesc->mUid, uid); |
| 3017 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3018 | return INVALID_OPERATION; |
| 3019 | } |
| 3020 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 3021 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3022 | } |
| 3023 | |
| 3024 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3025 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3026 | if (outputDesc == NULL) { |
| 3027 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3028 | return BAD_VALUE; |
| 3029 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3030 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 3031 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3032 | if (patchDesc != 0) { |
| 3033 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 3034 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 3035 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 3036 | return BAD_VALUE; |
| 3037 | } |
| 3038 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3039 | DeviceVector devices; |
| 3040 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3041 | // Only support mix to devices connection |
| 3042 | // TODO add support for mix to mix connection |
| 3043 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3044 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 3045 | return INVALID_OPERATION; |
| 3046 | } |
| 3047 | sp<DeviceDescriptor> devDesc = |
| 3048 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3049 | if (devDesc == 0) { |
| 3050 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 3051 | return BAD_VALUE; |
| 3052 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3053 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3054 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3055 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3056 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3057 | NULL, // updatedSamplingRate |
| 3058 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3059 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3060 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3061 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3062 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3063 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 3064 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3065 | return INVALID_OPERATION; |
| 3066 | } |
| 3067 | devices.add(devDesc); |
| 3068 | } |
| 3069 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3070 | return INVALID_OPERATION; |
| 3071 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3072 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3073 | // TODO: reconfigure output format and channels here |
| 3074 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3075 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3076 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3077 | index = mAudioPatches.indexOfKey(*handle); |
| 3078 | if (index >= 0) { |
| 3079 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3080 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3081 | } |
| 3082 | patchDesc = mAudioPatches.valueAt(index); |
| 3083 | patchDesc->mUid = uid; |
| 3084 | ALOGV("createAudioPatch() success"); |
| 3085 | } else { |
| 3086 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3087 | return INVALID_OPERATION; |
| 3088 | } |
| 3089 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3090 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3091 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3092 | // only one sink supported when connecting an input device to a mix |
| 3093 | if (patch->num_sinks > 1) { |
| 3094 | return INVALID_OPERATION; |
| 3095 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3096 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3097 | if (inputDesc == NULL) { |
| 3098 | return BAD_VALUE; |
| 3099 | } |
| 3100 | if (patchDesc != 0) { |
| 3101 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3102 | return BAD_VALUE; |
| 3103 | } |
| 3104 | } |
| 3105 | sp<DeviceDescriptor> devDesc = |
| 3106 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3107 | if (devDesc == 0) { |
| 3108 | return BAD_VALUE; |
| 3109 | } |
| 3110 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3111 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3112 | devDesc->mAddress, |
| 3113 | patch->sinks[0].sample_rate, |
| 3114 | NULL, /*updatedSampleRate*/ |
| 3115 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3116 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3117 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3118 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3119 | // FIXME for the parameter type, |
| 3120 | // and the NONE |
| 3121 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3122 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3123 | return INVALID_OPERATION; |
| 3124 | } |
| 3125 | // TODO: reconfigure output format and channels here |
| 3126 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3127 | devDesc->type(), inputDesc->mIoHandle); |
| 3128 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3129 | index = mAudioPatches.indexOfKey(*handle); |
| 3130 | if (index >= 0) { |
| 3131 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3132 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3133 | } |
| 3134 | patchDesc = mAudioPatches.valueAt(index); |
| 3135 | patchDesc->mUid = uid; |
| 3136 | ALOGV("createAudioPatch() success"); |
| 3137 | } else { |
| 3138 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3139 | return INVALID_OPERATION; |
| 3140 | } |
| 3141 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3142 | // device to device connection |
| 3143 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3144 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3145 | return BAD_VALUE; |
| 3146 | } |
| 3147 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3148 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3149 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3150 | if (srcDeviceDesc == 0) { |
| 3151 | return BAD_VALUE; |
| 3152 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3153 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3154 | //update source and sink with our own data as the data passed in the patch may |
| 3155 | // be incomplete. |
| 3156 | struct audio_patch newPatch = *patch; |
| 3157 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3158 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3159 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3160 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3161 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3162 | return INVALID_OPERATION; |
| 3163 | } |
| 3164 | |
| 3165 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3166 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3167 | if (sinkDeviceDesc == 0) { |
| 3168 | return BAD_VALUE; |
| 3169 | } |
| 3170 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3171 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3172 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3173 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3174 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3175 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3176 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3177 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3178 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3179 | return INVALID_OPERATION; |
| 3180 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3181 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3182 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3183 | // if the sink device is reachable via an opened output stream, request to go via |
| 3184 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3185 | audio_io_handle_t output = selectOutput(outputs, |
| 3186 | AUDIO_OUTPUT_FLAG_NONE, |
| 3187 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3188 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3189 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3190 | if (outputDesc->isDuplicated()) { |
| 3191 | return INVALID_OPERATION; |
| 3192 | } |
| 3193 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3194 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3195 | newPatch.num_sources = 2; |
| 3196 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3197 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3198 | } |
| 3199 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3200 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3201 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3202 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3203 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3204 | status); |
| 3205 | return INVALID_OPERATION; |
| 3206 | } |
| 3207 | } else { |
| 3208 | return BAD_VALUE; |
| 3209 | } |
| 3210 | } else { |
| 3211 | return BAD_VALUE; |
| 3212 | } |
| 3213 | return NO_ERROR; |
| 3214 | } |
| 3215 | |
| 3216 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3217 | uid_t uid) |
| 3218 | { |
| 3219 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3220 | |
| 3221 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3222 | |
| 3223 | if (index < 0) { |
| 3224 | return BAD_VALUE; |
| 3225 | } |
| 3226 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3227 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3228 | mUidCached, patchDesc->mUid, uid); |
| 3229 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3230 | return INVALID_OPERATION; |
| 3231 | } |
| 3232 | |
| 3233 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3234 | patchDesc->mUid = mUidCached; |
| 3235 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3236 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3237 | if (outputDesc == NULL) { |
| 3238 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3239 | return BAD_VALUE; |
| 3240 | } |
| 3241 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3242 | setOutputDevice(outputDesc, |
| 3243 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3244 | true, |
| 3245 | 0, |
| 3246 | NULL); |
| 3247 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3248 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3249 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3250 | if (inputDesc == NULL) { |
| 3251 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3252 | return BAD_VALUE; |
| 3253 | } |
| 3254 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3255 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3256 | true, |
| 3257 | NULL); |
| 3258 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3259 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3260 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3261 | status, patchDesc->mAfPatchHandle); |
| 3262 | removeAudioPatch(patchDesc->mHandle); |
| 3263 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3264 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3265 | } else { |
| 3266 | return BAD_VALUE; |
| 3267 | } |
| 3268 | } else { |
| 3269 | return BAD_VALUE; |
| 3270 | } |
| 3271 | return NO_ERROR; |
| 3272 | } |
| 3273 | |
| 3274 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3275 | struct audio_patch *patches, |
| 3276 | unsigned int *generation) |
| 3277 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3278 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3279 | return BAD_VALUE; |
| 3280 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3281 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3282 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3283 | } |
| 3284 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3285 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3286 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3287 | ALOGV("setAudioPortConfig()"); |
| 3288 | |
| 3289 | if (config == NULL) { |
| 3290 | return BAD_VALUE; |
| 3291 | } |
| 3292 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3293 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3294 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3295 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3296 | } |
| 3297 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3298 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3299 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3300 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3301 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3302 | if (outputDesc == NULL) { |
| 3303 | return BAD_VALUE; |
| 3304 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3305 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3306 | "setAudioPortConfig() called on duplicated output %d", |
| 3307 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3308 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3309 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3310 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3311 | if (inputDesc == NULL) { |
| 3312 | return BAD_VALUE; |
| 3313 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3314 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3315 | } else { |
| 3316 | return BAD_VALUE; |
| 3317 | } |
| 3318 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3319 | sp<DeviceDescriptor> deviceDesc; |
| 3320 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3321 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3322 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3323 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3324 | } else { |
| 3325 | return BAD_VALUE; |
| 3326 | } |
| 3327 | if (deviceDesc == NULL) { |
| 3328 | return BAD_VALUE; |
| 3329 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3330 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3331 | } else { |
| 3332 | return BAD_VALUE; |
| 3333 | } |
| 3334 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3335 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3336 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3337 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3338 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3339 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3340 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3341 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3342 | if (status != NO_ERROR) { |
| 3343 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3344 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3345 | |
| 3346 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3347 | } |
| 3348 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3349 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3350 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3351 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3352 | clearAudioPatches(uid); |
| 3353 | clearSessionRoutes(uid); |
| 3354 | } |
| 3355 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3356 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3357 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3358 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3359 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3360 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3361 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3366 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3367 | audio_io_handle_t ouptutToSkip) |
| 3368 | { |
| 3369 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3370 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3371 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3372 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3373 | continue; |
| 3374 | } |
| 3375 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3376 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3377 | continue; |
| 3378 | } |
| 3379 | // If the default device for this strategy is on another output mix, |
| 3380 | // invalidate all tracks in this strategy to force re connection. |
| 3381 | // Otherwise select new device on the output mix. |
| 3382 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3383 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3384 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3385 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3386 | } |
| 3387 | } |
| 3388 | } else { |
| 3389 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3390 | setOutputDevice(outputDesc, newDevice, false); |
| 3391 | } |
| 3392 | } |
| 3393 | } |
| 3394 | |
| 3395 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3396 | { |
| 3397 | // remove output routes associated with this uid |
| 3398 | SortedVector<routing_strategy> affectedStrategies; |
| 3399 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3400 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3401 | if (route->mUid == uid) { |
| 3402 | mOutputRoutes.removeItemsAt(i); |
| 3403 | if (route->mDeviceDescriptor != 0) { |
| 3404 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3405 | } |
| 3406 | } |
| 3407 | } |
| 3408 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3409 | for (const auto& strategy : affectedStrategies) { |
| 3410 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
| 3413 | // remove input routes associated with this uid |
| 3414 | SortedVector<audio_source_t> affectedSources; |
| 3415 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3416 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3417 | if (route->mUid == uid) { |
| 3418 | mInputRoutes.removeItemsAt(i); |
| 3419 | if (route->mDeviceDescriptor != 0) { |
| 3420 | affectedSources.add(route->mSource); |
| 3421 | } |
| 3422 | } |
| 3423 | } |
| 3424 | // reroute inputs if necessary |
| 3425 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3426 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3427 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3428 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3429 | inputsToClose.add(inputDesc->mIoHandle); |
| 3430 | } |
| 3431 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3432 | for (const auto& input : inputsToClose) { |
| 3433 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3434 | } |
| 3435 | } |
| 3436 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3437 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3438 | { |
| 3439 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3440 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3441 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3442 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3443 | } |
| 3444 | } |
| 3445 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3446 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3447 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3448 | audio_io_handle_t *ioHandle, |
| 3449 | audio_devices_t *device) |
| 3450 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3451 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3452 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3453 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3454 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3455 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3456 | } |
| 3457 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3458 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3459 | const audio_attributes_t *attributes, |
| 3460 | audio_port_handle_t *portId, |
| 3461 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3462 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3463 | ALOGV("%s", __FUNCTION__); |
| 3464 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3465 | |
| 3466 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3467 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3468 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3469 | return BAD_VALUE; |
| 3470 | } |
| 3471 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3472 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3473 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3474 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3475 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3476 | return INVALID_OPERATION; |
| 3477 | } |
| 3478 | |
| 3479 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3480 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3481 | String8(source->ext.device.address)); |
| 3482 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3483 | 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] | 3484 | return BAD_VALUE; |
| 3485 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3486 | |
| 3487 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3488 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3489 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3490 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3491 | |
| 3492 | sp<SourceClientDescriptor> sourceDesc = |
| 3493 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
| 3494 | streamTypefromAttributesInt(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3495 | |
| 3496 | status_t status = connectAudioSource(sourceDesc); |
| 3497 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3498 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3499 | } |
| 3500 | return status; |
| 3501 | } |
| 3502 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3503 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3504 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3505 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3506 | |
| 3507 | // make sure we only have one patch per source. |
| 3508 | disconnectAudioSource(sourceDesc); |
| 3509 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3510 | audio_attributes_t attributes = sourceDesc->attributes(); |
| 3511 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
| 3512 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3513 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3514 | |
| 3515 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3516 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3517 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3518 | |
| 3519 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3520 | |
| 3521 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3522 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3523 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3524 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3525 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3526 | // create patch between src device and output device |
| 3527 | // create Hwoutput and add to mHwOutputs |
| 3528 | } else { |
| 3529 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3530 | audio_io_handle_t output = |
| 3531 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3532 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3533 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3534 | return INVALID_OPERATION; |
| 3535 | } |
| 3536 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3537 | if (outputDesc->isDuplicated()) { |
| 3538 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3539 | return INVALID_OPERATION; |
| 3540 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3541 | status_t status = outputDesc->start(); |
| 3542 | if (status != NO_ERROR) { |
| 3543 | return status; |
| 3544 | } |
| 3545 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3546 | // create a special patch with no sink and two sources: |
| 3547 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3548 | // be connected as well as the stream type for volume control |
| 3549 | // - the sink is defined by whatever output device is currently selected for the output |
| 3550 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3551 | PatchBuilder patchBuilder; |
| 3552 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3553 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3554 | &afPatchHandle, |
| 3555 | 0); |
| 3556 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3557 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3558 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3559 | if (status != NO_ERROR) { |
| 3560 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3561 | __FUNCTION__, status); |
| 3562 | return INVALID_OPERATION; |
| 3563 | } |
| 3564 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3565 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3566 | |
| 3567 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3568 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3569 | return status; |
| 3570 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3571 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3572 | if (delayMs != 0) { |
| 3573 | usleep(delayMs * 1000); |
| 3574 | } |
| 3575 | } |
| 3576 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3577 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3578 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3579 | |
| 3580 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3583 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3584 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3585 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3586 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3587 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3588 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3589 | return BAD_VALUE; |
| 3590 | } |
| 3591 | status_t status = disconnectAudioSource(sourceDesc); |
| 3592 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3593 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3594 | return status; |
| 3595 | } |
| 3596 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3597 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3598 | { |
| 3599 | if (mMasterMono == mono) { |
| 3600 | return NO_ERROR; |
| 3601 | } |
| 3602 | mMasterMono = mono; |
| 3603 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3604 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3605 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3606 | // |
| 3607 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3608 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3609 | // play back offloaded. |
| 3610 | if (mMasterMono) { |
| 3611 | Vector<audio_io_handle_t> offloaded; |
| 3612 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3613 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3614 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3615 | offloaded.push(desc->mIoHandle); |
| 3616 | } |
| 3617 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3618 | for (const auto& handle : offloaded) { |
| 3619 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | // update master mono for all remaining outputs |
| 3623 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3624 | updateMono(mOutputs.keyAt(i)); |
| 3625 | } |
| 3626 | return NO_ERROR; |
| 3627 | } |
| 3628 | |
| 3629 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3630 | { |
| 3631 | *mono = mMasterMono; |
| 3632 | return NO_ERROR; |
| 3633 | } |
| 3634 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3635 | float AudioPolicyManager::getStreamVolumeDB( |
| 3636 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3637 | { |
| 3638 | return computeVolume(stream, index, device); |
| 3639 | } |
| 3640 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3641 | status_t AudioPolicyManager::getSupportedFormats(audio_io_handle_t ioHandle, |
| 3642 | FormatVector& formats) { |
| 3643 | if (ioHandle == AUDIO_IO_HANDLE_NONE) { |
| 3644 | return BAD_VALUE; |
| 3645 | } |
| 3646 | String8 reply; |
| 3647 | reply = mpClientInterface->getParameters( |
| 3648 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
| 3649 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
| 3650 | AudioParameter repliedParameters(reply); |
| 3651 | if (repliedParameters.get( |
| 3652 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
| 3653 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 3654 | return BAD_VALUE; |
| 3655 | } |
| 3656 | for (auto format : formatsFromString(reply.string())) { |
| 3657 | // Only AUDIO_FORMAT_AAC_LC will be used in Settings UI for all AAC formats. |
| 3658 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3659 | if (format == AAC_FORMATS[i]) { |
| 3660 | format = AUDIO_FORMAT_AAC_LC; |
| 3661 | break; |
| 3662 | } |
| 3663 | } |
| 3664 | bool exist = false; |
| 3665 | for (size_t i = 0; i < formats.size(); i++) { |
| 3666 | if (format == formats[i]) { |
| 3667 | exist = true; |
| 3668 | break; |
| 3669 | } |
| 3670 | } |
| 3671 | bool isSurroundFormat = false; |
| 3672 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3673 | if (SURROUND_FORMATS[i] == format) { |
| 3674 | isSurroundFormat = true; |
| 3675 | break; |
| 3676 | } |
| 3677 | } |
| 3678 | if (!exist && isSurroundFormat) { |
| 3679 | formats.add(format); |
| 3680 | } |
| 3681 | } |
| 3682 | return NO_ERROR; |
| 3683 | } |
| 3684 | |
| 3685 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3686 | audio_format_t *surroundFormats, |
| 3687 | bool *surroundFormatsEnabled, |
| 3688 | bool reported) |
| 3689 | { |
| 3690 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3691 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3692 | return BAD_VALUE; |
| 3693 | } |
| 3694 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p", |
| 3695 | *numSurroundFormats, surroundFormats, surroundFormatsEnabled); |
| 3696 | |
| 3697 | // Only return value if there is HDMI output. |
| 3698 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3699 | return INVALID_OPERATION; |
| 3700 | } |
| 3701 | |
| 3702 | size_t formatsWritten = 0; |
| 3703 | size_t formatsMax = *numSurroundFormats; |
| 3704 | *numSurroundFormats = 0; |
| 3705 | FormatVector formats; |
| 3706 | if (reported) { |
| 3707 | // Only get surround formats which are reported by device. |
| 3708 | // First list already open outputs that can be routed to this device |
| 3709 | audio_devices_t device = AUDIO_DEVICE_OUT_HDMI; |
| 3710 | SortedVector<audio_io_handle_t> outputs; |
| 3711 | bool reportedFormatFound = false; |
| 3712 | status_t status; |
| 3713 | sp<SwAudioOutputDescriptor> desc; |
| 3714 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3715 | desc = mOutputs.valueAt(i); |
| 3716 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
| 3717 | outputs.add(mOutputs.keyAt(i)); |
| 3718 | } |
| 3719 | } |
| 3720 | // Open an output to query dynamic parameters. |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3721 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3722 | AUDIO_DEVICE_OUT_HDMI); |
| 3723 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3724 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3725 | for (const auto& hwModule : mHwModules) { |
| 3726 | for (size_t i = 0; i < hwModule->getOutputProfiles().size(); i++) { |
| 3727 | sp<IOProfile> profile = hwModule->getOutputProfiles()[i]; |
| 3728 | if (profile->supportDevice(AUDIO_DEVICE_OUT_HDMI) && |
| 3729 | profile->supportDeviceAddress(address)) { |
| 3730 | size_t j; |
| 3731 | for (j = 0; j < outputs.size(); j++) { |
| 3732 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
| 3733 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
| 3734 | break; |
| 3735 | } |
| 3736 | } |
| 3737 | if (j != outputs.size()) { |
| 3738 | status = getSupportedFormats(outputs.itemAt(j), formats); |
| 3739 | reportedFormatFound |= (status == NO_ERROR); |
| 3740 | continue; |
| 3741 | } |
| 3742 | |
| 3743 | if (!profile->canOpenNewIo()) { |
| 3744 | ALOGW("Max Output number %u already opened for this profile %s", |
| 3745 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 3746 | continue; |
| 3747 | } |
| 3748 | |
| 3749 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 3750 | device, address.string(), profile.get(), profile->getName().string()); |
| 3751 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
| 3752 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3753 | status_t status = desc->open(nullptr, device, address, |
| 3754 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, |
| 3755 | &output); |
| 3756 | |
| 3757 | if (status == NO_ERROR) { |
| 3758 | status = getSupportedFormats(output, formats); |
| 3759 | reportedFormatFound |= (status == NO_ERROR); |
| 3760 | desc->close(); |
| 3761 | output = AUDIO_IO_HANDLE_NONE; |
| 3762 | } |
| 3763 | } |
| 3764 | } |
| 3765 | } |
| 3766 | } |
| 3767 | |
| 3768 | if (!reportedFormatFound) { |
| 3769 | return UNKNOWN_ERROR; |
| 3770 | } |
| 3771 | } else { |
| 3772 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3773 | formats.add(SURROUND_FORMATS[i]); |
| 3774 | } |
| 3775 | } |
| 3776 | for (size_t i = 0; i < formats.size(); i++) { |
| 3777 | if (formatsWritten < formatsMax) { |
| 3778 | surroundFormats[formatsWritten] = formats[i]; |
| 3779 | bool formatEnabled = false; |
| 3780 | if (formats[i] == AUDIO_FORMAT_AAC_LC) { |
| 3781 | for (size_t j = 0; j < ARRAY_SIZE(AAC_FORMATS); j++) { |
| 3782 | formatEnabled = |
| 3783 | mSurroundFormats.find(AAC_FORMATS[i]) != mSurroundFormats.end(); |
| 3784 | break; |
| 3785 | } |
| 3786 | } else { |
| 3787 | formatEnabled = mSurroundFormats.find(formats[i]) != mSurroundFormats.end(); |
| 3788 | } |
| 3789 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3790 | } |
| 3791 | (*numSurroundFormats)++; |
| 3792 | } |
| 3793 | return NO_ERROR; |
| 3794 | } |
| 3795 | |
| 3796 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3797 | { |
| 3798 | // Check if audio format is a surround formats. |
| 3799 | bool isSurroundFormat = false; |
| 3800 | for (size_t i = 0; i < ARRAY_SIZE(SURROUND_FORMATS); i++) { |
| 3801 | if (audioFormat == SURROUND_FORMATS[i]) { |
| 3802 | isSurroundFormat = true; |
| 3803 | break; |
| 3804 | } |
| 3805 | } |
| 3806 | if (!isSurroundFormat) { |
| 3807 | return BAD_VALUE; |
| 3808 | } |
| 3809 | |
| 3810 | // Should only be called when MANUAL. |
| 3811 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3812 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3813 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 3814 | return INVALID_OPERATION; |
| 3815 | } |
| 3816 | |
| 3817 | if ((mSurroundFormats.find(audioFormat) != mSurroundFormats.end() && enabled) |
| 3818 | || (mSurroundFormats.find(audioFormat) == mSurroundFormats.end() && !enabled)) { |
| 3819 | return NO_ERROR; |
| 3820 | } |
| 3821 | |
| 3822 | // The operation is valid only when there is HDMI output available. |
| 3823 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3824 | return INVALID_OPERATION; |
| 3825 | } |
| 3826 | |
| 3827 | if (enabled) { |
| 3828 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3829 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3830 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3831 | } |
| 3832 | } else { |
| 3833 | mSurroundFormats.insert(audioFormat); |
| 3834 | } |
| 3835 | } else { |
| 3836 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3837 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3838 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3839 | } |
| 3840 | } else { |
| 3841 | mSurroundFormats.erase(audioFormat); |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3846 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3847 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3848 | AUDIO_DEVICE_OUT_HDMI); |
| 3849 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3850 | // Simulate reconnection to update enabled surround sound formats. |
| 3851 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3852 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3853 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3854 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3855 | address.c_str(), |
| 3856 | name.c_str()); |
| 3857 | if (status != NO_ERROR) { |
| 3858 | continue; |
| 3859 | } |
| 3860 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3861 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3862 | address.c_str(), |
| 3863 | name.c_str()); |
| 3864 | profileUpdated |= (status == NO_ERROR); |
| 3865 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3866 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3867 | AUDIO_DEVICE_IN_HDMI); |
| 3868 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3869 | // Simulate reconnection to update enabled surround sound formats. |
| 3870 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3871 | String8 name = hdmiInputDevices[i]->getName(); |
| 3872 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3873 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3874 | address.c_str(), |
| 3875 | name.c_str()); |
| 3876 | if (status != NO_ERROR) { |
| 3877 | continue; |
| 3878 | } |
| 3879 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3880 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3881 | address.c_str(), |
| 3882 | name.c_str()); |
| 3883 | profileUpdated |= (status == NO_ERROR); |
| 3884 | } |
| 3885 | |
| 3886 | // Undo the surround formats change due to no audio profiles updated. |
| 3887 | if (!profileUpdated) { |
| 3888 | if (enabled) { |
| 3889 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3890 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3891 | mSurroundFormats.erase(AAC_FORMATS[i]); |
| 3892 | } |
| 3893 | } else { |
| 3894 | mSurroundFormats.erase(audioFormat); |
| 3895 | } |
| 3896 | } else { |
| 3897 | if (audioFormat == AUDIO_FORMAT_AAC_LC) { |
| 3898 | for (size_t i = 0; i < ARRAY_SIZE(AAC_FORMATS); i++) { |
| 3899 | mSurroundFormats.insert(AAC_FORMATS[i]); |
| 3900 | } |
| 3901 | } else { |
| 3902 | mSurroundFormats.insert(audioFormat); |
| 3903 | } |
| 3904 | } |
| 3905 | } |
| 3906 | |
| 3907 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3908 | } |
| 3909 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3910 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) |
| 3911 | { |
| 3912 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3913 | |
| 3914 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3915 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3916 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 3917 | AudioSessionCollection activeSessions = activeDesc->getAudioSessions(true); |
| 3918 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 3919 | sp<AudioSession> activeSession = activeSessions.valueAt(j); |
| 3920 | if (activeSession->uid() == uid) { |
| 3921 | activeSession->setSilenced(silenced); |
| 3922 | } |
| 3923 | } |
| 3924 | } |
| 3925 | } |
| 3926 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3927 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3928 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3929 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3930 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3931 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3932 | if (patchDesc == 0) { |
| 3933 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3934 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3935 | return BAD_VALUE; |
| 3936 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3937 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3938 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3939 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3940 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3941 | if (swOutputDesc != 0) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3942 | status_t status = stopSource(swOutputDesc, stream, false); |
| 3943 | if (status == NO_ERROR) { |
| 3944 | swOutputDesc->stop(); |
| 3945 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3946 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3947 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3948 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3949 | if (hwOutputDesc != 0) { |
| 3950 | // release patch between src device and output device |
| 3951 | // close Hwoutput and remove from mHwOutputs |
| 3952 | } else { |
| 3953 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3954 | } |
| 3955 | } |
| 3956 | return NO_ERROR; |
| 3957 | } |
| 3958 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3959 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3960 | audio_io_handle_t output, routing_strategy strategy) |
| 3961 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3962 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3963 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3964 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3965 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3966 | routing_strategy sourceStrategy = |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3967 | (routing_strategy) getStrategyForAttr(&attributes); |
| 3968 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3969 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3970 | source = sourceDesc; |
| 3971 | break; |
| 3972 | } |
| 3973 | } |
| 3974 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3975 | } |
| 3976 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3977 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3978 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3979 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3980 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3981 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3982 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3983 | } |
| 3984 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3985 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3986 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3987 | static const char *kConfigLocationList[] = |
| 3988 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3989 | static const int kConfigLocationListSize = |
| 3990 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3991 | |
| 3992 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3993 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3994 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3995 | status_t ret; |
| 3996 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3997 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3998 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3999 | // A2DP offload supported but disabled: try to use special XML file |
| 4000 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 4001 | } |
| 4002 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 4003 | |
| 4004 | for (const char* fileName : fileNames) { |
| 4005 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 4006 | PolicySerializer serializer; |
| 4007 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 4008 | "%s/%s", kConfigLocationList[i], fileName); |
| 4009 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 4010 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 4011 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 4012 | return ret; |
| 4013 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | return ret; |
| 4017 | } |
| 4018 | #endif |
| 4019 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4020 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 4021 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4022 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 4023 | 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] | 4024 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4025 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4026 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4027 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 4028 | mVolumeCurves(new VolumeCurvesCollection()), |
| 4029 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 4030 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4031 | #else |
| 4032 | mVolumeCurves(new StreamDescriptorCollection()), |
| 4033 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
| 4034 | mDefaultOutputDevice), |
| 4035 | #endif |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4036 | mAudioPortGeneration(1), |
| 4037 | mBeaconMuteRefCount(0), |
| 4038 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4039 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4040 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4041 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 4042 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 4043 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4044 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4045 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4046 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4047 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 4048 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 4049 | { |
| 4050 | loadConfig(); |
| 4051 | initialize(); |
| 4052 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4053 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4054 | void AudioPolicyManager::loadConfig() { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4055 | #ifdef USE_XML_AUDIO_POLICY_CONF |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4056 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4057 | #else |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4058 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) |
| 4059 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4060 | #endif |
| 4061 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4062 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4063 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4064 | } |
| 4065 | |
| 4066 | status_t AudioPolicyManager::initialize() { |
| 4067 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4068 | |
| 4069 | // 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] | 4070 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 4071 | if (!engineInstance) { |
| 4072 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4073 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4074 | } |
| 4075 | // Retrieve the Policy Manager Interface |
| 4076 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 4077 | if (mEngine == NULL) { |
| 4078 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4079 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4080 | } |
| 4081 | mEngine->setObserver(this); |
| 4082 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4083 | if (status != NO_ERROR) { |
| 4084 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 4085 | return status; |
| 4086 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4087 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4088 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4090 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 4091 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4092 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4093 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4094 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 4095 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4096 | continue; |
| 4097 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4098 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4099 | // open all output streams needed to access attached devices |
| 4100 | // except for direct output streams that are only opened when they are actually |
| 4101 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4102 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4103 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4104 | if (!outProfile->canOpenNewIo()) { |
| 4105 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 4106 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 4107 | continue; |
| 4108 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4109 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4110 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4111 | continue; |
| 4112 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4113 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4114 | mTtsOutputAvailable = true; |
| 4115 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4116 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4117 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4118 | continue; |
| 4119 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4120 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4121 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 4122 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4123 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4124 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4125 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4126 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4127 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4128 | if ((profileType & outputDeviceTypes) == 0) { |
| 4129 | continue; |
| 4130 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4131 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 4132 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4133 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4134 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 4135 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4136 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 4137 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4138 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4139 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 4140 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4141 | |
| 4142 | if (status != NO_ERROR) { |
| 4143 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 4144 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4145 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4146 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4147 | for (const auto& dev : supportedDevices) { |
| 4148 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4149 | // 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] | 4150 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4151 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4155 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4156 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4157 | } |
| 4158 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4159 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4160 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4161 | true, |
| 4162 | 0, |
| 4163 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4164 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4165 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4166 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4167 | // open input streams needed to access attached devices to validate |
| 4168 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4169 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4170 | if (!inProfile->canOpenNewIo()) { |
| 4171 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4172 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4173 | continue; |
| 4174 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4175 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4176 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4177 | continue; |
| 4178 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4179 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4180 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4181 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4182 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4183 | if ((profileType & inputDeviceTypes) == 0) { |
| 4184 | continue; |
| 4185 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4186 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4187 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4188 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4189 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4190 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4191 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4192 | : String8(""); |
| 4193 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4194 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4195 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4196 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4197 | status_t status = inputDesc->open(nullptr, |
| 4198 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4199 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4200 | AUDIO_SOURCE_MIC, |
| 4201 | AUDIO_INPUT_FLAG_NONE, |
| 4202 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4203 | |
| 4204 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4205 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4206 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4207 | // 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] | 4208 | if (index >= 0) { |
| 4209 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4210 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4211 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4212 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4213 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4214 | } |
| 4215 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4216 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4217 | } else { |
| 4218 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4219 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4220 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4221 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4222 | } |
| 4223 | } |
| 4224 | // make sure all attached devices have been allocated a unique ID |
| 4225 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4226 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4227 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4228 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4229 | continue; |
| 4230 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4231 | // The device is now validated and can be appended to the available devices of the engine |
| 4232 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4233 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4234 | i++; |
| 4235 | } |
| 4236 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4237 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4238 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4239 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4240 | continue; |
| 4241 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4242 | // The device is now validated and can be appended to the available devices of the engine |
| 4243 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4244 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4245 | i++; |
| 4246 | } |
| 4247 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4248 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4249 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4250 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4251 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4252 | // If microphones address is empty, set it according to device type |
| 4253 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4254 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4255 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4256 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4257 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4258 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4259 | } |
| 4260 | } |
| 4261 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4262 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4263 | if (mPrimaryOutput == 0) { |
| 4264 | ALOGE("Failed to open primary output"); |
| 4265 | status = NO_INIT; |
| 4266 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4267 | |
| 4268 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4269 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4270 | } |
| 4271 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4272 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4273 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4274 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4275 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4276 | } |
| 4277 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4278 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4279 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4280 | mAvailableOutputDevices.clear(); |
| 4281 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4282 | mOutputs.clear(); |
| 4283 | mInputs.clear(); |
| 4284 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4285 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4286 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4287 | } |
| 4288 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4289 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4290 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4291 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4294 | // --- |
| 4295 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4296 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4297 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4298 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4299 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4300 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4301 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4302 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4303 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4304 | } |
| 4305 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4306 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4307 | { |
| 4308 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4309 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4310 | } |
| 4311 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4312 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4313 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4314 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4315 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4316 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4317 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4318 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4319 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4320 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4321 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4322 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4323 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4324 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4325 | if (devDesc != 0) { |
| 4326 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4327 | desc->mIoHandle, address.string()); |
| 4328 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4329 | } |
| 4330 | } |
| 4331 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4332 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4333 | audio_policy_dev_state_t state, |
| 4334 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4335 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4337 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4338 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4339 | |
| 4340 | if (audio_device_is_digital(device)) { |
| 4341 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4342 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4343 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4345 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4346 | // first list already open outputs that can be routed to this device |
| 4347 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4348 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4349 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4350 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4351 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4352 | outputs.add(mOutputs.keyAt(i)); |
| 4353 | } else { |
| 4354 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4355 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4356 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4357 | } |
| 4358 | } |
| 4359 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4360 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4361 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4362 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4363 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4364 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4365 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4366 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4367 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4368 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4369 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4370 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | } |
| 4372 | } |
| 4373 | } |
| 4374 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4375 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4376 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4377 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4378 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4379 | return BAD_VALUE; |
| 4380 | } |
| 4381 | |
| 4382 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4383 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4384 | 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] | 4385 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4386 | |
| 4387 | // nothing to do if one output is already opened for this profile |
| 4388 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4389 | for (j = 0; j < outputs.size(); j++) { |
| 4390 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4391 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4392 | // matching profile: save the sample rates, format and channel masks supported |
| 4393 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4394 | if (audio_device_is_digital(device)) { |
| 4395 | devDesc->importAudioPort(profile); |
| 4396 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4397 | break; |
| 4398 | } |
| 4399 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4400 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4401 | continue; |
| 4402 | } |
| 4403 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4404 | if (!profile->canOpenNewIo()) { |
| 4405 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4406 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4407 | continue; |
| 4408 | } |
| 4409 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4410 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4411 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4412 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4413 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4414 | status_t status = desc->open(nullptr, device, address, |
| 4415 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4416 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4417 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4418 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4419 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4420 | char *param = audio_device_address_to_parameter(device, address); |
| 4421 | mpClientInterface->setParameters(output, String8(param)); |
| 4422 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4423 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4424 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4425 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4426 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4427 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4428 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4429 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4430 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4431 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4432 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4433 | profile->pickAudioProfile( |
| 4434 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4435 | config.offload_info.sample_rate = config.sample_rate; |
| 4436 | config.offload_info.channel_mask = config.channel_mask; |
| 4437 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4438 | |
| 4439 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4440 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4441 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4442 | output = AUDIO_IO_HANDLE_NONE; |
| 4443 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4444 | } |
| 4445 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4446 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4447 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4448 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4449 | sp<AudioPolicyMix> policyMix; |
| 4450 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4451 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4452 | address.string()); |
| 4453 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4454 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4455 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4456 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4457 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4458 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4459 | // no duplicated output for direct outputs and |
| 4460 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4461 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4462 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4463 | //TODO: configure audio effect output stage here |
| 4464 | |
| 4465 | // 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] | 4466 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4467 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4468 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4469 | &duplicatedOutput); |
| 4470 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4471 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4472 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4473 | } else { |
| 4474 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4475 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4476 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4477 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4478 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4479 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4480 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4481 | } |
| 4482 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4483 | } else { |
| 4484 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4485 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4486 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4487 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4488 | profiles.removeAt(profile_index); |
| 4489 | profile_index--; |
| 4490 | } else { |
| 4491 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4492 | // Load digital format info only for digital devices |
| 4493 | if (audio_device_is_digital(device)) { |
| 4494 | devDesc->importAudioPort(profile); |
| 4495 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4496 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4497 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4498 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4499 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4500 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4501 | NULL/*patch handle*/, address.string()); |
| 4502 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4503 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4504 | } |
| 4505 | } |
| 4506 | |
| 4507 | if (profiles.isEmpty()) { |
| 4508 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4509 | return BAD_VALUE; |
| 4510 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4511 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4512 | // check if one opened output is not needed any more after disconnecting one device |
| 4513 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4514 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4515 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4516 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4517 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4518 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4519 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4520 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4521 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4522 | mOutputs.keyAt(i)); |
| 4523 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4524 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4525 | } |
| 4526 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4527 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4528 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4529 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4530 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4531 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4532 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4533 | "clearing direct output profile %zu on module %s", |
| 4534 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4535 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | } |
| 4539 | } |
| 4540 | return NO_ERROR; |
| 4541 | } |
| 4542 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4543 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4544 | audio_policy_dev_state_t state, |
| 4545 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4546 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4547 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4548 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4549 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4550 | |
| 4551 | if (audio_device_is_digital(device)) { |
| 4552 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4553 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4556 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4557 | // first list already open inputs that can be routed to this device |
| 4558 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4559 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4560 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4561 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4562 | inputs.add(mInputs.keyAt(input_index)); |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4567 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4568 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4569 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4570 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4571 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4572 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4573 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4574 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4575 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4576 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4577 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4578 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4579 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4580 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4581 | } |
| 4582 | } |
| 4583 | } |
| 4584 | |
| 4585 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4586 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4587 | return BAD_VALUE; |
| 4588 | } |
| 4589 | |
| 4590 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4591 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4592 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4593 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4594 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4595 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4596 | // nothing to do if one input is already opened for this profile |
| 4597 | size_t input_index; |
| 4598 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4599 | desc = mInputs.valueAt(input_index); |
| 4600 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4601 | if (audio_device_is_digital(device)) { |
| 4602 | devDesc->importAudioPort(profile); |
| 4603 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4604 | break; |
| 4605 | } |
| 4606 | } |
| 4607 | if (input_index != mInputs.size()) { |
| 4608 | continue; |
| 4609 | } |
| 4610 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4611 | if (!profile->canOpenNewIo()) { |
| 4612 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4613 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4614 | continue; |
| 4615 | } |
| 4616 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4617 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4618 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4619 | status_t status = desc->open(nullptr, |
| 4620 | device, |
| 4621 | address, |
| 4622 | AUDIO_SOURCE_MIC, |
| 4623 | AUDIO_INPUT_FLAG_NONE, |
| 4624 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4625 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4626 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4627 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4628 | char *param = audio_device_address_to_parameter(device, address); |
| 4629 | mpClientInterface->setParameters(input, String8(param)); |
| 4630 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4631 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4632 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4633 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4634 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4635 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4636 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | if (input != 0) { |
| 4640 | addInput(input, desc); |
| 4641 | } |
| 4642 | } // endif input != 0 |
| 4643 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4644 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4645 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4646 | profiles.removeAt(profile_index); |
| 4647 | profile_index--; |
| 4648 | } else { |
| 4649 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4650 | if (audio_device_is_digital(device)) { |
| 4651 | devDesc->importAudioPort(profile); |
| 4652 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4653 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4654 | } |
| 4655 | } // end scan profiles |
| 4656 | |
| 4657 | if (profiles.isEmpty()) { |
| 4658 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4659 | return BAD_VALUE; |
| 4660 | } |
| 4661 | } else { |
| 4662 | // Disconnect |
| 4663 | // check if one opened input is not needed any more after disconnecting one device |
| 4664 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4665 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4666 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4667 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4668 | mInputs.keyAt(input_index)); |
| 4669 | inputs.add(mInputs.keyAt(input_index)); |
| 4670 | } |
| 4671 | } |
| 4672 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4673 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4674 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4675 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4676 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4677 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4678 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4679 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4680 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4681 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4682 | } |
| 4683 | } |
| 4684 | } |
| 4685 | } // end disconnect |
| 4686 | |
| 4687 | return NO_ERROR; |
| 4688 | } |
| 4689 | |
| 4690 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4691 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4692 | { |
| 4693 | ALOGV("closeOutput(%d)", output); |
| 4694 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4695 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4696 | if (outputDesc == NULL) { |
| 4697 | ALOGW("closeOutput() unknown output %d", output); |
| 4698 | return; |
| 4699 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4700 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4701 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4702 | // look for duplicated outputs connected to the output being removed. |
| 4703 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4704 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4705 | if (dupOutputDesc->isDuplicated() && |
| 4706 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4707 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4708 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4709 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4710 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4711 | } else { |
| 4712 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4713 | } |
| 4714 | // As all active tracks on duplicated output will be deleted, |
| 4715 | // and as they were also referenced on the other output, the reference |
| 4716 | // count for their stream type must be adjusted accordingly on |
| 4717 | // the other output. |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4718 | bool wasActive = outputDesc2->isActive(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4719 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4720 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4721 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4722 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4723 | // stop() will be a no op if the output is still active but is needed in case all |
| 4724 | // active streams refcounts where cleared above |
| 4725 | if (wasActive) { |
| 4726 | outputDesc2->stop(); |
| 4727 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4729 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4730 | |
| 4731 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4732 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4733 | } |
| 4734 | } |
| 4735 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4736 | nextAudioPortGeneration(); |
| 4737 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4738 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4739 | if (index >= 0) { |
| 4740 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4741 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4742 | mAudioPatches.removeItemsAt(index); |
| 4743 | mpClientInterface->onAudioPatchListUpdate(); |
| 4744 | } |
| 4745 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4746 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4747 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4748 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4749 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
| 4752 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4753 | { |
| 4754 | ALOGV("closeInput(%d)", input); |
| 4755 | |
| 4756 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4757 | if (inputDesc == NULL) { |
| 4758 | ALOGW("closeInput() unknown input %d", input); |
| 4759 | return; |
| 4760 | } |
| 4761 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4762 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4763 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame^] | 4764 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4765 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4766 | if (index >= 0) { |
| 4767 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4768 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4769 | mAudioPatches.removeItemsAt(index); |
| 4770 | mpClientInterface->onAudioPatchListUpdate(); |
| 4771 | } |
| 4772 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4773 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4774 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame^] | 4775 | |
| 4776 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4777 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4778 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4779 | SoundTrigger::setCaptureState(false); |
| 4780 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4781 | } |
| 4782 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4783 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4784 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4785 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4786 | { |
| 4787 | SortedVector<audio_io_handle_t> outputs; |
| 4788 | |
| 4789 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4790 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4791 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4792 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4793 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4794 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4795 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4796 | outputs.add(openOutputs.keyAt(i)); |
| 4797 | } |
| 4798 | } |
| 4799 | return outputs; |
| 4800 | } |
| 4801 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4802 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4803 | { |
| 4804 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4805 | // output is suspended before any tracks are moved to it |
| 4806 | checkA2dpSuspend(); |
| 4807 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4808 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4809 | updateDevicesAndOutputs(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4810 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
| 4811 | setMsdPatch(); |
| 4812 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4813 | } |
| 4814 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4815 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4816 | { |
| 4817 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4818 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4819 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4820 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4821 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4822 | // also take into account external policy-related changes: add all outputs which are |
| 4823 | // associated with policies in the "before" and "after" output vectors |
| 4824 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4825 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4826 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4827 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4828 | srcOutputs.add(desc->mIoHandle); |
| 4829 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4830 | } |
| 4831 | } |
| 4832 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4833 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4834 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4835 | dstOutputs.add(desc->mIoHandle); |
| 4836 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4837 | } |
| 4838 | } |
| 4839 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4840 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4841 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4842 | // audio from invalidated tracks will be rendered when unmuting |
| 4843 | uint32_t maxLatency = 0; |
| 4844 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4845 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4846 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4847 | maxLatency = desc->latency(); |
| 4848 | } |
| 4849 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4850 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4851 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4852 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4853 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4854 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4855 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4856 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4857 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4858 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4859 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4860 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4861 | if (source != 0){ |
| 4862 | connectAudioSource(source); |
| 4863 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | } |
| 4865 | |
| 4866 | // Move effects associated to this strategy from previous output to new output |
| 4867 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4868 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4869 | } |
| 4870 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4871 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4872 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4873 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | } |
| 4877 | } |
| 4878 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4879 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4880 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4881 | 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] | 4882 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4883 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4884 | 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] | 4885 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4886 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4887 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4888 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4889 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4890 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4891 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4892 | } |
| 4893 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4894 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4895 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4896 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4897 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4898 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4899 | return; |
| 4900 | } |
| 4901 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4902 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4903 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4904 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4905 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4906 | |
| 4907 | // if suspended, restore A2DP output if: |
| 4908 | // ((SCO device is NOT connected) || |
| 4909 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4910 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4911 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4912 | // if not suspended, suspend A2DP output if: |
| 4913 | // (SCO device is connected) && |
| 4914 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4915 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4916 | // |
| 4917 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4918 | if (!isScoConnected || |
| 4919 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4920 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4921 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4922 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4923 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4924 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4925 | |
| 4926 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4927 | mA2dpSuspended = false; |
| 4928 | } |
| 4929 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4930 | if (isScoConnected && |
| 4931 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4932 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4933 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4934 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4935 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4936 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4937 | |
| 4938 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4939 | mA2dpSuspended = true; |
| 4940 | } |
| 4941 | } |
| 4942 | } |
| 4943 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4944 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4945 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4946 | { |
| 4947 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4948 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4949 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4950 | if (index >= 0) { |
| 4951 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4952 | if (patchDesc->mUid != mUidCached) { |
| 4953 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4954 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4955 | return outputDesc->device(); |
| 4956 | } |
| 4957 | } |
| 4958 | |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4959 | // Check if an explicit routing request exists for an active stream on this output and |
| 4960 | // use it in priority before any other rule |
| 4961 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
| 4962 | if (outputDesc->isStreamActive((audio_stream_type_t)stream)) { |
| 4963 | audio_devices_t forcedDevice = |
| 4964 | mOutputRoutes.getActiveDeviceForStream( |
| 4965 | (audio_stream_type_t)stream, mAvailableOutputDevices); |
| 4966 | |
| 4967 | if (forcedDevice != AUDIO_DEVICE_NONE) { |
| 4968 | return forcedDevice; |
| 4969 | } |
| 4970 | } |
| 4971 | } |
| 4972 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4973 | // 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] | 4974 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4975 | // use device for strategy enforced audible |
| 4976 | // 2: we are in call or the strategy phone is active on the output: |
| 4977 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4978 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4979 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4980 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4981 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4982 | // 5: the strategy accessibility is active on the output: |
| 4983 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4984 | // 6: the strategy "respectful" sonification is active on the output: |
| 4985 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4986 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4987 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4988 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4989 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4990 | // 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] | 4991 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4992 | |
| 4993 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4994 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4995 | // as opposed to all streams on the same audio HAL module. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4996 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4997 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4998 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4999 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5000 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5001 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5002 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5003 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 5004 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 5005 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5006 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 5007 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5008 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5009 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5010 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5011 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5012 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5013 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5014 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5015 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5016 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5017 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5018 | } |
| 5019 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5020 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 5021 | return device; |
| 5022 | } |
| 5023 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5024 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5025 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5026 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5027 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5028 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5029 | if (index >= 0) { |
| 5030 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5031 | if (patchDesc->mUid != mUidCached) { |
| 5032 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5033 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5034 | return inputDesc->mDevice; |
| 5035 | } |
| 5036 | } |
| 5037 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5038 | // If we are not in call and no client is active on this input, this methods returns |
| 5039 | // 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] | 5040 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5041 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 5042 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 5043 | } |
| 5044 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5045 | device = getDeviceAndMixForInputSource(source); |
| 5046 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5047 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5048 | return device; |
| 5049 | } |
| 5050 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5051 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 5052 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 5053 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5054 | } |
| 5055 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5056 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5057 | return (uint32_t)getStrategy(stream); |
| 5058 | } |
| 5059 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5060 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5061 | // By checking the range of stream before calling getStrategy, we avoid |
| 5062 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 5063 | // 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] | 5064 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5065 | return AUDIO_DEVICE_NONE; |
| 5066 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5067 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5068 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 5069 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5070 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5071 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5072 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5073 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 5074 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5075 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 5076 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5077 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5078 | curDevices |= outputDesc->device(); |
| 5079 | } |
| 5080 | } |
| 5081 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5082 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5083 | |
| 5084 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 5085 | and doesn't really need to.*/ |
| 5086 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 5087 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 5088 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 5089 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5090 | return devices; |
| 5091 | } |
| 5092 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5093 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 5094 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5095 | 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] | 5096 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5097 | } |
| 5098 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5099 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 5100 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5101 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5102 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 5103 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5104 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5105 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 5106 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5107 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5108 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5109 | } |
| 5110 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5111 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5112 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5113 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5114 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 5115 | updateDevicesAndOutputs(); |
| 5116 | break; |
| 5117 | default: |
| 5118 | break; |
| 5119 | } |
| 5120 | } |
| 5121 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5122 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5123 | |
| 5124 | // skip beacon mute management if a dedicated TTS output is available |
| 5125 | if (mTtsOutputAvailable) { |
| 5126 | return 0; |
| 5127 | } |
| 5128 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5129 | switch(event) { |
| 5130 | case STARTING_OUTPUT: |
| 5131 | mBeaconMuteRefCount++; |
| 5132 | break; |
| 5133 | case STOPPING_OUTPUT: |
| 5134 | if (mBeaconMuteRefCount > 0) { |
| 5135 | mBeaconMuteRefCount--; |
| 5136 | } |
| 5137 | break; |
| 5138 | case STARTING_BEACON: |
| 5139 | mBeaconPlayingRefCount++; |
| 5140 | break; |
| 5141 | case STOPPING_BEACON: |
| 5142 | if (mBeaconPlayingRefCount > 0) { |
| 5143 | mBeaconPlayingRefCount--; |
| 5144 | } |
| 5145 | break; |
| 5146 | } |
| 5147 | |
| 5148 | if (mBeaconMuteRefCount > 0) { |
| 5149 | // any playback causes beacon to be muted |
| 5150 | return setBeaconMute(true); |
| 5151 | } else { |
| 5152 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5153 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5154 | } |
| 5155 | } |
| 5156 | |
| 5157 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5158 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5159 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5160 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5161 | if (mBeaconMuted != mute) { |
| 5162 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5163 | ALOGV("\t muting %d", mute); |
| 5164 | uint32_t maxLatency = 0; |
| 5165 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5166 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5167 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5168 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5169 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5170 | const uint32_t latency = desc->latency() * 2; |
| 5171 | if (latency > maxLatency) { |
| 5172 | maxLatency = latency; |
| 5173 | } |
| 5174 | } |
| 5175 | mBeaconMuted = mute; |
| 5176 | return maxLatency; |
| 5177 | } |
| 5178 | return 0; |
| 5179 | } |
| 5180 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5181 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5182 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5183 | { |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 5184 | // Check if an explicit routing request exists for a stream type corresponding to the |
| 5185 | // specified strategy and use it in priority over default routing rules. |
| 5186 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
| 5187 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 5188 | audio_devices_t forcedDevice = |
| 5189 | mOutputRoutes.getActiveDeviceForStream( |
| 5190 | (audio_stream_type_t)stream, mAvailableOutputDevices); |
| 5191 | if (forcedDevice != AUDIO_DEVICE_NONE) { |
| 5192 | return forcedDevice; |
| 5193 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5194 | } |
| 5195 | } |
| 5196 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5197 | if (fromCache) { |
| 5198 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5199 | strategy, mDeviceForStrategy[strategy]); |
| 5200 | return mDeviceForStrategy[strategy]; |
| 5201 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5202 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5203 | } |
| 5204 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5205 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5206 | { |
| 5207 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5208 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5209 | } |
| 5210 | mPreviousOutputs = mOutputs; |
| 5211 | } |
| 5212 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5213 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5214 | audio_devices_t prevDevice, |
| 5215 | uint32_t delayMs) |
| 5216 | { |
| 5217 | // mute/unmute strategies using an incompatible device combination |
| 5218 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5219 | // if unmuting, unmute only after the specified delay |
| 5220 | if (outputDesc->isDuplicated()) { |
| 5221 | return 0; |
| 5222 | } |
| 5223 | |
| 5224 | uint32_t muteWaitMs = 0; |
| 5225 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5226 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5227 | |
| 5228 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5229 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5230 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5231 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5232 | bool doMute = false; |
| 5233 | |
| 5234 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5235 | doMute = true; |
| 5236 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5237 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5238 | doMute = true; |
| 5239 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5240 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5241 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5242 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5243 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5244 | // skip output if it does not share any device with current output |
| 5245 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5246 | == AUDIO_DEVICE_NONE) { |
| 5247 | continue; |
| 5248 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5249 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5250 | mute ? "muting" : "unmuting", i, curDevice); |
| 5251 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5252 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5253 | if (mute) { |
| 5254 | // FIXME: should not need to double latency if volume could be applied |
| 5255 | // immediately by the audioflinger mixer. We must account for the delay |
| 5256 | // between now and the next time the audioflinger thread for this output |
| 5257 | // will process a buffer (which corresponds to one buffer size, |
| 5258 | // usually 1/2 or 1/4 of the latency). |
| 5259 | if (muteWaitMs < desc->latency() * 2) { |
| 5260 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5261 | } |
| 5262 | } |
| 5263 | } |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5268 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5269 | // different per device volumes |
| 5270 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5271 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5272 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5273 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5274 | if (muteWaitMs < tempMuteWaitMs) { |
| 5275 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5276 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5277 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5278 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5279 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5280 | // make sure that we do not start the temporary mute period too early in case of |
| 5281 | // delayed device change |
| 5282 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5283 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5284 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5285 | } |
| 5286 | } |
| 5287 | } |
| 5288 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5289 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5290 | if (muteWaitMs > delayMs) { |
| 5291 | muteWaitMs -= delayMs; |
| 5292 | usleep(muteWaitMs * 1000); |
| 5293 | return muteWaitMs; |
| 5294 | } |
| 5295 | return 0; |
| 5296 | } |
| 5297 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5298 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5299 | audio_devices_t device, |
| 5300 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5301 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5302 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5303 | const char *address, |
| 5304 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5305 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5306 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5307 | AudioParameter param; |
| 5308 | uint32_t muteWaitMs; |
| 5309 | |
| 5310 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5311 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5312 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5313 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5314 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5315 | return muteWaitMs; |
| 5316 | } |
| 5317 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5318 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5319 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5320 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5321 | return 0; |
| 5322 | } |
| 5323 | |
| 5324 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5325 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5326 | |
| 5327 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5328 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5329 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5330 | |
| 5331 | if (device != AUDIO_DEVICE_NONE) { |
| 5332 | outputDesc->mDevice = device; |
| 5333 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5334 | |
| 5335 | // if the outputs are not materially active, there is no need to mute. |
| 5336 | if (requiresMuteCheck) { |
| 5337 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5338 | } else { |
| 5339 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5340 | muteWaitMs = 0; |
| 5341 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5342 | |
| 5343 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5344 | // the requested device is AUDIO_DEVICE_NONE |
| 5345 | // OR the requested device is the same as current device |
| 5346 | // AND force is not specified |
| 5347 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5348 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5349 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5350 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5351 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5352 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5353 | return muteWaitMs; |
| 5354 | } |
| 5355 | |
| 5356 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5357 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5358 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5359 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5360 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5361 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5362 | DeviceVector deviceList; |
| 5363 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5364 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5365 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5366 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5367 | device, String8(address)); |
| 5368 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5369 | } |
| 5370 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5371 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5372 | PatchBuilder patchBuilder; |
| 5373 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5374 | 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] | 5375 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5376 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5377 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5378 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5379 | |
| 5380 | // inform all input as well |
| 5381 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5382 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5383 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5384 | AudioParameter inputCmd = AudioParameter(); |
| 5385 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5386 | inputDescriptor->mIoHandle, device); |
| 5387 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5388 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5389 | inputCmd.toString(), |
| 5390 | delayMs); |
| 5391 | } |
| 5392 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5393 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5394 | |
| 5395 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5396 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5397 | |
| 5398 | return muteWaitMs; |
| 5399 | } |
| 5400 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5401 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5402 | int delayMs, |
| 5403 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5404 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5405 | ssize_t index; |
| 5406 | if (patchHandle) { |
| 5407 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5408 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5409 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5410 | } |
| 5411 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5412 | return INVALID_OPERATION; |
| 5413 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5414 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5415 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5416 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5417 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5418 | removeAudioPatch(patchDesc->mHandle); |
| 5419 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5420 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5421 | return status; |
| 5422 | } |
| 5423 | |
| 5424 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5425 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5426 | bool force, |
| 5427 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5428 | { |
| 5429 | status_t status = NO_ERROR; |
| 5430 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5431 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5432 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5433 | inputDesc->mDevice = device; |
| 5434 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5435 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5436 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5437 | PatchBuilder patchBuilder; |
| 5438 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5439 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5440 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5441 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5442 | auto result = usecase; |
| 5443 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5444 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5445 | } |
| 5446 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5447 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5448 | addSource(deviceList.itemAt(0)); |
| 5449 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5450 | } |
| 5451 | } |
| 5452 | return status; |
| 5453 | } |
| 5454 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5455 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5456 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5457 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5458 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5459 | ssize_t index; |
| 5460 | if (patchHandle) { |
| 5461 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5462 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5463 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5464 | } |
| 5465 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5466 | return INVALID_OPERATION; |
| 5467 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5468 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5469 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5470 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5471 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5472 | removeAudioPatch(patchDesc->mHandle); |
| 5473 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5474 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5475 | return status; |
| 5476 | } |
| 5477 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5478 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5479 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5480 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5481 | audio_format_t& format, |
| 5482 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5483 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5484 | { |
| 5485 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5486 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5487 | // |
| 5488 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5489 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5490 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5491 | sp<IOProfile> firstInexact; |
| 5492 | uint32_t updatedSamplingRate = 0; |
| 5493 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5494 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5495 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5496 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5497 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5498 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5499 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5500 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5501 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5502 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5503 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5504 | &channelMask /*updatedChannelMask*/, |
| 5505 | // FIXME ugly cast |
| 5506 | (audio_output_flags_t) flags, |
| 5507 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5508 | return profile; |
| 5509 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5510 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5511 | samplingRate, |
| 5512 | &updatedSamplingRate, |
| 5513 | format, |
| 5514 | &updatedFormat, |
| 5515 | channelMask, |
| 5516 | &updatedChannelMask, |
| 5517 | // FIXME ugly cast |
| 5518 | (audio_output_flags_t) flags, |
| 5519 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5520 | firstInexact = profile; |
| 5521 | } |
| 5522 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5523 | } |
| 5524 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5525 | if (firstInexact != nullptr) { |
| 5526 | samplingRate = updatedSamplingRate; |
| 5527 | format = updatedFormat; |
| 5528 | channelMask = updatedChannelMask; |
| 5529 | return firstInexact; |
| 5530 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5531 | return NULL; |
| 5532 | } |
| 5533 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5534 | |
| 5535 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5536 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5537 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5538 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5539 | audio_devices_t selectedDeviceFromMix = |
| 5540 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5541 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5542 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5543 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5544 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5545 | return getDeviceForInputSource(inputSource); |
| 5546 | } |
| 5547 | |
| 5548 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5549 | { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5550 | // Routing |
| 5551 | // Scan the whole RouteMap to see if we have an explicit route: |
| 5552 | // if the input source in the RouteMap is the same as the argument above, |
| 5553 | // and activity count is non-zero and the device in the route descriptor is available |
| 5554 | // then select this device. |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5555 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5556 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 2157f5b | 2018-04-25 18:33:02 -0700 | [diff] [blame] | 5557 | if ((inputSource == route->mSource) && route->isActiveOrChanged() && |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5558 | (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5559 | return route->mDeviceDescriptor->type(); |
| 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5564 | } |
| 5565 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5566 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5567 | int index, |
| 5568 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5569 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5570 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5571 | |
| 5572 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5573 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5574 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5575 | // the ringtone volume |
| 5576 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5577 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5578 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5579 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5580 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5581 | } |
| 5582 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5583 | // in-call: always cap volume by voice volume + some low headroom |
| 5584 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5585 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5586 | switch (stream) { |
| 5587 | case AUDIO_STREAM_SYSTEM: |
| 5588 | case AUDIO_STREAM_RING: |
| 5589 | case AUDIO_STREAM_MUSIC: |
| 5590 | case AUDIO_STREAM_ALARM: |
| 5591 | case AUDIO_STREAM_NOTIFICATION: |
| 5592 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5593 | case AUDIO_STREAM_DTMF: |
| 5594 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5595 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5596 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5597 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5598 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5599 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5600 | if (volumeDB > maxVoiceVolDb) { |
| 5601 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5602 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5603 | volumeDB = maxVoiceVolDb; |
| 5604 | } |
| 5605 | } break; |
| 5606 | default: |
| 5607 | break; |
| 5608 | } |
| 5609 | } |
| 5610 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5611 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5612 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5613 | // - always attenuate notifications volume by 6dB |
| 5614 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5615 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5616 | // - if music is playing, always limit the volume to current music volume, |
| 5617 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5618 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5619 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5620 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5621 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5622 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5623 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5624 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5625 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5626 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5627 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5628 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5629 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5630 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5631 | // when the phone is ringing we must consider that music could have been paused just before |
| 5632 | // by the music application and behave as if music was active if the last music track was |
| 5633 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5634 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5635 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5636 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5637 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5638 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5639 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5640 | musicDevice), |
| 5641 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5642 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5643 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5644 | if (volumeDB > minVolDB) { |
| 5645 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5646 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5648 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5649 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5650 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5651 | // intended to be played |
| 5652 | if ((volumeDB > -96.0f) && |
| 5653 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5654 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5655 | stream, device, |
| 5656 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5657 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5658 | } |
| 5659 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5660 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5661 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5662 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5663 | } |
| 5664 | } |
| 5665 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5666 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5667 | } |
| 5668 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5669 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5670 | audio_stream_type_t srcStream, |
| 5671 | audio_stream_type_t dstStream) |
| 5672 | { |
| 5673 | if (srcStream == dstStream) { |
| 5674 | return srcIndex; |
| 5675 | } |
| 5676 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5677 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5678 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5679 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5680 | |
| 5681 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5682 | } |
| 5683 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5684 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5685 | int index, |
| 5686 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5687 | audio_devices_t device, |
| 5688 | int delayMs, |
| 5689 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5690 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5691 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5692 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5693 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5694 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5695 | return NO_ERROR; |
| 5696 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5697 | audio_policy_forced_cfg_t forceUseForComm = |
| 5698 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5699 | // 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] | 5700 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5701 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5702 | 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] | 5703 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5704 | return INVALID_OPERATION; |
| 5705 | } |
| 5706 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5707 | if (device == AUDIO_DEVICE_NONE) { |
| 5708 | device = outputDesc->device(); |
| 5709 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5710 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5711 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5712 | if (outputDesc->isFixedVolume(device) || |
| 5713 | // Force VoIP volume to max for bluetooth SCO |
| 5714 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5715 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5716 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5717 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5718 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5719 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5720 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5721 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5722 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5723 | float voiceVolume; |
| 5724 | // 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] | 5725 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5726 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5727 | } else { |
| 5728 | voiceVolume = 1.0; |
| 5729 | } |
| 5730 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5731 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5732 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5733 | mLastVoiceVolume = voiceVolume; |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | return NO_ERROR; |
| 5738 | } |
| 5739 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5740 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5741 | audio_devices_t device, |
| 5742 | int delayMs, |
| 5743 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5744 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5745 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5746 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5747 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5748 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5749 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5750 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5751 | device, |
| 5752 | delayMs, |
| 5753 | force); |
| 5754 | } |
| 5755 | } |
| 5756 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5757 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5758 | bool on, |
| 5759 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5760 | int delayMs, |
| 5761 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5762 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5763 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5764 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5765 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5766 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5767 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5768 | } |
| 5769 | } |
| 5770 | } |
| 5771 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5772 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5773 | bool on, |
| 5774 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5775 | int delayMs, |
| 5776 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5777 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5778 | if (device == AUDIO_DEVICE_NONE) { |
| 5779 | device = outputDesc->device(); |
| 5780 | } |
| 5781 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5782 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5783 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5784 | |
| 5785 | if (on) { |
| 5786 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5787 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5788 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5789 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5790 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5791 | } |
| 5792 | } |
| 5793 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5794 | outputDesc->mMuteCount[stream]++; |
| 5795 | } else { |
| 5796 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5797 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5798 | return; |
| 5799 | } |
| 5800 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5801 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5802 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5803 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5804 | device, |
| 5805 | delayMs); |
| 5806 | } |
| 5807 | } |
| 5808 | } |
| 5809 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5810 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5811 | { |
| 5812 | // flags to stream type mapping |
| 5813 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5814 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5815 | } |
| 5816 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5817 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5818 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5819 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5820 | return AUDIO_STREAM_TTS; |
| 5821 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5822 | |
| 5823 | // usage to stream type mapping |
| 5824 | switch (attr->usage) { |
| 5825 | case AUDIO_USAGE_MEDIA: |
| 5826 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5827 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5828 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5829 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5830 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5831 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5832 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5833 | return AUDIO_STREAM_SYSTEM; |
| 5834 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5835 | return AUDIO_STREAM_VOICE_CALL; |
| 5836 | |
| 5837 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5838 | return AUDIO_STREAM_DTMF; |
| 5839 | |
| 5840 | case AUDIO_USAGE_ALARM: |
| 5841 | return AUDIO_STREAM_ALARM; |
| 5842 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5843 | return AUDIO_STREAM_RING; |
| 5844 | |
| 5845 | case AUDIO_USAGE_NOTIFICATION: |
| 5846 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5847 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5848 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5849 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5850 | return AUDIO_STREAM_NOTIFICATION; |
| 5851 | |
| 5852 | case AUDIO_USAGE_UNKNOWN: |
| 5853 | default: |
| 5854 | return AUDIO_STREAM_MUSIC; |
| 5855 | } |
| 5856 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5857 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5858 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5859 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5860 | // has flags that map to a strategy? |
| 5861 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5862 | return true; |
| 5863 | } |
| 5864 | |
| 5865 | // has known usage? |
| 5866 | switch (paa->usage) { |
| 5867 | case AUDIO_USAGE_UNKNOWN: |
| 5868 | case AUDIO_USAGE_MEDIA: |
| 5869 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5870 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5871 | case AUDIO_USAGE_ALARM: |
| 5872 | case AUDIO_USAGE_NOTIFICATION: |
| 5873 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5874 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5875 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5876 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5877 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5878 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5879 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5880 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5881 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5882 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5883 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5884 | break; |
| 5885 | default: |
| 5886 | return false; |
| 5887 | } |
| 5888 | return true; |
| 5889 | } |
| 5890 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5891 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5892 | routing_strategy strategy, uint32_t inPastMs, |
| 5893 | nsecs_t sysTime) const |
| 5894 | { |
| 5895 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5896 | sysTime = systemTime(); |
| 5897 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5898 | 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] | 5899 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5900 | (NUM_STRATEGIES == strategy)) && |
| 5901 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5902 | return true; |
| 5903 | } |
| 5904 | } |
| 5905 | return false; |
| 5906 | } |
| 5907 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5908 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5909 | routing_strategy strategy, uint32_t inPastMs, |
| 5910 | nsecs_t sysTime) const |
| 5911 | { |
| 5912 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5913 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5914 | if (outputDesc->sharesHwModuleWith(desc) |
| 5915 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5916 | return true; |
| 5917 | } |
| 5918 | } |
| 5919 | return false; |
| 5920 | } |
| 5921 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5922 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5923 | { |
| 5924 | return mEngine->getForceUse(usage); |
| 5925 | } |
| 5926 | |
| 5927 | bool AudioPolicyManager::isInCall() |
| 5928 | { |
| 5929 | return isStateInCall(mEngine->getPhoneState()); |
| 5930 | } |
| 5931 | |
| 5932 | bool AudioPolicyManager::isStateInCall(int state) |
| 5933 | { |
| 5934 | return is_state_in_call(state); |
| 5935 | } |
| 5936 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5937 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5938 | { |
| 5939 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5940 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5941 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5942 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5943 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5944 | } |
| 5945 | } |
| 5946 | |
| 5947 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5948 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5949 | bool release = false; |
| 5950 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5951 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5952 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5953 | source->ext.device.type == deviceDesc->type()) { |
| 5954 | release = true; |
| 5955 | } |
| 5956 | } |
| 5957 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5958 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5959 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5960 | sink->ext.device.type == deviceDesc->type()) { |
| 5961 | release = true; |
| 5962 | } |
| 5963 | } |
| 5964 | if (release) { |
| 5965 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5966 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5967 | } |
| 5968 | } |
| 5969 | } |
| 5970 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5971 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5972 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5973 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5974 | // TODO Set this based on Config properties. |
| 5975 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5976 | |
| 5977 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5978 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5979 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5980 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5981 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5982 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5983 | formats.clear(); |
| 5984 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5985 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5986 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5987 | // Always enable IEC61937 when in MANUAL mode. |
| 5988 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5989 | } else { // NEVER, AUTO or ALWAYS |
| 5990 | // Analyze original support for various formats. |
| 5991 | bool supportsAC3 = false; |
| 5992 | bool supportsOtherSurround = false; |
| 5993 | bool supportsIEC61937 = false; |
| 5994 | mSurroundFormats.clear(); |
| 5995 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5996 | audio_format_t format = formats[formatIndex]; |
| 5997 | switch (format) { |
| 5998 | case AUDIO_FORMAT_AC3: |
| 5999 | supportsAC3 = true; |
| 6000 | break; |
| 6001 | case AUDIO_FORMAT_E_AC3: |
| 6002 | case AUDIO_FORMAT_DTS: |
| 6003 | case AUDIO_FORMAT_DTS_HD: |
| 6004 | // If ALWAYS, remove all other surround formats here |
| 6005 | // since we will add them later. |
| 6006 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 6007 | formats.removeAt(formatIndex); |
| 6008 | formatIndex--; |
| 6009 | } |
| 6010 | supportsOtherSurround = true; |
| 6011 | break; |
| 6012 | case AUDIO_FORMAT_IEC61937: |
| 6013 | supportsIEC61937 = true; |
| 6014 | break; |
| 6015 | default: |
| 6016 | break; |
| 6017 | } |
| 6018 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6019 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6020 | // Modify formats based on surround preferences. |
| 6021 | // If NEVER, remove support for surround formats. |
| 6022 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 6023 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 6024 | // Remove surround sound related formats. |
| 6025 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 6026 | audio_format_t format = formats[formatIndex]; |
| 6027 | switch(format) { |
| 6028 | case AUDIO_FORMAT_AC3: |
| 6029 | case AUDIO_FORMAT_E_AC3: |
| 6030 | case AUDIO_FORMAT_DTS: |
| 6031 | case AUDIO_FORMAT_DTS_HD: |
| 6032 | case AUDIO_FORMAT_IEC61937: |
| 6033 | formats.removeAt(formatIndex); |
| 6034 | break; |
| 6035 | default: |
| 6036 | formatIndex++; // keep it |
| 6037 | break; |
| 6038 | } |
| 6039 | } |
| 6040 | supportsAC3 = false; |
| 6041 | supportsOtherSurround = false; |
| 6042 | supportsIEC61937 = false; |
| 6043 | } |
| 6044 | } else { // AUTO or ALWAYS |
| 6045 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 6046 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 6047 | && !supportsAC3) { |
| 6048 | formats.add(AUDIO_FORMAT_AC3); |
| 6049 | supportsAC3 = true; |
| 6050 | } |
| 6051 | |
| 6052 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 6053 | // This assumes that if any of these formats are reported by the HAL |
| 6054 | // then the report is valid and should not be modified. |
| 6055 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 6056 | formats.add(AUDIO_FORMAT_E_AC3); |
| 6057 | formats.add(AUDIO_FORMAT_DTS); |
| 6058 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 6059 | supportsOtherSurround = true; |
| 6060 | } |
| 6061 | |
| 6062 | // Add support for IEC61937 if any raw surround supported. |
| 6063 | // The HAL could do this but add it here, just in case. |
| 6064 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 6065 | formats.add(AUDIO_FORMAT_IEC61937); |
| 6066 | supportsIEC61937 = true; |
| 6067 | } |
| 6068 | |
| 6069 | // Add reported surround sound formats to enabled surround formats. |
| 6070 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6071 | audio_format_t format = formats[formatIndex]; |
| 6072 | switch(format) { |
| 6073 | case AUDIO_FORMAT_AC3: |
| 6074 | case AUDIO_FORMAT_E_AC3: |
| 6075 | case AUDIO_FORMAT_DTS: |
| 6076 | case AUDIO_FORMAT_DTS_HD: |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6077 | case AUDIO_FORMAT_AAC_LC: |
| 6078 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 6079 | case AUDIO_FORMAT_E_AC3_JOC: |
| 6080 | mSurroundFormats.insert(format); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6081 | default: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6082 | break; |
| 6083 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6084 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 6085 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6086 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | // Modify the list of channel masks supported. |
| 6090 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 6091 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 6092 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 6093 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 6094 | |
| 6095 | // If NEVER, then remove support for channelMasks > stereo. |
| 6096 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 6097 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 6098 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 6099 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 6100 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 6101 | channelMasks.removeAt(maskIndex); |
| 6102 | } else { |
| 6103 | maskIndex++; |
| 6104 | } |
| 6105 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6106 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 6107 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 6108 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6109 | bool supports5dot1 = false; |
| 6110 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6111 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6112 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 6113 | supports5dot1 = true; |
| 6114 | break; |
| 6115 | } |
| 6116 | } |
| 6117 | // If not then add 5.1 support. |
| 6118 | if (!supports5dot1) { |
| 6119 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 6120 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 6121 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6122 | } |
| 6123 | } |
| 6124 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6125 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 6126 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6127 | AudioProfileVector &profiles) |
| 6128 | { |
| 6129 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6130 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6131 | // Format MUST be checked first to update the list of AudioProfile |
| 6132 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6133 | reply = mpClientInterface->getParameters( |
| 6134 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 6135 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6136 | AudioParameter repliedParameters(reply); |
| 6137 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6138 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6139 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 6140 | return; |
| 6141 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6142 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6143 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6144 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 6145 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 6146 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6147 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6148 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6149 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6150 | ChannelsVector channelMasks; |
| 6151 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6152 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6153 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6154 | |
| 6155 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6156 | reply = mpClientInterface->getParameters( |
| 6157 | ioHandle, |
| 6158 | requestedParameters.toString() + ";" + |
| 6159 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6160 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6161 | AudioParameter repliedParameters(reply); |
| 6162 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6163 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6164 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6165 | } |
| 6166 | } |
| 6167 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6168 | reply = mpClientInterface->getParameters(ioHandle, |
| 6169 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6170 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6171 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6172 | AudioParameter repliedParameters(reply); |
| 6173 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6174 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6175 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6176 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6177 | filterSurroundChannelMasks(&channelMasks); |
| 6178 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6179 | } |
| 6180 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6181 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6182 | } |
| 6183 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6184 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6185 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6186 | audio_patch_handle_t *patchHandle, |
| 6187 | AudioIODescriptorInterface *ioDescriptor, |
| 6188 | const struct audio_patch *patch, |
| 6189 | int delayMs) |
| 6190 | { |
| 6191 | ssize_t index = mAudioPatches.indexOfKey( |
| 6192 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6193 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6194 | sp<AudioPatch> patchDesc; |
| 6195 | status_t status = installPatch( |
| 6196 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6197 | if (status == NO_ERROR) { |
| 6198 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6199 | } |
| 6200 | return status; |
| 6201 | } |
| 6202 | |
| 6203 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6204 | ssize_t index, |
| 6205 | audio_patch_handle_t *patchHandle, |
| 6206 | const struct audio_patch *patch, |
| 6207 | int delayMs, |
| 6208 | uid_t uid, |
| 6209 | sp<AudioPatch> *patchDescPtr) |
| 6210 | { |
| 6211 | sp<AudioPatch> patchDesc; |
| 6212 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6213 | if (index >= 0) { |
| 6214 | patchDesc = mAudioPatches.valueAt(index); |
| 6215 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6216 | } |
| 6217 | |
| 6218 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6219 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6220 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6221 | if (status == NO_ERROR) { |
| 6222 | if (index < 0) { |
| 6223 | patchDesc = new AudioPatch(patch, uid); |
| 6224 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6225 | } else { |
| 6226 | patchDesc->mPatch = *patch; |
| 6227 | } |
| 6228 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6229 | if (patchHandle) { |
| 6230 | *patchHandle = patchDesc->mHandle; |
| 6231 | } |
| 6232 | nextAudioPortGeneration(); |
| 6233 | mpClientInterface->onAudioPatchListUpdate(); |
| 6234 | } |
| 6235 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6236 | return status; |
| 6237 | } |
| 6238 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6239 | } // namespace android |