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 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 65 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 66 | static const std::vector<audio_format_t> compressedFormatsOrder = {{ |
| 67 | AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 68 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 69 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
| 70 | static const std::vector<audio_channel_mask_t> surroundChannelMasksOrder = {{ |
| 71 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 72 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 73 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 74 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 75 | // ---------------------------------------------------------------------------- |
| 76 | // AudioPolicyInterface implementation |
| 77 | // ---------------------------------------------------------------------------- |
| 78 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 79 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 80 | audio_policy_dev_state_t state, |
| 81 | const char *device_address, |
| 82 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 83 | { |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 84 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); |
| 85 | nextAudioPortGeneration(); |
| 86 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 87 | } |
| 88 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 89 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 90 | audio_policy_dev_state_t state, |
| 91 | const String8 &device_address) |
| 92 | { |
| 93 | AudioParameter param(device_address); |
| 94 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 95 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 96 | param.addInt(key, device); |
| 97 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 98 | } |
| 99 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 100 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 101 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 102 | const char *device_address, |
| 103 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 104 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 105 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 106 | device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 107 | |
| 108 | // connect/disconnect only 1 device at a time |
| 109 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 110 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 111 | sp<DeviceDescriptor> devDesc = |
| 112 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 113 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 114 | // handle output devices |
| 115 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 116 | SortedVector <audio_io_handle_t> outputs; |
| 117 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 118 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 119 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 120 | // save a copy of the opened output descriptors before any output is opened or closed |
| 121 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 122 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 123 | switch (state) |
| 124 | { |
| 125 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 126 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 127 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 128 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 129 | return INVALID_OPERATION; |
| 130 | } |
| 131 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 132 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 133 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 134 | index = mAvailableOutputDevices.add(devDesc); |
| 135 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 136 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 137 | if (module == 0) { |
| 138 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 139 | device); |
| 140 | mAvailableOutputDevices.remove(devDesc); |
| 141 | return INVALID_OPERATION; |
| 142 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 143 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 144 | } else { |
| 145 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 146 | } |
| 147 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 148 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 149 | // parameters on newly connected devices (instead of opening the outputs...) |
| 150 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 151 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 152 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 153 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 154 | |
| 155 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 156 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 157 | return INVALID_OPERATION; |
| 158 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 159 | // Propagate device availability to Engine |
| 160 | mEngine->setDeviceConnectionState(devDesc, state); |
| 161 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 162 | // outputs should never be empty here |
| 163 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 164 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 165 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 166 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 167 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 168 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 169 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 170 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 171 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 172 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 173 | return INVALID_OPERATION; |
| 174 | } |
| 175 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 176 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 177 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 178 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 179 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 180 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 181 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 182 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 184 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 185 | |
| 186 | // Propagate device availability to Engine |
| 187 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 188 | } break; |
| 189 | |
| 190 | default: |
| 191 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 192 | return BAD_VALUE; |
| 193 | } |
| 194 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 195 | checkForDeviceAndOutputChanges([&]() { |
| 196 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 197 | if (!outputs.isEmpty()) { |
| 198 | for (audio_io_handle_t output : outputs) { |
| 199 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 200 | // close unused outputs after device disconnection or direct outputs that have been |
| 201 | // opened by checkOutputsForDevice() to query dynamic parameters |
| 202 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 203 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 204 | (desc->mDirectOpenCount == 0))) { |
| 205 | closeOutput(output); |
| 206 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 208 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 209 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 210 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 211 | return false; |
| 212 | }); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 214 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 215 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 216 | updateCallRouting(newDevice); |
| 217 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 218 | const audio_devices_t msdOutDevice = getMsdAudioOutDeviceTypes(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 219 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 220 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 221 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 222 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 223 | // do not force device change on duplicated output because if device is 0, it will |
| 224 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 225 | // a valid device selection on those outputs. |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 226 | bool force = (msdOutDevice == AUDIO_DEVICE_NONE || msdOutDevice != desc->device()) |
| 227 | && !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 228 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 229 | // always force when disconnecting (a non-duplicated device) |
| 230 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 231 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 232 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 235 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 236 | cleanUpForDevice(devDesc); |
| 237 | } |
| 238 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 239 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 240 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 241 | } // end if is output device |
| 242 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 | // handle input devices |
| 244 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 245 | SortedVector <audio_io_handle_t> inputs; |
| 246 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 247 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 248 | switch (state) |
| 249 | { |
| 250 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 251 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 252 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 253 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 254 | return INVALID_OPERATION; |
| 255 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 256 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 257 | if (module == NULL) { |
| 258 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 259 | device); |
| 260 | return INVALID_OPERATION; |
| 261 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 262 | |
| 263 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 264 | // parameters on newly connected devices (instead of opening the inputs...) |
| 265 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 266 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 267 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 268 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 269 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 270 | return INVALID_OPERATION; |
| 271 | } |
| 272 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 273 | index = mAvailableInputDevices.add(devDesc); |
| 274 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 275 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 276 | } else { |
| 277 | return NO_MEMORY; |
| 278 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 279 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 280 | // Propagate device availability to Engine |
| 281 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 282 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 283 | |
| 284 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 285 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 286 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 287 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 288 | return INVALID_OPERATION; |
| 289 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 290 | |
| 291 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 292 | |
| 293 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 294 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 295 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 296 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 297 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 298 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 299 | // Propagate device availability to Engine |
| 300 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 301 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 302 | |
| 303 | default: |
| 304 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 305 | return BAD_VALUE; |
| 306 | } |
| 307 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 308 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 309 | // As the input device list can impact the output device selection, update |
| 310 | // getDeviceForStrategy() cache |
| 311 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 313 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 314 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 315 | updateCallRouting(newDevice); |
| 316 | } |
| 317 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 318 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 319 | cleanUpForDevice(devDesc); |
| 320 | } |
| 321 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 322 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 323 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 324 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 325 | |
| 326 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 327 | return BAD_VALUE; |
| 328 | } |
| 329 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 330 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 331 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 333 | sp<DeviceDescriptor> devDesc = |
| 334 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 335 | (strlen(device_address) != 0)/*matchAddress*/); |
| 336 | |
| 337 | if (devDesc == 0) { |
| 338 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 339 | device, device_address); |
| 340 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 341 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 342 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 343 | DeviceVector *deviceVector; |
| 344 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 345 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 346 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 347 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 348 | deviceVector = &mAvailableInputDevices; |
| 349 | } else { |
| 350 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 351 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
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 | |
| 354 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 355 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 358 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 359 | const char *device_address, |
| 360 | const char *device_name) |
| 361 | { |
| 362 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 363 | String8 reply; |
| 364 | AudioParameter param; |
| 365 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 366 | |
| 367 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 368 | device, device_address, device_name); |
| 369 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 370 | // connect/disconnect only 1 device at a time |
| 371 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 372 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 373 | // Check if the device is currently connected |
| 374 | sp<DeviceDescriptor> devDesc = |
| 375 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 376 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 377 | if (index < 0) { |
| 378 | // Nothing to do: device is not connected |
| 379 | return NO_ERROR; |
| 380 | } |
| 381 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 382 | // For offloaded A2DP, Hw modules may have the capability to |
| 383 | // configure codecs. Check if any of the loaded hw modules |
| 384 | // supports this. |
| 385 | // If supported, send a set parameter to configure A2DP codecs |
| 386 | // and return. No need to toggle device state. |
| 387 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 388 | reply = mpClientInterface->getParameters( |
| 389 | AUDIO_IO_HANDLE_NONE, |
| 390 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 391 | AudioParameter repliedParameters(reply); |
| 392 | repliedParameters.getInt( |
| 393 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 394 | if (isReconfigA2dpSupported) { |
| 395 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 396 | param.add(key, String8("true")); |
| 397 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 398 | return NO_ERROR; |
| 399 | } |
| 400 | } |
| 401 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 402 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 403 | // This will force reading again the device configuration |
| 404 | status = setDeviceConnectionState(device, |
| 405 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 406 | device_address, device_name); |
| 407 | if (status != NO_ERROR) { |
| 408 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 409 | status); |
| 410 | return status; |
| 411 | } |
| 412 | |
| 413 | status = setDeviceConnectionState(device, |
| 414 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 415 | device_address, device_name); |
| 416 | if (status != NO_ERROR) { |
| 417 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 418 | status); |
| 419 | return status; |
| 420 | } |
| 421 | |
| 422 | return NO_ERROR; |
| 423 | } |
| 424 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 425 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 426 | { |
| 427 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 428 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 429 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 430 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 431 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 432 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 433 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 434 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 435 | |
| 436 | // release existing RX patch if any |
| 437 | if (mCallRxPatch != 0) { |
| 438 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 439 | mCallRxPatch.clear(); |
| 440 | } |
| 441 | // release TX patch if any |
| 442 | if (mCallTxPatch != 0) { |
| 443 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 444 | mCallTxPatch.clear(); |
| 445 | } |
| 446 | |
| 447 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 448 | // via setOutputDevice() on primary output. |
| 449 | // Otherwise, create two audio patches for TX and RX path. |
| 450 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 451 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 453 | // of it due to legacy implementation. If not, create a patch. |
| 454 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 455 | == AUDIO_DEVICE_NONE) { |
| 456 | createTxPatch = true; |
| 457 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 458 | } else { // create RX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 459 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 460 | createTxPatch = true; |
| 461 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 462 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 463 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 464 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 465 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 466 | return muteWaitMs; |
| 467 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 468 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 469 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
| 470 | bool isRx, audio_devices_t device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 471 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 472 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 473 | sp<DeviceDescriptor> txSourceDeviceDesc; |
| 474 | if (isRx) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 475 | patchBuilder.addSink(findDevice(mAvailableOutputDevices, device)). |
| 476 | addSource(findDevice(mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 477 | } else { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 478 | patchBuilder.addSource(txSourceDeviceDesc = findDevice(mAvailableInputDevices, device)). |
| 479 | addSink(findDevice(mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX; |
| 483 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs); |
| 484 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 485 | // request to reuse existing output stream if one is already opened to reach the target device |
| 486 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 487 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 488 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 489 | "%s() %#x device output %d is duplicated", __func__, outputDevice, output); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 490 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 494 | // terminate active capture if on the same HW module as the call TX source device |
| 495 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 496 | // call TX device but this information is not in the audio patch and logic here must be |
| 497 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 498 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 499 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 500 | closeActiveClients(activeDesc); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 503 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 504 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 505 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 506 | status_t status = mpClientInterface->createAudioPatch( |
| 507 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 508 | ALOGW_IF(status != NO_ERROR, |
| 509 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 510 | sp<AudioPatch> audioPatch; |
| 511 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 512 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 513 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 514 | audioPatch->mUid = mUidCached; |
| 515 | } |
| 516 | return audioPatch; |
| 517 | } |
| 518 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 519 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 520 | const DeviceVector& devices, audio_devices_t device) const { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 521 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 522 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 523 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 524 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 527 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 528 | { |
| 529 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 530 | // store previous phone state for management of sonification strategy below |
| 531 | int oldState = mEngine->getPhoneState(); |
| 532 | |
| 533 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 534 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 535 | return; |
| 536 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 537 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 538 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 539 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 540 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 541 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 542 | } |
| 543 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 544 | /** |
| 545 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 546 | * routing command. |
| 547 | */ |
| 548 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 549 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 550 | |
| 551 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 552 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 553 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 | int delayMs = 0; |
| 555 | if (isStateInCall(state)) { |
| 556 | nsecs_t sysTime = systemTime(); |
| 557 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 558 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 559 | // mute media and sonification strategies and delay device switch by the largest |
| 560 | // latency of any output where either strategy is active. |
| 561 | // 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] | 562 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 563 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 564 | sysTime) || |
| 565 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 566 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 567 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 568 | (delayMs < (int)desc->latency()*2)) { |
| 569 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 571 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 572 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 573 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 574 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 575 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 576 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 577 | } |
| 578 | } |
| 579 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 580 | if (hasPrimaryOutput()) { |
| 581 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 582 | // the device returned is not necessarily reachable via this output |
| 583 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 584 | // force routing command to audio hardware when ending call |
| 585 | // even if no device change is needed |
| 586 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 587 | rxDevice = mPrimaryOutput->device(); |
| 588 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 589 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 590 | if (state == AUDIO_MODE_IN_CALL) { |
| 591 | updateCallRouting(rxDevice, delayMs); |
| 592 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 593 | if (mCallRxPatch != 0) { |
| 594 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 595 | mCallRxPatch.clear(); |
| 596 | } |
| 597 | if (mCallTxPatch != 0) { |
| 598 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 599 | mCallTxPatch.clear(); |
| 600 | } |
| 601 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 602 | } else { |
| 603 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 604 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 605 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 606 | |
| 607 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 608 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 609 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 610 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 611 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 612 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | if (isStateInCall(state)) { |
| 617 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 618 | // force reevaluating accessibility routing when call starts |
| 619 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | // 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] | 623 | if (state == AUDIO_MODE_RINGTONE && |
| 624 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 625 | mLimitRingtoneVolume = true; |
| 626 | } else { |
| 627 | mLimitRingtoneVolume = false; |
| 628 | } |
| 629 | } |
| 630 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 631 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 632 | return mEngine->getPhoneState(); |
| 633 | } |
| 634 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 635 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 636 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 638 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 639 | if (config == mEngine->getForceUse(usage)) { |
| 640 | return; |
| 641 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 642 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 643 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 644 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 645 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 646 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 647 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 648 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 649 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 650 | |
| 651 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 652 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 653 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 654 | //FIXME: workaround for truncated touch sounds |
| 655 | // to be removed when the problem is handled by system UI |
| 656 | uint32_t delayMs = 0; |
| 657 | uint32_t waitMs = 0; |
| 658 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 659 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 660 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 661 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 662 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 663 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 664 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 665 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 666 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 667 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 668 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 669 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 670 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 671 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 672 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 673 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 677 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 678 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 679 | // 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] | 680 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 681 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 682 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 683 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 684 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 685 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 686 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 689 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 690 | { |
| 691 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 692 | } |
| 693 | |
| 694 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 695 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 696 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 697 | audio_devices_t device, |
| 698 | uint32_t samplingRate, |
| 699 | audio_format_t format, |
| 700 | audio_channel_mask_t channelMask, |
| 701 | audio_output_flags_t flags) |
| 702 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 703 | // only retain flags that will drive the direct output profile selection |
| 704 | // if explicitly requested |
| 705 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 706 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 707 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 708 | flags = |
| 709 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 710 | |
| 711 | sp<IOProfile> profile; |
| 712 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 713 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 714 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 715 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 716 | samplingRate, NULL /*updatedSamplingRate*/, |
| 717 | format, NULL /*updatedFormat*/, |
| 718 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 719 | flags)) { |
| 720 | continue; |
| 721 | } |
| 722 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 723 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 724 | continue; |
| 725 | } |
| 726 | // 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] | 727 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 728 | continue; |
| 729 | } |
| 730 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 731 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 732 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 733 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 736 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 737 | } |
| 738 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 739 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 740 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 741 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 742 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 743 | |
| 744 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 745 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 746 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 747 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 748 | // and AudioSystem::getOutputSamplingRate(). |
| 749 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 750 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 751 | 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] | 752 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 753 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 754 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 757 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 758 | audio_io_handle_t *output, |
| 759 | audio_session_t session, |
| 760 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 761 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 762 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 763 | audio_output_flags_t *flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 764 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 765 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 766 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 767 | audio_attributes_t attributes; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 768 | DeviceVector outputDevices; |
| 769 | routing_strategy strategy; |
| 770 | audio_devices_t device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 771 | const audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 772 | audio_devices_t msdDevice = getMsdAudioOutDeviceTypes(); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 773 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 774 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 775 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 776 | return INVALID_OPERATION; |
| 777 | } |
| 778 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 779 | if (attr != NULL) { |
| 780 | if (!isValidAttributes(attr)) { |
| 781 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 782 | attr->usage, attr->content_type, attr->flags, |
| 783 | attr->tags); |
| 784 | return BAD_VALUE; |
| 785 | } |
| 786 | attributes = *attr; |
| 787 | } else { |
| 788 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 789 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 790 | return BAD_VALUE; |
| 791 | } |
| 792 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 793 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 794 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 795 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 796 | " session %d selectedDeviceId %d", |
| 797 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 798 | session, requestedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 799 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 800 | *stream = streamTypefromAttributesInt(&attributes); |
| 801 | |
| 802 | strategy = getStrategyForAttr(&attributes); |
| 803 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 804 | // First check for explicit routing (eg. setPreferredDevice) |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 805 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 806 | sp<DeviceDescriptor> deviceDesc = |
| 807 | mAvailableOutputDevices.getDeviceFromId(requestedDeviceId); |
| 808 | device = deviceDesc->type(); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 809 | } else { |
| 810 | // If no explict route, is there a matching dynamic policy that applies? |
| 811 | sp<SwAudioOutputDescriptor> desc; |
| 812 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
| 813 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 814 | if (!audio_has_proportional_frames(config->format)) { |
| 815 | return BAD_VALUE; |
| 816 | } |
| 817 | *stream = streamTypefromAttributesInt(&attributes); |
| 818 | *output = desc->mIoHandle; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 819 | AudioMix *mix = desc->mPolicyMix; |
| 820 | sp<DeviceDescriptor> deviceDesc = |
| 821 | mAvailableOutputDevices.getDevice(mix->mDeviceType, mix->mDeviceAddress); |
| 822 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 823 | ALOGV("getOutputForAttr() returns output %d", *output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 824 | goto exit; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | // Virtual sources must always be dynamicaly or explicitly routed |
| 828 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 829 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 830 | return BAD_VALUE; |
| 831 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 832 | device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 833 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 834 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 835 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 836 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 839 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 840 | // chosen by the engine if not. |
| 841 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 842 | // to getOutputForDevice. |
| 843 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
| 844 | *stream == AUDIO_STREAM_MUSIC && |
| 845 | audio_is_linear_pcm(config->format) && |
| 846 | isInCall()) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 847 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 848 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 849 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 850 | // Get the devce type directly from the engine to bypass preferred route logic |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 851 | device = mEngine->getDeviceForStrategy(strategy); |
| 852 | } |
| 853 | } |
| 854 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 855 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 856 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 857 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 858 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 859 | *output = AUDIO_IO_HANDLE_NONE; |
| 860 | if (msdDevice != AUDIO_DEVICE_NONE) { |
| 861 | *output = getOutputForDevice(msdDevice, session, *stream, config, flags); |
| 862 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(device) == NO_ERROR) { |
| 863 | ALOGV("%s() Using MSD device 0x%x instead of device 0x%x", |
| 864 | __func__, msdDevice, device); |
| 865 | device = msdDevice; |
| 866 | } else { |
| 867 | *output = AUDIO_IO_HANDLE_NONE; |
| 868 | } |
| 869 | } |
| 870 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 871 | *output = getOutputForDevice(device, session, *stream, config, flags); |
| 872 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 873 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 874 | return INVALID_OPERATION; |
| 875 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 876 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 877 | outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 878 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 879 | : AUDIO_PORT_HANDLE_NONE; |
| 880 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 881 | exit: |
| 882 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 883 | .format = config->format, |
| 884 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 885 | *portId = AudioPort::getNextUniqueId(); |
| 886 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 887 | sp<TrackClientDescriptor> clientDesc = |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 888 | new TrackClientDescriptor(*portId, uid, session, attributes, clientConfig, |
| 889 | requestedDeviceId, *stream, |
| 890 | getStrategyForAttr(&attributes), |
| 891 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 892 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 893 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 894 | |
| 895 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d for port ID %d", |
| 896 | *output, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 897 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 898 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 902 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 903 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 904 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 905 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 906 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 907 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 908 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 909 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 910 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 911 | // open a direct output if required by specified parameters |
| 912 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 913 | // and all common behaviors are driven by checking only the direct flag |
| 914 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 915 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 916 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 918 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 919 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 920 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 921 | // only allow deep buffering for music stream type |
| 922 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 923 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 924 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 925 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 926 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 927 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 928 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 929 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 930 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 931 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 932 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 933 | audio_is_linear_pcm(config->format)) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 934 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 935 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 936 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 937 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 938 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 939 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 940 | sp<IOProfile> profile; |
| 941 | |
| 942 | // 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] | 943 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 944 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 945 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 946 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 947 | goto non_direct_output; |
| 948 | } |
| 949 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 950 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 951 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 952 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 953 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 954 | // This may prevent offloading in rare situations where effects are left active by apps |
| 955 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 956 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 957 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 958 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 959 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 960 | config->sample_rate, |
| 961 | config->format, |
| 962 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 963 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 966 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 967 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 968 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 969 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 970 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 971 | // reuse direct output if currently open by the same client |
| 972 | // and configured with same parameters |
| 973 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 974 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 975 | (config->channel_mask == desc->mChannelMask) && |
| 976 | (session == desc->mDirectClientSession)) { |
| 977 | desc->mDirectOpenCount++; |
| 978 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 979 | mOutputs.keyAt(i), session); |
| 980 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 984 | |
| 985 | if (!profile->canOpenNewIo()) { |
| 986 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 987 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 988 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 989 | sp<SwAudioOutputDescriptor> outputDesc = |
| 990 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 991 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 992 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 993 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 994 | : String8(""); |
| 995 | |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 996 | // MSD patch may be using the only output stream that can service this request. Release |
| 997 | // MSD patch to prioritize this request over any active output on MSD. |
| 998 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 999 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1000 | const auto& patch = msdPatches[i]; |
| 1001 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1002 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1003 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 1004 | (sink->ext.device.type & device) != AUDIO_DEVICE_NONE && |
| 1005 | (address.isEmpty() || strncmp(sink->ext.device.address, address.string(), |
| 1006 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 1007 | releaseAudioPatch(patch->mHandle, mUidCached); |
| 1008 | break; |
| 1009 | } |
| 1010 | } |
| 1011 | } |
| 1012 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1013 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | |
| 1015 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1016 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1017 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1018 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1019 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1020 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1021 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1022 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1023 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1024 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1025 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1026 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1027 | // 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] | 1028 | if (audio_is_linear_pcm(config->format) && |
| 1029 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1030 | goto non_direct_output; |
| 1031 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1032 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1033 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1034 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1035 | outputDesc->mDirectClientSession = session; |
| 1036 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1037 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1038 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1039 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1040 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | return output; |
| 1042 | } |
| 1043 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1044 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1045 | |
| 1046 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1047 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1048 | 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] | 1049 | return AUDIO_IO_HANDLE_NONE; |
| 1050 | } |
| 1051 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1052 | // ignoring channel mask due to downmix capability in mixer |
| 1053 | |
| 1054 | // open a non direct output |
| 1055 | |
| 1056 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1057 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1058 | // get which output is suitable for the specified stream. The actual |
| 1059 | // routing change will happen when startOutput() will be called |
| 1060 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1061 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1062 | // 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] | 1063 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1064 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1065 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1066 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1067 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1068 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1069 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | return output; |
| 1071 | } |
| 1072 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame^] | 1073 | sp<HwModule> AudioPolicyManager::getMsdModule() const { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1074 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame^] | 1075 | if (msdModule != 0 && property_get_bool("audio.msd.disable", false /* default_value */)) { |
| 1076 | ALOGI("use of the MSD module is disabled by audio.msd.disable property"); |
| 1077 | return 0; |
| 1078 | } |
| 1079 | return msdModule; |
| 1080 | } |
| 1081 | |
| 1082 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
| 1083 | sp<HwModule> msdModule = getMsdModule(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1084 | if (msdModule != 0) { |
| 1085 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1086 | msdModule->getHandle()); |
| 1087 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1088 | } |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | audio_devices_t AudioPolicyManager::getMsdAudioOutDeviceTypes() const { |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame^] | 1093 | sp<HwModule> msdModule = getMsdModule(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1094 | if (msdModule != 0) { |
| 1095 | return mAvailableOutputDevices.getDeviceTypesFromHwModule(msdModule->getHandle()); |
| 1096 | } |
| 1097 | return AUDIO_DEVICE_NONE; |
| 1098 | } |
| 1099 | |
| 1100 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1101 | AudioPatchCollection msdPatches; |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame^] | 1102 | // This function ignores audio.msd.disable property to allow patch teardown. |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1103 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1104 | if (msdModule != 0) { |
| 1105 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1106 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1107 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1108 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1109 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1110 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1111 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1112 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | return msdPatches; |
| 1117 | } |
| 1118 | |
| 1119 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1120 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1121 | { |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame^] | 1122 | sp<HwModule> msdModule = getMsdModule(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1123 | if (msdModule == nullptr) { |
| 1124 | ALOGE("%s() unable to get MSD module", __func__); |
| 1125 | return NO_INIT; |
| 1126 | } |
| 1127 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1128 | if (deviceModule == nullptr) { |
| 1129 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1130 | return NO_INIT; |
| 1131 | } |
| 1132 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1133 | if (inputProfiles.isEmpty()) { |
| 1134 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1135 | return NO_INIT; |
| 1136 | } |
| 1137 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1138 | if (outputProfiles.isEmpty()) { |
| 1139 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1140 | return NO_INIT; |
| 1141 | } |
| 1142 | AudioProfileVector msdProfiles; |
| 1143 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1144 | for (const auto &inProfile : inputProfiles) { |
| 1145 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1146 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1147 | } |
| 1148 | } |
| 1149 | AudioProfileVector deviceProfiles; |
| 1150 | for (const auto &outProfile : outputProfiles) { |
| 1151 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1152 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1153 | } |
| 1154 | } |
| 1155 | struct audio_config_base bestSinkConfig; |
| 1156 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1157 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1158 | &bestSinkConfig); |
| 1159 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1160 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1161 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1162 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1163 | } |
| 1164 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1165 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1166 | sinkConfig->format = bestSinkConfig.format; |
| 1167 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1168 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1169 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1170 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1171 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1172 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1173 | sourceConfig->format = bestSinkConfig.format; |
| 1174 | // Copy input stream directly without any processing (e.g. resampling). |
| 1175 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1176 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1177 | if (hwAvSync) { |
| 1178 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1179 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1180 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1181 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1182 | } |
| 1183 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1184 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1185 | sinkConfig->config_mask |= config_mask; |
| 1186 | sourceConfig->config_mask |= config_mask; |
| 1187 | return NO_ERROR; |
| 1188 | } |
| 1189 | |
| 1190 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1191 | { |
| 1192 | PatchBuilder patchBuilder; |
| 1193 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1194 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1195 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1196 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1197 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1198 | // For now, we just forcefully try with HwAvSync first. |
| 1199 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1200 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1201 | getBestMsdAudioProfileFor( |
| 1202 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1203 | if (res == NO_ERROR) { |
| 1204 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1205 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1206 | } |
| 1207 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1208 | " supporting PCM format conversion.", __func__); |
| 1209 | return patchBuilder; |
| 1210 | } |
| 1211 | |
| 1212 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1213 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1214 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1215 | // Use media strategy for unspecified output device. This should only |
| 1216 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1217 | // therefore invalidate explicit routing requests. |
| 1218 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1219 | } |
| 1220 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1221 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1222 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1223 | if (!msdPatches.isEmpty()) { |
| 1224 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1225 | "The current MSD prototype only supports one output patch"); |
| 1226 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1227 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1228 | return NO_ERROR; |
| 1229 | } |
| 1230 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1231 | } |
| 1232 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1233 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1234 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1235 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1236 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1237 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1238 | return status; |
| 1239 | } |
| 1240 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1241 | 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] | 1242 | audio_output_flags_t flags, |
| 1243 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1244 | { |
| 1245 | // select one output among several that provide a path to a particular device or set of |
| 1246 | // devices (the list was previously build by getOutputsForDevice()). |
| 1247 | // The priority is as follows: |
| 1248 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1249 | // 2: the output with the bit depth the closest to the requested one |
| 1250 | // 3: the primary output |
| 1251 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | |
| 1253 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1254 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1255 | } |
| 1256 | if (outputs.size() == 1) { |
| 1257 | return outputs[0]; |
| 1258 | } |
| 1259 | |
| 1260 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1261 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1262 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1263 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1264 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1265 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1266 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1267 | for (audio_io_handle_t output : outputs) { |
| 1268 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1269 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1270 | // if a valid format is specified, skip output if not compatible |
| 1271 | if (format != AUDIO_FORMAT_INVALID) { |
| 1272 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1273 | if (format != outputDesc->mFormat) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1274 | continue; |
| 1275 | } |
| 1276 | } else if (!audio_is_linear_pcm(format)) { |
| 1277 | continue; |
| 1278 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1279 | if (AudioPort::isBetterFormatMatch( |
| 1280 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1281 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1282 | bestFormat = outputDesc->mFormat; |
| 1283 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1286 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1287 | if (commonFlags >= maxCommonFlags) { |
| 1288 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1289 | if (format != AUDIO_FORMAT_INVALID |
| 1290 | && AudioPort::isBetterFormatMatch( |
| 1291 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1292 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1293 | bestFormatForFlags = outputDesc->mFormat; |
| 1294 | } |
| 1295 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1296 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1297 | maxCommonFlags = commonFlags; |
| 1298 | bestFormatForFlags = outputDesc->mFormat; |
| 1299 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1300 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1301 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1302 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1303 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
| 1306 | } |
| 1307 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1308 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1309 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1310 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1311 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1312 | return outputForFormat; |
| 1313 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1314 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1315 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | return outputs[0]; |
| 1319 | } |
| 1320 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1321 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1322 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1323 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1324 | |
| 1325 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1326 | if (outputDesc == 0) { |
| 1327 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1328 | return BAD_VALUE; |
| 1329 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1330 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
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", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1333 | outputDesc->mIoHandle, client->stream(), client->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 | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1340 | uint32_t delayMs; |
| 1341 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1342 | |
| 1343 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1344 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1345 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1346 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1347 | if (delayMs != 0) { |
| 1348 | usleep(delayMs * 1000); |
| 1349 | } |
| 1350 | |
| 1351 | return status; |
| 1352 | } |
| 1353 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1354 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1355 | const sp<TrackClientDescriptor>& client, |
| 1356 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1357 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1358 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1359 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1360 | |
| 1361 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1362 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1363 | if (stream == AUDIO_STREAM_TTS) { |
| 1364 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1365 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1366 | return INVALID_OPERATION; |
| 1367 | } else { |
| 1368 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1369 | } |
| 1370 | } else { |
| 1371 | // some playback other than beacon starts |
| 1372 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1373 | } |
| 1374 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1375 | // 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] | 1376 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1377 | bool force = !outputDesc->isActive() && |
| 1378 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1379 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1380 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 1381 | AudioMix *policyMix = NULL; |
| 1382 | const char *address = NULL; |
| 1383 | if (outputDesc->mPolicyMix != NULL) { |
| 1384 | policyMix = outputDesc->mPolicyMix; |
| 1385 | address = policyMix->mDeviceAddress.string(); |
| 1386 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1387 | device = policyMix->mDeviceType; |
| 1388 | } else { |
| 1389 | device = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1390 | } |
| 1391 | } |
| 1392 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1393 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1394 | // It covers a common case when there is no materially active audio |
| 1395 | // and muting would result in unnecessary delay and dropped audio. |
| 1396 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1397 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1398 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1399 | // increment usage count for this stream on the requested output: |
| 1400 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1401 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1402 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1403 | |
| 1404 | if (client->hasPreferredDevice(true)) { |
| 1405 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 1406 | if (device != outputDesc->device()) { |
| 1407 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1408 | } |
| 1409 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1410 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1411 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1412 | selectOutputForMusicEffects(); |
| 1413 | } |
| 1414 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1415 | if (outputDesc->streamActiveCount(stream) == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1416 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1417 | if (device == AUDIO_DEVICE_NONE) { |
| 1418 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1419 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1420 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1421 | routing_strategy strategy = getStrategy(stream); |
| 1422 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1423 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1424 | (beaconMuteLatency > 0); |
| 1425 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1426 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1427 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1428 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1429 | // An output has a shared device if |
| 1430 | // - managed by the same hw module |
| 1431 | // - supports the currently selected device |
| 1432 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1433 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1434 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1435 | // force a device change if any other output is: |
| 1436 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1437 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1438 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1439 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1440 | // 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] | 1441 | // change the device currently selected by the other output. |
| 1442 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1443 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1444 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1445 | force = true; |
| 1446 | } |
| 1447 | // 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] | 1448 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1449 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1450 | const uint32_t latencyMs = desc->latency(); |
| 1451 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1452 | |
| 1453 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1454 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1455 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1456 | |
| 1457 | // Require mute check if another output is on a shared device |
| 1458 | // and currently active to have proper drain and avoid pops. |
| 1459 | // Note restoring AudioTracks onto this output needs to invoke |
| 1460 | // a volume ramp if there is no mute. |
| 1461 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1462 | } |
| 1463 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1464 | |
| 1465 | const uint32_t muteWaitMs = |
| 1466 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1468 | // apply volume rules for current stream and device if necessary |
| 1469 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1470 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1471 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1472 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1473 | |
| 1474 | // update the outputs if starting an output with a stream that can affect notification |
| 1475 | // routing |
| 1476 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1477 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1478 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1479 | if (strategy == STRATEGY_SONIFICATION) { |
| 1480 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1481 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1482 | |
| 1483 | if (waitMs > muteWaitMs) { |
| 1484 | *delayMs = waitMs - muteWaitMs; |
| 1485 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1486 | |
| 1487 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1488 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1489 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1490 | // change occurs after the MixerThread starts and causes a stream volume |
| 1491 | // glitch. |
| 1492 | // |
| 1493 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1494 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1495 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1496 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1497 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1498 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1499 | } |
| 1500 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1501 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1502 | // of type MIX_TYPE_RECORDERS |
| 1503 | if (audio_is_remote_submix_device(device) && policyMix != NULL && |
| 1504 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1505 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1506 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1507 | address, |
| 1508 | "remote-submix"); |
| 1509 | } |
| 1510 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1511 | return NO_ERROR; |
| 1512 | } |
| 1513 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1514 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1515 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1516 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1517 | |
| 1518 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1519 | if (outputDesc == 0) { |
| 1520 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1521 | return BAD_VALUE; |
| 1522 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1523 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1524 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1525 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1526 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1528 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1529 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1530 | if (status == NO_ERROR ) { |
| 1531 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1532 | } |
| 1533 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1536 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1537 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1538 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1539 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1540 | audio_stream_type_t stream = client->stream(); |
| 1541 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1542 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1543 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1544 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1545 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1546 | // Automatically disable the remote submix input when output is stopped on a |
| 1547 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1548 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1549 | outputDesc->mPolicyMix != NULL && |
| 1550 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1551 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1552 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1553 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1554 | "remote-submix"); |
| 1555 | } |
| 1556 | } |
| 1557 | bool forceDeviceUpdate = false; |
| 1558 | if (client->hasPreferredDevice(true)) { |
| 1559 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1560 | forceDeviceUpdate = true; |
| 1561 | } |
| 1562 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1563 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1564 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1565 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1566 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1567 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1568 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1569 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1570 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1571 | // the track stop() command is received and at that time the audio track buffer can |
| 1572 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1573 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1574 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1575 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1576 | |
| 1577 | // force restoring the device selection on other active outputs if it differs from the |
| 1578 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1579 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1581 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1582 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1583 | desc->isActive() && |
| 1584 | outputDesc->sharesHwModuleWith(desc) && |
| 1585 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1586 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1587 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1588 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1589 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1590 | newDevice2, |
| 1591 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1592 | delayMs); |
| 1593 | // re-apply device specific volume if not done by setOutputDevice() |
| 1594 | if (!force) { |
| 1595 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1596 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1600 | handleNotificationRoutingForStream(stream); |
| 1601 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1602 | |
| 1603 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1604 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1605 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1606 | } |
| 1607 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1608 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1609 | selectOutputForMusicEffects(); |
| 1610 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1611 | return NO_ERROR; |
| 1612 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1613 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1614 | return INVALID_OPERATION; |
| 1615 | } |
| 1616 | } |
| 1617 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1618 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1619 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1620 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1621 | |
| 1622 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1623 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1624 | // If an output descriptor is closed due to a device routing change, |
| 1625 | // then there are race conditions with releaseOutput from tracks |
| 1626 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1627 | // destroyed shortly thereafter. |
| 1628 | // |
| 1629 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1630 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1631 | return; |
| 1632 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1633 | |
| 1634 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1635 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1636 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1637 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1638 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1639 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1640 | return; |
| 1641 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1642 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1643 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1644 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1645 | } |
| 1646 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1647 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1648 | // otherwise there may be inaccurate stream reference counts. |
| 1649 | // This is checked in outputDesc->removeClient below. |
| 1650 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1653 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1654 | audio_io_handle_t *input, |
| 1655 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1656 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1657 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1658 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1659 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1660 | input_type_t *inputType, |
| 1661 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1662 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1663 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1664 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1665 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1666 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1667 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1668 | // handle legacy remote submix case where the address was not always specified |
| 1669 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1670 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1671 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1672 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1673 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1674 | sp<AudioInputDescriptor> inputDesc; |
| 1675 | sp<RecordClientDescriptor> clientDesc; |
| 1676 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1677 | bool isSoundTrigger; |
| 1678 | audio_devices_t device; |
| 1679 | |
| 1680 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1681 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1682 | return INVALID_OPERATION; |
| 1683 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1684 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1685 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1686 | inputSource = AUDIO_SOURCE_MIC; |
| 1687 | } |
| 1688 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1689 | // Explicit routing? |
| 1690 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1691 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1692 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1693 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1694 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1695 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1696 | // possible |
| 1697 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1698 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1699 | ssize_t index = mInputs.indexOfKey(*input); |
| 1700 | if (index < 0) { |
| 1701 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1702 | status = BAD_VALUE; |
| 1703 | goto error; |
| 1704 | } |
| 1705 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1706 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1707 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1708 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1709 | status = BAD_VALUE; |
| 1710 | goto error; |
| 1711 | } |
| 1712 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1713 | // 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] | 1714 | // corresponds to a new client and is only permitted from the same UID. |
| 1715 | // If the first UID is silenced, allow a new UID connection and replace with new UID |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1716 | if (clients.size() > 1) { |
| 1717 | for (const auto& client : clients) { |
| 1718 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1719 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1720 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1721 | // to an actual client |
| 1722 | if (client == *clients.cbegin()) { |
| 1723 | continue; |
| 1724 | } |
| 1725 | if (uid != client->uid() && !client->isSilenced()) { |
| 1726 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1727 | uid, client->portId(), client->uid()); |
| 1728 | status = INVALID_OPERATION; |
| 1729 | goto error; |
| 1730 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1731 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1732 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1733 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1734 | device = inputDesc->mDevice; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1735 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1736 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1737 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1738 | } |
| 1739 | |
| 1740 | *input = AUDIO_IO_HANDLE_NONE; |
| 1741 | *inputType = API_INPUT_INVALID; |
| 1742 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1743 | halInputSource = inputSource; |
| 1744 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1745 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1746 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1747 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1748 | if (status != NO_ERROR) { |
| 1749 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1750 | } |
| 1751 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1752 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1753 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1754 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1755 | if (deviceDesc != 0) { |
| 1756 | device = deviceDesc->type(); |
| 1757 | } else { |
| 1758 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
| 1759 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1760 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1761 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1762 | status = BAD_VALUE; |
| 1763 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1764 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1765 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1766 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1767 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1768 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1769 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1770 | // know about it and is therefore considered "legacy" |
| 1771 | *inputType = API_INPUT_LEGACY; |
| 1772 | } else { |
| 1773 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1774 | // an external policy |
| 1775 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1776 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1777 | } else if (audio_is_remote_submix_device(device)) { |
| 1778 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1779 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1780 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1781 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1782 | } else { |
| 1783 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1784 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1785 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1786 | } |
| 1787 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1788 | *input = getInputForDevice(device, address, session, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1789 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1790 | policyMix); |
| 1791 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1792 | status = INVALID_OPERATION; |
| 1793 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1794 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1795 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1796 | exit: |
| 1797 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1798 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1799 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1800 | : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1801 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1802 | isSoundTrigger = inputSource == AUDIO_SOURCE_HOTWORD && |
| 1803 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1804 | *portId = AudioPort::getNextUniqueId(); |
| 1805 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1806 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1807 | *attr, *config, requestedDeviceId, |
| 1808 | inputSource,flags, isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1809 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1810 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1811 | |
| 1812 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1813 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1814 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1815 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1816 | |
| 1817 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1818 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | |
| 1822 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1823 | String8 address, |
| 1824 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1825 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1826 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1827 | audio_input_flags_t flags, |
| 1828 | AudioMix *policyMix) |
| 1829 | { |
| 1830 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1831 | audio_source_t halInputSource = inputSource; |
| 1832 | bool isSoundTrigger = false; |
| 1833 | |
| 1834 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1835 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1836 | if (index >= 0) { |
| 1837 | input = mSoundTriggerSessions.valueFor(session); |
| 1838 | isSoundTrigger = true; |
| 1839 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1840 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1841 | } else { |
| 1842 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1843 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1844 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1845 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1846 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1849 | // find a compatible input profile (not necessarily identical in parameters) |
| 1850 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1851 | // sampling rate and flags may be updated by getInputProfile |
| 1852 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1853 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1854 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1855 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1856 | audio_input_flags_t profileFlags = flags; |
| 1857 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1858 | 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] | 1859 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1860 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1861 | profileFlags); |
| 1862 | if (profile != 0) { |
| 1863 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1864 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1865 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1866 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1867 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1868 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1869 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1870 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1871 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1872 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1873 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1874 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1875 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1876 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1877 | if (samplingRate == 0) { |
| 1878 | samplingRate = profileSamplingRate; |
| 1879 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1880 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1881 | if (profile->getModuleHandle() == 0) { |
| 1882 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1883 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1884 | } |
| 1885 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1886 | if (!profile->canOpenNewIo()) { |
| 1887 | return AUDIO_IO_HANDLE_NONE; |
| 1888 | } |
| 1889 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1890 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1891 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1892 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1893 | lConfig.sample_rate = profileSamplingRate; |
| 1894 | lConfig.channel_mask = profileChannelMask; |
| 1895 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1896 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1897 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1898 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1899 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1900 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1901 | } |
| 1902 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1903 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1904 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1905 | |
| 1906 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1907 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1908 | (profileSamplingRate != lConfig.sample_rate) || |
| 1909 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1910 | (profileChannelMask != lConfig.channel_mask)) { |
| 1911 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1912 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1913 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1914 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1915 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1916 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1917 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1920 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1921 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1922 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1923 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1924 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1925 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1928 | //static |
| 1929 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1930 | { |
| 1931 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1932 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1933 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1934 | } |
| 1935 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1936 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1937 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1938 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1939 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1940 | unsigned int numModules = 0; |
| 1941 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1942 | |
| 1943 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1944 | if (status == NO_ERROR && numModules != 0) { |
| 1945 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1946 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1947 | if (nModules == NULL) { |
| 1948 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1949 | // ram the next time this function is called. |
| 1950 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
| 1954 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1955 | if (status == NO_ERROR) { |
| 1956 | soundTriggerSupportsConcurrentCapture = true; |
| 1957 | for (size_t i = 0; i < numModules; ++i) { |
| 1958 | soundTriggerSupportsConcurrentCapture &= |
| 1959 | nModules[i].properties.concurrent_capture; |
| 1960 | } |
| 1961 | } |
| 1962 | free(nModules); |
| 1963 | } |
| 1964 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1965 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1966 | } |
| 1967 | return mSoundTriggerSupportsConcurrentCapture; |
| 1968 | } |
| 1969 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1970 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1971 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1972 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1973 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1974 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1975 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
| 1976 | |
| 1977 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1978 | |
| 1979 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 1980 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1981 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1982 | return BAD_VALUE; |
| 1983 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1984 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1985 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1986 | if (client->active()) { |
| 1987 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 1988 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1991 | audio_session_t session = client->session(); |
| 1992 | |
| 1993 | ALOGV("%s input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 1994 | __FUNCTION__, input, session, silenced, *concurrency); |
| 1995 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1996 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1997 | if (mCallTxPatch != 0 && |
| 1998 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1999 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2000 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2001 | return INVALID_OPERATION; |
| 2002 | } |
| 2003 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2004 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2005 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2006 | // If a UID is idle and records silence and another not silenced recording starts |
| 2007 | // from another UID (idle or active) we stop the current idle UID recording in |
| 2008 | // favor of the new one - "There can be only one" TM |
| 2009 | if (!silenced) { |
| 2010 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2011 | if ((activeDesc->getAudioPort()->getFlags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2012 | activeDesc->getId() == inputDesc->getId()) { |
| 2013 | continue; |
| 2014 | } |
| 2015 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2016 | RecordClientVector activeClients = activeDesc->clientsList(true /*activeOnly*/); |
| 2017 | for (const auto& activeClient : activeClients) { |
| 2018 | if (activeClient->isSilenced()) { |
| 2019 | closeClient(activeClient->portId()); |
| 2020 | ALOGV("%s client %d stopping silenced client %d", __FUNCTION__, |
| 2021 | portId, activeClient->portId()); |
| 2022 | activeInputs = mInputs.getActiveInputs(); |
| 2023 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | } |
| 2027 | |
| 2028 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2029 | if ((client->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 2030 | activeDesc->getId() == inputDesc->getId()) { |
| 2031 | continue; |
| 2032 | } |
| 2033 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2034 | audio_source_t activeSource = activeDesc->inputSource(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2035 | if (client->source() == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2036 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2037 | if (activeDesc->hasPreemptedSession(session)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2038 | ALOGW("%s input %d failed for HOTWORD: " |
| 2039 | "other input %d already started for HOTWORD", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2040 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2041 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2042 | return INVALID_OPERATION; |
| 2043 | } |
| 2044 | } else { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2045 | ALOGV("%s input %d failed for HOTWORD: other input %d already started", |
| 2046 | __FUNCTION__, input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2047 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2048 | return INVALID_OPERATION; |
| 2049 | } |
| 2050 | } else { |
| 2051 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2052 | ALOGW("%s input %d failed: other input %d already started", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2053 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2054 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2055 | return INVALID_OPERATION; |
| 2056 | } |
| 2057 | } |
| 2058 | } |
| 2059 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2060 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 2061 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 2062 | // that's running. |
| 2063 | const bool allowConcurrentWithSoundTrigger = |
| 2064 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 2065 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2066 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2067 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2068 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 2069 | continue; |
| 2070 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2071 | RecordClientVector activeHotwordClients = |
| 2072 | activeDesc->clientsList(true, AUDIO_SOURCE_HOTWORD); |
| 2073 | if (activeHotwordClients.size() > 0) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2074 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2075 | |
| 2076 | for (const auto& activeClient : activeHotwordClients) { |
| 2077 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
| 2078 | sessions.add(activeClient->session()); |
| 2079 | closeClient(activeClient->portId()); |
| 2080 | ALOGV("%s input %d for HOTWORD preempting HOTWORD input %d", __FUNCTION__, |
| 2081 | input, activeDesc->mIoHandle); |
| 2082 | } |
| 2083 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2084 | inputDesc->setPreemptedSessions(sessions); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2088 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2089 | // Make sure we start with the correct silence state |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2090 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2091 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2092 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2093 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2094 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2095 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2096 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2097 | // primary HW module |
| 2098 | audio_devices_t device = getNewInputDevice(inputDesc); |
| 2099 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2100 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2101 | status_t status = inputDesc->start(); |
| 2102 | if (status != NO_ERROR) { |
| 2103 | inputDesc->setClientActive(client, false); |
| 2104 | return status; |
| 2105 | } |
| 2106 | |
| 2107 | if (inputDesc->activeCount() == 1) { |
| 2108 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2109 | if ((inputDesc->mPolicyMix != NULL) |
| 2110 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2111 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2112 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2113 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2114 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2115 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2116 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2117 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2118 | SoundTrigger::setCaptureState(true); |
| 2119 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2120 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2121 | // automatically enable the remote submix output when input is started if not |
| 2122 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2123 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2124 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2125 | String8 address = String8(""); |
| 2126 | if (inputDesc->mPolicyMix == NULL) { |
| 2127 | address = String8("0"); |
| 2128 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2129 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2130 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2131 | if (address != "") { |
| 2132 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2133 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2134 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2135 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2136 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | } |
| 2138 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2139 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2140 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2141 | return NO_ERROR; |
| 2142 | } |
| 2143 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2144 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2145 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2146 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2147 | |
| 2148 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2149 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2150 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2151 | return BAD_VALUE; |
| 2152 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2153 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2154 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2155 | if (!client->active()) { |
| 2156 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2157 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2160 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2161 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2162 | inputDesc->stop(); |
| 2163 | if (inputDesc->isActive()) { |
| 2164 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2165 | } else { |
| 2166 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2167 | if ((inputDesc->mPolicyMix != NULL) |
| 2168 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2169 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2170 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2171 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2172 | |
| 2173 | // automatically disable the remote submix output when input is stopped if not |
| 2174 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2175 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2176 | String8 address = String8(""); |
| 2177 | if (inputDesc->mPolicyMix == NULL) { |
| 2178 | address = String8("0"); |
| 2179 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2180 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2181 | } |
| 2182 | if (address != "") { |
| 2183 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2184 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2185 | address, "remote-submix"); |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | audio_devices_t device = inputDesc->mDevice; |
| 2190 | resetInputDevice(input); |
| 2191 | |
| 2192 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2193 | // primary HW module |
| 2194 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2195 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2196 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2197 | SoundTrigger::setCaptureState(false); |
| 2198 | } |
| 2199 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2200 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2201 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2204 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2205 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2206 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2207 | |
| 2208 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2209 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2210 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2211 | return; |
| 2212 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2213 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2214 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2215 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2216 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2217 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2218 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2219 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2220 | if (inputDesc->getClientCount() > 0) { |
| 2221 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2222 | return; |
| 2223 | } |
| 2224 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2225 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2226 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2227 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2230 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2231 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2232 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2233 | |
| 2234 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2235 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2236 | } |
| 2237 | } |
| 2238 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2239 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2240 | { |
| 2241 | stopInput(portId); |
| 2242 | releaseInput(portId); |
| 2243 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2244 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2245 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2246 | bool patchRemoved = false; |
| 2247 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2248 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2249 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2250 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2251 | if (patch_index >= 0) { |
| 2252 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2253 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2254 | mAudioPatches.removeItemsAt(patch_index); |
| 2255 | patchRemoved = true; |
| 2256 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2257 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2258 | } |
| 2259 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2260 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2261 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2262 | |
| 2263 | if (patchRemoved) { |
| 2264 | mpClientInterface->onAudioPatchListUpdate(); |
| 2265 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2266 | } |
| 2267 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2268 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2269 | int indexMin, |
| 2270 | int indexMax) |
| 2271 | { |
| 2272 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2273 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2274 | |
| 2275 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2276 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2277 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2278 | continue; |
| 2279 | } |
| 2280 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2281 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2284 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2285 | int index, |
| 2286 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2287 | { |
| 2288 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2289 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2290 | // app that has MODIFY_PHONE_STATE permission. |
| 2291 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2292 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2293 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2294 | return BAD_VALUE; |
| 2295 | } |
| 2296 | if (!audio_is_output_device(device)) { |
| 2297 | return BAD_VALUE; |
| 2298 | } |
| 2299 | |
| 2300 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2301 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2303 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2304 | stream, device, index); |
| 2305 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2306 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2307 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2308 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2309 | continue; |
| 2310 | } |
| 2311 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2312 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2314 | // update volume on all outputs and streams matching the following: |
| 2315 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2316 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2317 | // the requested device |
| 2318 | // - For non default requested device, currently selected device on the output is either the |
| 2319 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2320 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2321 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2322 | status_t status = NO_ERROR; |
| 2323 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2324 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2325 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2326 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2327 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2328 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2329 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2330 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2331 | continue; |
| 2332 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2333 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2334 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2335 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2336 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2337 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2338 | continue; |
| 2339 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2340 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2341 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2342 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2343 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2344 | } else { |
| 2345 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2346 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2347 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2348 | // rescale index before applying to curStream as ranges may be different for |
| 2349 | // stream and curStream |
| 2350 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2351 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2352 | //FIXME: workaround for truncated touch sounds |
| 2353 | // delayed volume change for system stream to be removed when the problem is |
| 2354 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2355 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2356 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2357 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2358 | if (volStatus != NO_ERROR) { |
| 2359 | status = volStatus; |
| 2360 | } |
| 2361 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2362 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2363 | } |
| 2364 | return status; |
| 2365 | } |
| 2366 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2367 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2368 | int *index, |
| 2369 | audio_devices_t device) |
| 2370 | { |
| 2371 | if (index == NULL) { |
| 2372 | return BAD_VALUE; |
| 2373 | } |
| 2374 | if (!audio_is_output_device(device)) { |
| 2375 | return BAD_VALUE; |
| 2376 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2377 | // 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] | 2378 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2379 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2380 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2381 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2382 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2383 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2384 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2385 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2386 | return NO_ERROR; |
| 2387 | } |
| 2388 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2389 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2390 | { |
| 2391 | // select one output among several suitable for global effects. |
| 2392 | // The priority is as follows: |
| 2393 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2394 | // AudioFlinger will invalidate the track and the offloaded output |
| 2395 | // will be closed causing the effect to be moved to a PCM output. |
| 2396 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2397 | // 3: The primary output |
| 2398 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2399 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2400 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2401 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2402 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2403 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2404 | if (outputs.size() == 0) { |
| 2405 | return AUDIO_IO_HANDLE_NONE; |
| 2406 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2407 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2408 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2409 | bool activeOnly = true; |
| 2410 | |
| 2411 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2412 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2413 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2414 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2415 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2416 | for (audio_io_handle_t output : outputs) { |
| 2417 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2418 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2419 | continue; |
| 2420 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2421 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2422 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2423 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2424 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2425 | } |
| 2426 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2427 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2428 | } |
| 2429 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2430 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2431 | } |
| 2432 | } |
| 2433 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2434 | output = outputOffloaded; |
| 2435 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2436 | output = outputDeepBuffer; |
| 2437 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2438 | output = outputPrimary; |
| 2439 | } else { |
| 2440 | output = outputs[0]; |
| 2441 | } |
| 2442 | activeOnly = false; |
| 2443 | } |
| 2444 | |
| 2445 | if (output != mMusicEffectOutput) { |
| 2446 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2447 | mMusicEffectOutput = output; |
| 2448 | } |
| 2449 | |
| 2450 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2451 | return output; |
| 2452 | } |
| 2453 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2454 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2455 | { |
| 2456 | return selectOutputForMusicEffects(); |
| 2457 | } |
| 2458 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2459 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2460 | audio_io_handle_t io, |
| 2461 | uint32_t strategy, |
| 2462 | int session, |
| 2463 | int id) |
| 2464 | { |
| 2465 | ssize_t index = mOutputs.indexOfKey(io); |
| 2466 | if (index < 0) { |
| 2467 | index = mInputs.indexOfKey(io); |
| 2468 | if (index < 0) { |
| 2469 | ALOGW("registerEffect() unknown io %d", io); |
| 2470 | return INVALID_OPERATION; |
| 2471 | } |
| 2472 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2473 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2474 | } |
| 2475 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2476 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2477 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2478 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2479 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2480 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2481 | continue; |
| 2482 | } |
| 2483 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2484 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2485 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2486 | } |
| 2487 | |
| 2488 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2489 | { |
| 2490 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2491 | } |
| 2492 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2493 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2494 | { |
| 2495 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2496 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2497 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2498 | return true; |
| 2499 | } |
| 2500 | } |
| 2501 | return false; |
| 2502 | } |
| 2503 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2504 | // Register a list of custom mixes with their attributes and format. |
| 2505 | // When a mix is registered, corresponding input and output profiles are |
| 2506 | // added to the remote submix hw module. The profile contains only the |
| 2507 | // parameters (sampling rate, format...) specified by the mix. |
| 2508 | // The corresponding input remote submix device is also connected. |
| 2509 | // |
| 2510 | // When a remote submix device is connected, the address is checked to select the |
| 2511 | // appropriate profile and the corresponding input or output stream is opened. |
| 2512 | // |
| 2513 | // When capture starts, getInputForAttr() will: |
| 2514 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2515 | // - 2 if none found, getDeviceForInputSource() will: |
| 2516 | // - 2.1 look for a mix matching the attributes source |
| 2517 | // - 2.2 if none found, default to device selection by policy rules |
| 2518 | // At this time, the corresponding output remote submix device is also connected |
| 2519 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2520 | // after AudioTracks are invalidated |
| 2521 | // |
| 2522 | // When playback starts, getOutputForAttr() will: |
| 2523 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2524 | // - 2 if none found, look for a mix matching the attributes usage |
| 2525 | // - 3 if none found, default to device and output selection by policy rules. |
| 2526 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2527 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2528 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2529 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2530 | status_t res = NO_ERROR; |
| 2531 | |
| 2532 | sp<HwModule> rSubmixModule; |
| 2533 | // examine each mix's route type |
| 2534 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2535 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2536 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2537 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2538 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2539 | break; |
| 2540 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2541 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2542 | 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] | 2543 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2544 | rSubmixModule = mHwModules.getModuleFromName( |
| 2545 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2546 | if (rSubmixModule == 0) { |
| 2547 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2548 | i); |
| 2549 | res = INVALID_OPERATION; |
| 2550 | break; |
| 2551 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2552 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2553 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2554 | String8 address = mix.mDeviceAddress; |
| 2555 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2556 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2557 | } else { |
| 2558 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2559 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2560 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2561 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2562 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2563 | res = INVALID_OPERATION; |
| 2564 | break; |
| 2565 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2566 | audio_config_t outputConfig = mix.mFormat; |
| 2567 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2568 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2569 | // stereo and let audio flinger do the channel conversion if needed. |
| 2570 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2571 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2572 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2573 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2574 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2575 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2576 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2577 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2578 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2579 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2580 | address.string(), "remote-submix"); |
| 2581 | } else { |
| 2582 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2583 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2584 | address.string(), "remote-submix"); |
| 2585 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2586 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2587 | String8 address = mix.mDeviceAddress; |
| 2588 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2589 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2590 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2591 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2592 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2593 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2594 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2595 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2596 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2597 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2598 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2599 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2600 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2601 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2602 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2603 | } else { |
| 2604 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2605 | } |
| 2606 | break; |
| 2607 | } |
| 2608 | } |
| 2609 | |
| 2610 | if (res != NO_ERROR) { |
| 2611 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2612 | i, device, address.string()); |
| 2613 | res = INVALID_OPERATION; |
| 2614 | break; |
| 2615 | } else if (!foundOutput) { |
| 2616 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2617 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2618 | res = INVALID_OPERATION; |
| 2619 | break; |
| 2620 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2621 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2622 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2623 | if (res != NO_ERROR) { |
| 2624 | unregisterPolicyMixes(mixes); |
| 2625 | } |
| 2626 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2630 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2631 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2632 | status_t res = NO_ERROR; |
| 2633 | sp<HwModule> rSubmixModule; |
| 2634 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2635 | for (const auto& mix : mixes) { |
| 2636 | 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] | 2637 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2638 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2639 | rSubmixModule = mHwModules.getModuleFromName( |
| 2640 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2641 | if (rSubmixModule == 0) { |
| 2642 | res = INVALID_OPERATION; |
| 2643 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2644 | } |
| 2645 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2646 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2647 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2648 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2649 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2650 | res = INVALID_OPERATION; |
| 2651 | continue; |
| 2652 | } |
| 2653 | |
| 2654 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2655 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2656 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2657 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2658 | address.string(), "remote-submix"); |
| 2659 | } |
| 2660 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2661 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2662 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2663 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2664 | address.string(), "remote-submix"); |
| 2665 | } |
| 2666 | rSubmixModule->removeOutputProfile(address); |
| 2667 | rSubmixModule->removeInputProfile(address); |
| 2668 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2669 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2670 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2671 | res = INVALID_OPERATION; |
| 2672 | continue; |
| 2673 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2674 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2675 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2676 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2677 | } |
| 2678 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2679 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2680 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2681 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2682 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2683 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2684 | std::string stateLiteral; |
| 2685 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2686 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2687 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2688 | "communications", "media", "record", "dock", "system", |
| 2689 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2690 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2691 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2692 | dst->appendFormat(" Force use for %s: %d\n", |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2693 | forceUses[i], mEngine->getForceUse(i)); |
| 2694 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2695 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2696 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2697 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2698 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2699 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2700 | mHwModulesAll.dump(dst); |
| 2701 | mOutputs.dump(dst); |
| 2702 | mInputs.dump(dst); |
| 2703 | mVolumeCurves->dump(dst); |
| 2704 | mEffects.dump(dst); |
| 2705 | mAudioPatches.dump(dst); |
| 2706 | mPolicyMixes.dump(dst); |
| 2707 | mAudioSources.dump(dst); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2708 | if (!mSurroundFormats.empty()) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2709 | dst->append("\nEnabled Surround Formats:\n"); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2710 | size_t i = 0; |
| 2711 | for (const auto& fmt : mSurroundFormats) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2712 | dst->append(i++ == 0 ? " " : ", "); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2713 | std::string sfmt; |
| 2714 | FormatConverter::toString(fmt, sfmt); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2715 | dst->append(sfmt.c_str()); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2716 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2717 | dst->append("\n"); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2718 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | status_t AudioPolicyManager::dump(int fd) |
| 2722 | { |
| 2723 | String8 result; |
| 2724 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2725 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2726 | return NO_ERROR; |
| 2727 | } |
| 2728 | |
| 2729 | // This function checks for the parameters which can be offloaded. |
| 2730 | // This can be enhanced depending on the capability of the DSP and policy |
| 2731 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2732 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2733 | { |
| 2734 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2735 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2736 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2737 | offloadInfo.format, |
| 2738 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2739 | offloadInfo.has_video); |
| 2740 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2741 | if (mMasterMono) { |
| 2742 | return false; // no offloading if mono is set. |
| 2743 | } |
| 2744 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2745 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2746 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2747 | ALOGV("offload disabled by audio.offload.disable"); |
| 2748 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | // Check if stream type is music, then only allow offload as of now. |
| 2752 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2753 | { |
| 2754 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2755 | return false; |
| 2756 | } |
| 2757 | |
| 2758 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2759 | const bool allowOffloadWithVideo = |
| 2760 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2761 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2762 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2763 | return false; |
| 2764 | } |
| 2765 | |
| 2766 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2767 | const int min_duration_secs = property_get_int32( |
| 2768 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2769 | if (min_duration_secs >= 0) { |
| 2770 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2771 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2772 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2773 | return false; |
| 2774 | } |
| 2775 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2776 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2777 | return false; |
| 2778 | } |
| 2779 | |
| 2780 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2781 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2782 | // detects there is an active non offloadable effect. |
| 2783 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2784 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2785 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2786 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2787 | return false; |
| 2788 | } |
| 2789 | |
| 2790 | // See if there is a profile to support this. |
| 2791 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2792 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2793 | offloadInfo.sample_rate, |
| 2794 | offloadInfo.format, |
| 2795 | offloadInfo.channel_mask, |
| 2796 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2797 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2798 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2799 | } |
| 2800 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2801 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2802 | audio_port_type_t type, |
| 2803 | unsigned int *num_ports, |
| 2804 | struct audio_port *ports, |
| 2805 | unsigned int *generation) |
| 2806 | { |
| 2807 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2808 | generation == NULL) { |
| 2809 | return BAD_VALUE; |
| 2810 | } |
| 2811 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2812 | if (ports == NULL) { |
| 2813 | *num_ports = 0; |
| 2814 | } |
| 2815 | |
| 2816 | size_t portsWritten = 0; |
| 2817 | size_t portsMax = *num_ports; |
| 2818 | *num_ports = 0; |
| 2819 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2820 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2821 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2822 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2823 | for (const auto& dev : mAvailableOutputDevices) { |
| 2824 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2825 | continue; |
| 2826 | } |
| 2827 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2828 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2829 | } |
| 2830 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2831 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2832 | } |
| 2833 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2834 | for (const auto& dev : mAvailableInputDevices) { |
| 2835 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2836 | continue; |
| 2837 | } |
| 2838 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2839 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2840 | } |
| 2841 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2842 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2846 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2847 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2848 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2849 | } |
| 2850 | *num_ports += mInputs.size(); |
| 2851 | } |
| 2852 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2853 | size_t numOutputs = 0; |
| 2854 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2855 | if (!mOutputs[i]->isDuplicated()) { |
| 2856 | numOutputs++; |
| 2857 | if (portsWritten < portsMax) { |
| 2858 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2859 | } |
| 2860 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2861 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2862 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2866 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2867 | return NO_ERROR; |
| 2868 | } |
| 2869 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2870 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2871 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2872 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2873 | return BAD_VALUE; |
| 2874 | } |
| 2875 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2876 | if (dev != 0) { |
| 2877 | dev->toAudioPort(port); |
| 2878 | return NO_ERROR; |
| 2879 | } |
| 2880 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2881 | if (dev != 0) { |
| 2882 | dev->toAudioPort(port); |
| 2883 | return NO_ERROR; |
| 2884 | } |
| 2885 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2886 | if (out != 0) { |
| 2887 | out->toAudioPort(port); |
| 2888 | return NO_ERROR; |
| 2889 | } |
| 2890 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2891 | if (in != 0) { |
| 2892 | in->toAudioPort(port); |
| 2893 | return NO_ERROR; |
| 2894 | } |
| 2895 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2896 | } |
| 2897 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2898 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2899 | audio_patch_handle_t *handle, |
| 2900 | uid_t uid) |
| 2901 | { |
| 2902 | ALOGV("createAudioPatch()"); |
| 2903 | |
| 2904 | if (handle == NULL || patch == NULL) { |
| 2905 | return BAD_VALUE; |
| 2906 | } |
| 2907 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2908 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2909 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2910 | return BAD_VALUE; |
| 2911 | } |
| 2912 | // only one source per audio patch supported for now |
| 2913 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2914 | return INVALID_OPERATION; |
| 2915 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2916 | |
| 2917 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2918 | return INVALID_OPERATION; |
| 2919 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2920 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2921 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2922 | return INVALID_OPERATION; |
| 2923 | } |
| 2924 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2925 | |
| 2926 | sp<AudioPatch> patchDesc; |
| 2927 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2928 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2929 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2930 | patch->sources[0].role, |
| 2931 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2932 | #if LOG_NDEBUG == 0 |
| 2933 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2934 | 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] | 2935 | patch->sinks[i].role, |
| 2936 | patch->sinks[i].type); |
| 2937 | } |
| 2938 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2939 | |
| 2940 | if (index >= 0) { |
| 2941 | patchDesc = mAudioPatches.valueAt(index); |
| 2942 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2943 | mUidCached, patchDesc->mUid, uid); |
| 2944 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2945 | return INVALID_OPERATION; |
| 2946 | } |
| 2947 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2948 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
| 2951 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2952 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2953 | if (outputDesc == NULL) { |
| 2954 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2955 | return BAD_VALUE; |
| 2956 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2957 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2958 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2959 | if (patchDesc != 0) { |
| 2960 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2961 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2962 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2963 | return BAD_VALUE; |
| 2964 | } |
| 2965 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2966 | DeviceVector devices; |
| 2967 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2968 | // Only support mix to devices connection |
| 2969 | // TODO add support for mix to mix connection |
| 2970 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2971 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2972 | return INVALID_OPERATION; |
| 2973 | } |
| 2974 | sp<DeviceDescriptor> devDesc = |
| 2975 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2976 | if (devDesc == 0) { |
| 2977 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2978 | return BAD_VALUE; |
| 2979 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2980 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2981 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2982 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2983 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2984 | NULL, // updatedSamplingRate |
| 2985 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2986 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2987 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2988 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2989 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2990 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2991 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2992 | return INVALID_OPERATION; |
| 2993 | } |
| 2994 | devices.add(devDesc); |
| 2995 | } |
| 2996 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2997 | return INVALID_OPERATION; |
| 2998 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2999 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3000 | // TODO: reconfigure output format and channels here |
| 3001 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3002 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3003 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3004 | index = mAudioPatches.indexOfKey(*handle); |
| 3005 | if (index >= 0) { |
| 3006 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3007 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3008 | } |
| 3009 | patchDesc = mAudioPatches.valueAt(index); |
| 3010 | patchDesc->mUid = uid; |
| 3011 | ALOGV("createAudioPatch() success"); |
| 3012 | } else { |
| 3013 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3014 | return INVALID_OPERATION; |
| 3015 | } |
| 3016 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3017 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3018 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3019 | // only one sink supported when connecting an input device to a mix |
| 3020 | if (patch->num_sinks > 1) { |
| 3021 | return INVALID_OPERATION; |
| 3022 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3023 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3024 | if (inputDesc == NULL) { |
| 3025 | return BAD_VALUE; |
| 3026 | } |
| 3027 | if (patchDesc != 0) { |
| 3028 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3029 | return BAD_VALUE; |
| 3030 | } |
| 3031 | } |
| 3032 | sp<DeviceDescriptor> devDesc = |
| 3033 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3034 | if (devDesc == 0) { |
| 3035 | return BAD_VALUE; |
| 3036 | } |
| 3037 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3038 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3039 | devDesc->mAddress, |
| 3040 | patch->sinks[0].sample_rate, |
| 3041 | NULL, /*updatedSampleRate*/ |
| 3042 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3043 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3044 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3045 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3046 | // FIXME for the parameter type, |
| 3047 | // and the NONE |
| 3048 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3049 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3050 | return INVALID_OPERATION; |
| 3051 | } |
| 3052 | // TODO: reconfigure output format and channels here |
| 3053 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3054 | devDesc->type(), inputDesc->mIoHandle); |
| 3055 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3056 | index = mAudioPatches.indexOfKey(*handle); |
| 3057 | if (index >= 0) { |
| 3058 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3059 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3060 | } |
| 3061 | patchDesc = mAudioPatches.valueAt(index); |
| 3062 | patchDesc->mUid = uid; |
| 3063 | ALOGV("createAudioPatch() success"); |
| 3064 | } else { |
| 3065 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3066 | return INVALID_OPERATION; |
| 3067 | } |
| 3068 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3069 | // device to device connection |
| 3070 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3071 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3072 | return BAD_VALUE; |
| 3073 | } |
| 3074 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3075 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3076 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3077 | if (srcDeviceDesc == 0) { |
| 3078 | return BAD_VALUE; |
| 3079 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3080 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3081 | //update source and sink with our own data as the data passed in the patch may |
| 3082 | // be incomplete. |
| 3083 | struct audio_patch newPatch = *patch; |
| 3084 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3085 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3086 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3087 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3088 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3089 | return INVALID_OPERATION; |
| 3090 | } |
| 3091 | |
| 3092 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3093 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3094 | if (sinkDeviceDesc == 0) { |
| 3095 | return BAD_VALUE; |
| 3096 | } |
| 3097 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3098 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3099 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3100 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3101 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3102 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3103 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3104 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3105 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3106 | return INVALID_OPERATION; |
| 3107 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3108 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3109 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3110 | // if the sink device is reachable via an opened output stream, request to go via |
| 3111 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3112 | audio_io_handle_t output = selectOutput(outputs, |
| 3113 | AUDIO_OUTPUT_FLAG_NONE, |
| 3114 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3115 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3116 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3117 | if (outputDesc->isDuplicated()) { |
| 3118 | return INVALID_OPERATION; |
| 3119 | } |
| 3120 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3121 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3122 | newPatch.num_sources = 2; |
| 3123 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3124 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3125 | } |
| 3126 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3127 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3128 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3129 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3130 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3131 | status); |
| 3132 | return INVALID_OPERATION; |
| 3133 | } |
| 3134 | } else { |
| 3135 | return BAD_VALUE; |
| 3136 | } |
| 3137 | } else { |
| 3138 | return BAD_VALUE; |
| 3139 | } |
| 3140 | return NO_ERROR; |
| 3141 | } |
| 3142 | |
| 3143 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3144 | uid_t uid) |
| 3145 | { |
| 3146 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3147 | |
| 3148 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3149 | |
| 3150 | if (index < 0) { |
| 3151 | return BAD_VALUE; |
| 3152 | } |
| 3153 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3154 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3155 | mUidCached, patchDesc->mUid, uid); |
| 3156 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3157 | return INVALID_OPERATION; |
| 3158 | } |
| 3159 | |
| 3160 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3161 | patchDesc->mUid = mUidCached; |
| 3162 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3163 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3164 | if (outputDesc == NULL) { |
| 3165 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3166 | return BAD_VALUE; |
| 3167 | } |
| 3168 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3169 | setOutputDevice(outputDesc, |
| 3170 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3171 | true, |
| 3172 | 0, |
| 3173 | NULL); |
| 3174 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3175 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3176 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3177 | if (inputDesc == NULL) { |
| 3178 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3179 | return BAD_VALUE; |
| 3180 | } |
| 3181 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3182 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3183 | true, |
| 3184 | NULL); |
| 3185 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3186 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3187 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3188 | status, patchDesc->mAfPatchHandle); |
| 3189 | removeAudioPatch(patchDesc->mHandle); |
| 3190 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3191 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3192 | } else { |
| 3193 | return BAD_VALUE; |
| 3194 | } |
| 3195 | } else { |
| 3196 | return BAD_VALUE; |
| 3197 | } |
| 3198 | return NO_ERROR; |
| 3199 | } |
| 3200 | |
| 3201 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3202 | struct audio_patch *patches, |
| 3203 | unsigned int *generation) |
| 3204 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3205 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3206 | return BAD_VALUE; |
| 3207 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3208 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3209 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3210 | } |
| 3211 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3212 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3213 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3214 | ALOGV("setAudioPortConfig()"); |
| 3215 | |
| 3216 | if (config == NULL) { |
| 3217 | return BAD_VALUE; |
| 3218 | } |
| 3219 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3220 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3221 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3222 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3225 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3226 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3227 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3228 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3229 | if (outputDesc == NULL) { |
| 3230 | return BAD_VALUE; |
| 3231 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3232 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3233 | "setAudioPortConfig() called on duplicated output %d", |
| 3234 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3235 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3236 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3237 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3238 | if (inputDesc == NULL) { |
| 3239 | return BAD_VALUE; |
| 3240 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3241 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3242 | } else { |
| 3243 | return BAD_VALUE; |
| 3244 | } |
| 3245 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3246 | sp<DeviceDescriptor> deviceDesc; |
| 3247 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3248 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3249 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3250 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3251 | } else { |
| 3252 | return BAD_VALUE; |
| 3253 | } |
| 3254 | if (deviceDesc == NULL) { |
| 3255 | return BAD_VALUE; |
| 3256 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3257 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3258 | } else { |
| 3259 | return BAD_VALUE; |
| 3260 | } |
| 3261 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3262 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3263 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3264 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3265 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3266 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3267 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3268 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3269 | if (status != NO_ERROR) { |
| 3270 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3271 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3272 | |
| 3273 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3274 | } |
| 3275 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3276 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3277 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3278 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3279 | clearAudioPatches(uid); |
| 3280 | clearSessionRoutes(uid); |
| 3281 | } |
| 3282 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3283 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3284 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3285 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3286 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3287 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3288 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3289 | } |
| 3290 | } |
| 3291 | } |
| 3292 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3293 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3294 | audio_io_handle_t ouptutToSkip) |
| 3295 | { |
| 3296 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3297 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3298 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3299 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3300 | continue; |
| 3301 | } |
| 3302 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3303 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3304 | continue; |
| 3305 | } |
| 3306 | // If the default device for this strategy is on another output mix, |
| 3307 | // invalidate all tracks in this strategy to force re connection. |
| 3308 | // Otherwise select new device on the output mix. |
| 3309 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3310 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3311 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3312 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3313 | } |
| 3314 | } |
| 3315 | } else { |
| 3316 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3317 | setOutputDevice(outputDesc, newDevice, false); |
| 3318 | } |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3323 | { |
| 3324 | // remove output routes associated with this uid |
| 3325 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3326 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3327 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3328 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3329 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3330 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3331 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3336 | for (const auto& strategy : affectedStrategies) { |
| 3337 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | // remove input routes associated with this uid |
| 3341 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3342 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3343 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3344 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3345 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3346 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3347 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | // reroute inputs if necessary |
| 3352 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3353 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3354 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3355 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3356 | inputsToClose.add(inputDesc->mIoHandle); |
| 3357 | } |
| 3358 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3359 | for (const auto& input : inputsToClose) { |
| 3360 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3361 | } |
| 3362 | } |
| 3363 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3364 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3365 | { |
| 3366 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3367 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3368 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3369 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3370 | } |
| 3371 | } |
| 3372 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3373 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3374 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3375 | audio_io_handle_t *ioHandle, |
| 3376 | audio_devices_t *device) |
| 3377 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3378 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3379 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3380 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3381 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3382 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3385 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3386 | const audio_attributes_t *attributes, |
| 3387 | audio_port_handle_t *portId, |
| 3388 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3389 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3390 | ALOGV("%s", __FUNCTION__); |
| 3391 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3392 | |
| 3393 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3394 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3395 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3396 | return BAD_VALUE; |
| 3397 | } |
| 3398 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3399 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3400 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3401 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3402 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3403 | return INVALID_OPERATION; |
| 3404 | } |
| 3405 | |
| 3406 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3407 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3408 | String8(source->ext.device.address)); |
| 3409 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3410 | 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] | 3411 | return BAD_VALUE; |
| 3412 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3413 | |
| 3414 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3415 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3416 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3417 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3418 | |
| 3419 | sp<SourceClientDescriptor> sourceDesc = |
| 3420 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3421 | streamTypefromAttributesInt(attributes), |
| 3422 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3423 | |
| 3424 | status_t status = connectAudioSource(sourceDesc); |
| 3425 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3426 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3427 | } |
| 3428 | return status; |
| 3429 | } |
| 3430 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3431 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3432 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3433 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3434 | |
| 3435 | // make sure we only have one patch per source. |
| 3436 | disconnectAudioSource(sourceDesc); |
| 3437 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3438 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3439 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3440 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3441 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3442 | |
| 3443 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3444 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3445 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3446 | |
| 3447 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3448 | |
| 3449 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3450 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3451 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3452 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3453 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3454 | // create patch between src device and output device |
| 3455 | // create Hwoutput and add to mHwOutputs |
| 3456 | } else { |
| 3457 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3458 | audio_io_handle_t output = |
| 3459 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3460 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3461 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3462 | return INVALID_OPERATION; |
| 3463 | } |
| 3464 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3465 | if (outputDesc->isDuplicated()) { |
| 3466 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3467 | return INVALID_OPERATION; |
| 3468 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3469 | status_t status = outputDesc->start(); |
| 3470 | if (status != NO_ERROR) { |
| 3471 | return status; |
| 3472 | } |
| 3473 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3474 | // create a special patch with no sink and two sources: |
| 3475 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3476 | // be connected as well as the stream type for volume control |
| 3477 | // - the sink is defined by whatever output device is currently selected for the output |
| 3478 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3479 | PatchBuilder patchBuilder; |
| 3480 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3481 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3482 | &afPatchHandle, |
| 3483 | 0); |
| 3484 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3485 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3486 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3487 | if (status != NO_ERROR) { |
| 3488 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3489 | __FUNCTION__, status); |
| 3490 | return INVALID_OPERATION; |
| 3491 | } |
| 3492 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3493 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3494 | |
| 3495 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3496 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3497 | return status; |
| 3498 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3499 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3500 | if (delayMs != 0) { |
| 3501 | usleep(delayMs * 1000); |
| 3502 | } |
| 3503 | } |
| 3504 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3505 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3506 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3507 | |
| 3508 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3509 | } |
| 3510 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3511 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3512 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3513 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3514 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3515 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3516 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3517 | return BAD_VALUE; |
| 3518 | } |
| 3519 | status_t status = disconnectAudioSource(sourceDesc); |
| 3520 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3521 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3522 | return status; |
| 3523 | } |
| 3524 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3525 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3526 | { |
| 3527 | if (mMasterMono == mono) { |
| 3528 | return NO_ERROR; |
| 3529 | } |
| 3530 | mMasterMono = mono; |
| 3531 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3532 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3533 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3534 | // |
| 3535 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3536 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3537 | // play back offloaded. |
| 3538 | if (mMasterMono) { |
| 3539 | Vector<audio_io_handle_t> offloaded; |
| 3540 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3541 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3542 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3543 | offloaded.push(desc->mIoHandle); |
| 3544 | } |
| 3545 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3546 | for (const auto& handle : offloaded) { |
| 3547 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3548 | } |
| 3549 | } |
| 3550 | // update master mono for all remaining outputs |
| 3551 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3552 | updateMono(mOutputs.keyAt(i)); |
| 3553 | } |
| 3554 | return NO_ERROR; |
| 3555 | } |
| 3556 | |
| 3557 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3558 | { |
| 3559 | *mono = mMasterMono; |
| 3560 | return NO_ERROR; |
| 3561 | } |
| 3562 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3563 | float AudioPolicyManager::getStreamVolumeDB( |
| 3564 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3565 | { |
| 3566 | return computeVolume(stream, index, device); |
| 3567 | } |
| 3568 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3569 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3570 | audio_format_t *surroundFormats, |
| 3571 | bool *surroundFormatsEnabled, |
| 3572 | bool reported) |
| 3573 | { |
| 3574 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3575 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3576 | return BAD_VALUE; |
| 3577 | } |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3578 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p" |
| 3579 | " reported %d", *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3580 | |
| 3581 | // Only return value if there is HDMI output. |
| 3582 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3583 | return INVALID_OPERATION; |
| 3584 | } |
| 3585 | |
| 3586 | size_t formatsWritten = 0; |
| 3587 | size_t formatsMax = *numSurroundFormats; |
| 3588 | *numSurroundFormats = 0; |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3589 | std::unordered_set<audio_format_t> formats; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3590 | if (reported) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3591 | // Return formats from HDMI profiles, that have already been resolved by |
| 3592 | // checkOutputsForDevice(). |
| 3593 | DeviceVector hdmiOutputDevs = mAvailableOutputDevices.getDevicesFromTypeMask( |
| 3594 | AUDIO_DEVICE_OUT_HDMI); |
| 3595 | for (size_t i = 0; i < hdmiOutputDevs.size(); i++) { |
| 3596 | FormatVector supportedFormats = |
| 3597 | hdmiOutputDevs[i]->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3598 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3599 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3600 | formats.insert(supportedFormats[j]); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3601 | } else { |
| 3602 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3603 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3604 | formats.insert(pair.first); |
| 3605 | break; |
| 3606 | } |
| 3607 | } |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3608 | } |
| 3609 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3610 | } |
| 3611 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3612 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3613 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3614 | } |
| 3615 | } |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3616 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3617 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3618 | surroundFormats[formatsWritten] = format; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3619 | bool formatEnabled = false; |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3620 | if (mConfig.getSurroundFormats().count(format) == 0) { |
| 3621 | // Check sub-formats |
| 3622 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3623 | for (const auto& subformat : pair.second) { |
| 3624 | formatEnabled = mSurroundFormats.count(subformat) != 0; |
| 3625 | if (formatEnabled) break; |
| 3626 | } |
| 3627 | if (formatEnabled) break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3628 | } |
| 3629 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3630 | formatEnabled = mSurroundFormats.count(format) != 0; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3631 | } |
| 3632 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3633 | } |
| 3634 | (*numSurroundFormats)++; |
| 3635 | } |
| 3636 | return NO_ERROR; |
| 3637 | } |
| 3638 | |
| 3639 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3640 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3641 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3642 | // Check if audio format is a surround formats. |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3643 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3644 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3645 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3646 | return BAD_VALUE; |
| 3647 | } |
| 3648 | |
| 3649 | // Should only be called when MANUAL. |
| 3650 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3651 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3652 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3653 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3654 | return INVALID_OPERATION; |
| 3655 | } |
| 3656 | |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3657 | if ((mSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3658 | return NO_ERROR; |
| 3659 | } |
| 3660 | |
| 3661 | // The operation is valid only when there is HDMI output available. |
| 3662 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3663 | ALOGW("%s() no HDMI out devices found", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3664 | return INVALID_OPERATION; |
| 3665 | } |
| 3666 | |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3667 | std::unordered_set<audio_format_t> surroundFormatsBackup(mSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3668 | if (enabled) { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3669 | mSurroundFormats.insert(audioFormat); |
| 3670 | for (const auto& subFormat : formatIter->second) { |
| 3671 | mSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3672 | } |
| 3673 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3674 | mSurroundFormats.erase(audioFormat); |
| 3675 | for (const auto& subFormat : formatIter->second) { |
| 3676 | mSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3681 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3682 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3683 | AUDIO_DEVICE_OUT_HDMI); |
| 3684 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3685 | // Simulate reconnection to update enabled surround sound formats. |
| 3686 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3687 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3688 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3689 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3690 | address.c_str(), |
| 3691 | name.c_str()); |
| 3692 | if (status != NO_ERROR) { |
| 3693 | continue; |
| 3694 | } |
| 3695 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3696 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3697 | address.c_str(), |
| 3698 | name.c_str()); |
| 3699 | profileUpdated |= (status == NO_ERROR); |
| 3700 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3701 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3702 | AUDIO_DEVICE_IN_HDMI); |
| 3703 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3704 | // Simulate reconnection to update enabled surround sound formats. |
| 3705 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3706 | String8 name = hdmiInputDevices[i]->getName(); |
| 3707 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3708 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3709 | address.c_str(), |
| 3710 | name.c_str()); |
| 3711 | if (status != NO_ERROR) { |
| 3712 | continue; |
| 3713 | } |
| 3714 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3715 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3716 | address.c_str(), |
| 3717 | name.c_str()); |
| 3718 | profileUpdated |= (status == NO_ERROR); |
| 3719 | } |
| 3720 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3721 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3722 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3723 | mSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3727 | } |
| 3728 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3729 | void AudioPolicyManager::setRecordSilenced(uid_t uid, bool silenced) |
| 3730 | { |
| 3731 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3732 | |
| 3733 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3734 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3735 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3736 | RecordClientVector clients = activeDesc->clientsList(true /*activeOnly*/); |
| 3737 | for (const auto& client : clients) { |
| 3738 | if (uid == client->uid()) { |
| 3739 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3740 | } |
| 3741 | } |
| 3742 | } |
| 3743 | } |
| 3744 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3745 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3746 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3747 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3748 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3749 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3750 | if (patchDesc == 0) { |
| 3751 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3752 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3753 | return BAD_VALUE; |
| 3754 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3755 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3756 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3757 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3758 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3759 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3760 | if (status == NO_ERROR) { |
| 3761 | swOutputDesc->stop(); |
| 3762 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3763 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3764 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3765 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3766 | if (hwOutputDesc != 0) { |
| 3767 | // release patch between src device and output device |
| 3768 | // close Hwoutput and remove from mHwOutputs |
| 3769 | } else { |
| 3770 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3771 | } |
| 3772 | } |
| 3773 | return NO_ERROR; |
| 3774 | } |
| 3775 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3776 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3777 | audio_io_handle_t output, routing_strategy strategy) |
| 3778 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3779 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3780 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3781 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3782 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3783 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3784 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3785 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3786 | source = sourceDesc; |
| 3787 | break; |
| 3788 | } |
| 3789 | } |
| 3790 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3793 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3794 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3795 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3796 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3797 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3798 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3801 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3802 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3803 | static const char *kConfigLocationList[] = |
| 3804 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3805 | static const int kConfigLocationListSize = |
| 3806 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3807 | |
| 3808 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3809 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3810 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3811 | status_t ret; |
| 3812 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3813 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3814 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3815 | // A2DP offload supported but disabled: try to use special XML file |
| 3816 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3817 | } |
| 3818 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3819 | |
| 3820 | for (const char* fileName : fileNames) { |
| 3821 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3822 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3823 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3824 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3825 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3826 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3827 | return ret; |
| 3828 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3829 | } |
| 3830 | } |
| 3831 | return ret; |
| 3832 | } |
| 3833 | #endif |
| 3834 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3835 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3836 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3837 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3838 | 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] | 3839 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3840 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3841 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3842 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3843 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3844 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3845 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3846 | #else |
| 3847 | mVolumeCurves(new StreamDescriptorCollection()), |
| 3848 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
| 3849 | mDefaultOutputDevice), |
| 3850 | #endif |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3851 | mAudioPortGeneration(1), |
| 3852 | mBeaconMuteRefCount(0), |
| 3853 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3854 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3855 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3856 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3857 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3858 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3859 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3860 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3861 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3862 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3863 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3864 | { |
| 3865 | loadConfig(); |
| 3866 | initialize(); |
| 3867 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3868 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3869 | void AudioPolicyManager::loadConfig() { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3870 | #ifdef USE_XML_AUDIO_POLICY_CONF |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3871 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3872 | #else |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3873 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, getConfig()) != NO_ERROR) |
| 3874 | && (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, getConfig()) != NO_ERROR)) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3875 | #endif |
| 3876 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3877 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3878 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3879 | } |
| 3880 | |
| 3881 | status_t AudioPolicyManager::initialize() { |
| 3882 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3883 | |
| 3884 | // 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] | 3885 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3886 | if (!engineInstance) { |
| 3887 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3888 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3889 | } |
| 3890 | // Retrieve the Policy Manager Interface |
| 3891 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3892 | if (mEngine == NULL) { |
| 3893 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3894 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3895 | } |
| 3896 | mEngine->setObserver(this); |
| 3897 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3898 | if (status != NO_ERROR) { |
| 3899 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3900 | return status; |
| 3901 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3902 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3903 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3904 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3905 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3906 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3907 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3908 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3909 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3910 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3911 | continue; |
| 3912 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3913 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | // open all output streams needed to access attached devices |
| 3915 | // except for direct output streams that are only opened when they are actually |
| 3916 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3917 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3918 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3919 | if (!outProfile->canOpenNewIo()) { |
| 3920 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3921 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3922 | continue; |
| 3923 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3924 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3925 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3926 | continue; |
| 3927 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3928 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3929 | mTtsOutputAvailable = true; |
| 3930 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3931 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3932 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3933 | continue; |
| 3934 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3935 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3936 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3937 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3938 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3939 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3940 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3941 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3942 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3943 | if ((profileType & outputDeviceTypes) == 0) { |
| 3944 | continue; |
| 3945 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3946 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3947 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3948 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3949 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 3950 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3951 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3952 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3953 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3954 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3955 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3956 | |
| 3957 | if (status != NO_ERROR) { |
| 3958 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3959 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3960 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3961 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3962 | for (const auto& dev : supportedDevices) { |
| 3963 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3964 | // 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] | 3965 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3966 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3967 | } |
| 3968 | } |
| 3969 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3970 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3971 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3972 | } |
| 3973 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3974 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3975 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3976 | true, |
| 3977 | 0, |
| 3978 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3979 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3980 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3981 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3982 | // open input streams needed to access attached devices to validate |
| 3983 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3984 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3985 | if (!inProfile->canOpenNewIo()) { |
| 3986 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 3987 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 3988 | continue; |
| 3989 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3990 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3991 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3992 | continue; |
| 3993 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3994 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3995 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3996 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3997 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3998 | if ((profileType & inputDeviceTypes) == 0) { |
| 3999 | continue; |
| 4000 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4001 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4002 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4003 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4004 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4005 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4006 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4007 | : String8(""); |
| 4008 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4009 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4010 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4011 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4012 | status_t status = inputDesc->open(nullptr, |
| 4013 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4014 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4015 | AUDIO_SOURCE_MIC, |
| 4016 | AUDIO_INPUT_FLAG_NONE, |
| 4017 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4018 | |
| 4019 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4020 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4021 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4022 | // 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] | 4023 | if (index >= 0) { |
| 4024 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4025 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4026 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4027 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4028 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4029 | } |
| 4030 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4031 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4032 | } else { |
| 4033 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4034 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4035 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4036 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | // make sure all attached devices have been allocated a unique ID |
| 4040 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4041 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4042 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4043 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4044 | continue; |
| 4045 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4046 | // The device is now validated and can be appended to the available devices of the engine |
| 4047 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4048 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4049 | i++; |
| 4050 | } |
| 4051 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4052 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4053 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4054 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4055 | continue; |
| 4056 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4057 | // The device is now validated and can be appended to the available devices of the engine |
| 4058 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4059 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4060 | i++; |
| 4061 | } |
| 4062 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4063 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4064 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4065 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4066 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4067 | // If microphones address is empty, set it according to device type |
| 4068 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4069 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4070 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4071 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4072 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4073 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4074 | } |
| 4075 | } |
| 4076 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4077 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4078 | if (mPrimaryOutput == 0) { |
| 4079 | ALOGE("Failed to open primary output"); |
| 4080 | status = NO_INIT; |
| 4081 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4082 | |
| 4083 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4084 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4085 | } |
| 4086 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4087 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4088 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4090 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4091 | } |
| 4092 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4093 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4094 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4095 | mAvailableOutputDevices.clear(); |
| 4096 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4097 | mOutputs.clear(); |
| 4098 | mInputs.clear(); |
| 4099 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4100 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4101 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4102 | } |
| 4103 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4104 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4105 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4106 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4109 | // --- |
| 4110 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4111 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4112 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4113 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4114 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4115 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4116 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4117 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4118 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4119 | } |
| 4120 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4121 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4122 | { |
| 4123 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4124 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4125 | } |
| 4126 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4127 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4128 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4129 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4130 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4131 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4132 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4133 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4134 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4135 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4136 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4137 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4138 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4139 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4140 | if (devDesc != 0) { |
| 4141 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4142 | desc->mIoHandle, address.string()); |
| 4143 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4147 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4148 | audio_policy_dev_state_t state, |
| 4149 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4150 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4151 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4152 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4153 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4154 | |
| 4155 | if (audio_device_is_digital(device)) { |
| 4156 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4157 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4158 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4159 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4160 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4161 | // first list already open outputs that can be routed to this device |
| 4162 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4163 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4164 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4165 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4166 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4167 | outputs.add(mOutputs.keyAt(i)); |
| 4168 | } else { |
| 4169 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4170 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4171 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4172 | } |
| 4173 | } |
| 4174 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4175 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4176 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4177 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4178 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4179 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4180 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4181 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4182 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4183 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4184 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4185 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4186 | } |
| 4187 | } |
| 4188 | } |
| 4189 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4190 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4191 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4192 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4193 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4194 | return BAD_VALUE; |
| 4195 | } |
| 4196 | |
| 4197 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4198 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4199 | 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] | 4200 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4201 | |
| 4202 | // nothing to do if one output is already opened for this profile |
| 4203 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4204 | for (j = 0; j < outputs.size(); j++) { |
| 4205 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4206 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4207 | // matching profile: save the sample rates, format and channel masks supported |
| 4208 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4209 | if (audio_device_is_digital(device)) { |
| 4210 | devDesc->importAudioPort(profile); |
| 4211 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4212 | break; |
| 4213 | } |
| 4214 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4215 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4216 | continue; |
| 4217 | } |
| 4218 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4219 | if (!profile->canOpenNewIo()) { |
| 4220 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4221 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4222 | continue; |
| 4223 | } |
| 4224 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4225 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4226 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4227 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4228 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4229 | status_t status = desc->open(nullptr, device, address, |
| 4230 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4231 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4232 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4233 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4234 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4235 | char *param = audio_device_address_to_parameter(device, address); |
| 4236 | mpClientInterface->setParameters(output, String8(param)); |
| 4237 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4238 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4239 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4240 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4241 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4242 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4243 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4244 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4245 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4246 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4247 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4248 | profile->pickAudioProfile( |
| 4249 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4250 | config.offload_info.sample_rate = config.sample_rate; |
| 4251 | config.offload_info.channel_mask = config.channel_mask; |
| 4252 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4253 | |
| 4254 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4255 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4256 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4257 | output = AUDIO_IO_HANDLE_NONE; |
| 4258 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4259 | } |
| 4260 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4261 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4262 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4263 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4264 | sp<AudioPolicyMix> policyMix; |
| 4265 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4266 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4267 | address.string()); |
| 4268 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4269 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4270 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4271 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4272 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4273 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4274 | // no duplicated output for direct outputs and |
| 4275 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4276 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4278 | //TODO: configure audio effect output stage here |
| 4279 | |
| 4280 | // 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] | 4281 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4282 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4283 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4284 | &duplicatedOutput); |
| 4285 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4286 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4287 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4288 | } else { |
| 4289 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4290 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4291 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4292 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4293 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4294 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4295 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4296 | } |
| 4297 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4298 | } else { |
| 4299 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4300 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4301 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4302 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4303 | profiles.removeAt(profile_index); |
| 4304 | profile_index--; |
| 4305 | } else { |
| 4306 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4307 | // Load digital format info only for digital devices |
| 4308 | if (audio_device_is_digital(device)) { |
| 4309 | devDesc->importAudioPort(profile); |
| 4310 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4311 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4312 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4313 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4314 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4315 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4316 | NULL/*patch handle*/, address.string()); |
| 4317 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4318 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4319 | } |
| 4320 | } |
| 4321 | |
| 4322 | if (profiles.isEmpty()) { |
| 4323 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4324 | return BAD_VALUE; |
| 4325 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4326 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4327 | // check if one opened output is not needed any more after disconnecting one device |
| 4328 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4329 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4330 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4331 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4332 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4333 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4334 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4335 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4336 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4337 | mOutputs.keyAt(i)); |
| 4338 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4339 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4340 | } |
| 4341 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4342 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4343 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4344 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4345 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4346 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4347 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4348 | "clearing direct output profile %zu on module %s", |
| 4349 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4350 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4351 | } |
| 4352 | } |
| 4353 | } |
| 4354 | } |
| 4355 | return NO_ERROR; |
| 4356 | } |
| 4357 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4358 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4359 | audio_policy_dev_state_t state, |
| 4360 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4361 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4362 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4363 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4364 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4365 | |
| 4366 | if (audio_device_is_digital(device)) { |
| 4367 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4368 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4369 | } |
| 4370 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4371 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4372 | // first list already open inputs that can be routed to this device |
| 4373 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4374 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4375 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4376 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4377 | inputs.add(mInputs.keyAt(input_index)); |
| 4378 | } |
| 4379 | } |
| 4380 | |
| 4381 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4382 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4383 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4384 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4385 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4386 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4387 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4388 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4389 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4390 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4391 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4392 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4393 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4394 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4395 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | } |
| 4399 | |
| 4400 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4401 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4402 | return BAD_VALUE; |
| 4403 | } |
| 4404 | |
| 4405 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4406 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4407 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4408 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4409 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4410 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4411 | // nothing to do if one input is already opened for this profile |
| 4412 | size_t input_index; |
| 4413 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4414 | desc = mInputs.valueAt(input_index); |
| 4415 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4416 | if (audio_device_is_digital(device)) { |
| 4417 | devDesc->importAudioPort(profile); |
| 4418 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4419 | break; |
| 4420 | } |
| 4421 | } |
| 4422 | if (input_index != mInputs.size()) { |
| 4423 | continue; |
| 4424 | } |
| 4425 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4426 | if (!profile->canOpenNewIo()) { |
| 4427 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4428 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4429 | continue; |
| 4430 | } |
| 4431 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4432 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4433 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4434 | status_t status = desc->open(nullptr, |
| 4435 | device, |
| 4436 | address, |
| 4437 | AUDIO_SOURCE_MIC, |
| 4438 | AUDIO_INPUT_FLAG_NONE, |
| 4439 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4440 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4441 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4442 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4443 | char *param = audio_device_address_to_parameter(device, address); |
| 4444 | mpClientInterface->setParameters(input, String8(param)); |
| 4445 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4446 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4447 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4448 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4449 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4450 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4451 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4452 | } |
| 4453 | |
| 4454 | if (input != 0) { |
| 4455 | addInput(input, desc); |
| 4456 | } |
| 4457 | } // endif input != 0 |
| 4458 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4459 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4460 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4461 | profiles.removeAt(profile_index); |
| 4462 | profile_index--; |
| 4463 | } else { |
| 4464 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4465 | if (audio_device_is_digital(device)) { |
| 4466 | devDesc->importAudioPort(profile); |
| 4467 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4468 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4469 | } |
| 4470 | } // end scan profiles |
| 4471 | |
| 4472 | if (profiles.isEmpty()) { |
| 4473 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4474 | return BAD_VALUE; |
| 4475 | } |
| 4476 | } else { |
| 4477 | // Disconnect |
| 4478 | // check if one opened input is not needed any more after disconnecting one device |
| 4479 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4480 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4481 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4482 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4483 | mInputs.keyAt(input_index)); |
| 4484 | inputs.add(mInputs.keyAt(input_index)); |
| 4485 | } |
| 4486 | } |
| 4487 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4488 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4489 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4490 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4491 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4492 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4493 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4494 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4495 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4496 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4497 | } |
| 4498 | } |
| 4499 | } |
| 4500 | } // end disconnect |
| 4501 | |
| 4502 | return NO_ERROR; |
| 4503 | } |
| 4504 | |
| 4505 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4506 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4507 | { |
| 4508 | ALOGV("closeOutput(%d)", output); |
| 4509 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4510 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4511 | if (outputDesc == NULL) { |
| 4512 | ALOGW("closeOutput() unknown output %d", output); |
| 4513 | return; |
| 4514 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4515 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4516 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4517 | // look for duplicated outputs connected to the output being removed. |
| 4518 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4519 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | if (dupOutputDesc->isDuplicated() && |
| 4521 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4522 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4523 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4524 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4525 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4526 | } else { |
| 4527 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4528 | } |
| 4529 | // As all active tracks on duplicated output will be deleted, |
| 4530 | // and as they were also referenced on the other output, the reference |
| 4531 | // count for their stream type must be adjusted accordingly on |
| 4532 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4533 | const bool wasActive = outputDesc2->isActive(); |
| 4534 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4535 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4537 | // stop() will be a no op if the output is still active but is needed in case all |
| 4538 | // active streams refcounts where cleared above |
| 4539 | if (wasActive) { |
| 4540 | outputDesc2->stop(); |
| 4541 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4542 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4543 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4544 | |
| 4545 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4546 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4547 | } |
| 4548 | } |
| 4549 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4550 | nextAudioPortGeneration(); |
| 4551 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4552 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4553 | if (index >= 0) { |
| 4554 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4555 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4556 | mAudioPatches.removeItemsAt(index); |
| 4557 | mpClientInterface->onAudioPatchListUpdate(); |
| 4558 | } |
| 4559 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4560 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4561 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4562 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4563 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4564 | |
| 4565 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4566 | // no direct outputs are open. |
| 4567 | if (getMsdAudioOutDeviceTypes() != AUDIO_DEVICE_NONE) { |
| 4568 | bool directOutputOpen = false; |
| 4569 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4570 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4571 | directOutputOpen = true; |
| 4572 | break; |
| 4573 | } |
| 4574 | } |
| 4575 | if (!directOutputOpen) { |
| 4576 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4577 | setMsdPatch(); |
| 4578 | } |
| 4579 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4583 | { |
| 4584 | ALOGV("closeInput(%d)", input); |
| 4585 | |
| 4586 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4587 | if (inputDesc == NULL) { |
| 4588 | ALOGW("closeInput() unknown input %d", input); |
| 4589 | return; |
| 4590 | } |
| 4591 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4592 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4593 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4594 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4595 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4596 | if (index >= 0) { |
| 4597 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4598 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4599 | mAudioPatches.removeItemsAt(index); |
| 4600 | mpClientInterface->onAudioPatchListUpdate(); |
| 4601 | } |
| 4602 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4603 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4604 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4605 | |
| 4606 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4607 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4608 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4609 | SoundTrigger::setCaptureState(false); |
| 4610 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4611 | } |
| 4612 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4613 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4614 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4615 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4616 | { |
| 4617 | SortedVector<audio_io_handle_t> outputs; |
| 4618 | |
| 4619 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4620 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4621 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4622 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4623 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4625 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4626 | outputs.add(openOutputs.keyAt(i)); |
| 4627 | } |
| 4628 | } |
| 4629 | return outputs; |
| 4630 | } |
| 4631 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4632 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4633 | { |
| 4634 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4635 | // output is suspended before any tracks are moved to it |
| 4636 | checkA2dpSuspend(); |
| 4637 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4638 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4639 | updateDevicesAndOutputs(); |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame^] | 4640 | if (getMsdModule() != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4641 | setMsdPatch(); |
| 4642 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4645 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | { |
| 4647 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4648 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4649 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4650 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4651 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4652 | // also take into account external policy-related changes: add all outputs which are |
| 4653 | // associated with policies in the "before" and "after" output vectors |
| 4654 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4655 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4656 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4657 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4658 | srcOutputs.add(desc->mIoHandle); |
| 4659 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4660 | } |
| 4661 | } |
| 4662 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4663 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4664 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4665 | dstOutputs.add(desc->mIoHandle); |
| 4666 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4667 | } |
| 4668 | } |
| 4669 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4670 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4671 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4672 | // audio from invalidated tracks will be rendered when unmuting |
| 4673 | uint32_t maxLatency = 0; |
| 4674 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4675 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4676 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4677 | maxLatency = desc->latency(); |
| 4678 | } |
| 4679 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4680 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4681 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4682 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4683 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4684 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4685 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4686 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4687 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4688 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4689 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4690 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4691 | if (source != 0){ |
| 4692 | connectAudioSource(source); |
| 4693 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | // Move effects associated to this strategy from previous output to new output |
| 4697 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4698 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4699 | } |
| 4700 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4701 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4702 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4703 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4704 | } |
| 4705 | } |
| 4706 | } |
| 4707 | } |
| 4708 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4709 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4711 | 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] | 4712 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4713 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4714 | 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] | 4715 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4716 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4717 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4718 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4719 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4720 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4721 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4722 | } |
| 4723 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4724 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4725 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4726 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4727 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4728 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4729 | return; |
| 4730 | } |
| 4731 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4732 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4733 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4734 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4735 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4736 | |
| 4737 | // if suspended, restore A2DP output if: |
| 4738 | // ((SCO device is NOT connected) || |
| 4739 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4740 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4741 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4742 | // if not suspended, suspend A2DP output if: |
| 4743 | // (SCO device is connected) && |
| 4744 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4745 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4746 | // |
| 4747 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4748 | if (!isScoConnected || |
| 4749 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4750 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4751 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4752 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4753 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4754 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4755 | |
| 4756 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4757 | mA2dpSuspended = false; |
| 4758 | } |
| 4759 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4760 | if (isScoConnected && |
| 4761 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4762 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4763 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4764 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4765 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4766 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4767 | |
| 4768 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4769 | mA2dpSuspended = true; |
| 4770 | } |
| 4771 | } |
| 4772 | } |
| 4773 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4774 | template <class IoDescriptor, class Filter> |
| 4775 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4776 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4777 | { |
| 4778 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4779 | auto activeClientsWithRoute = |
| 4780 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4781 | active = activeClients.size() > 0; |
| 4782 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4783 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4784 | } |
| 4785 | return nullptr; |
| 4786 | } |
| 4787 | |
| 4788 | template <class IoCollection, class Filter> |
| 4789 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4790 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4791 | { |
| 4792 | sp<DeviceDescriptor> device; |
| 4793 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4794 | auto desc = ioCollection.valueAt(i); |
| 4795 | bool active; |
| 4796 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4797 | if (active && curDevice == nullptr) { |
| 4798 | return nullptr; |
| 4799 | } else if (curDevice != nullptr) { |
| 4800 | device = curDevice; |
| 4801 | } |
| 4802 | } |
| 4803 | return device; |
| 4804 | } |
| 4805 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4806 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4807 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4808 | { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4809 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4810 | if (index >= 0) { |
| 4811 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4812 | if (patchDesc->mUid != mUidCached) { |
| 4813 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4814 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4815 | return outputDesc->device(); |
| 4816 | } |
| 4817 | } |
| 4818 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4819 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4820 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4821 | bool active; // unused |
| 4822 | sp<DeviceDescriptor> deviceDesc = |
| 4823 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
| 4824 | if (deviceDesc != nullptr) { |
| 4825 | return deviceDesc->type(); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4826 | } |
| 4827 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4828 | // 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] | 4829 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | // use device for strategy enforced audible |
| 4831 | // 2: we are in call or the strategy phone is active on the output: |
| 4832 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4833 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4834 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4835 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4836 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4837 | // 5: the strategy accessibility is active on the output: |
| 4838 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4839 | // 6: the strategy "respectful" sonification is active on the output: |
| 4840 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4841 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4842 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4843 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4844 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4845 | // 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] | 4846 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4847 | |
| 4848 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4849 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4850 | // as opposed to all streams on the same audio HAL module. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4851 | audio_devices_t device = AUDIO_DEVICE_NONE; |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4852 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4853 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4854 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4855 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4856 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4857 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4858 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4859 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4860 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4861 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4862 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4863 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4864 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4865 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4866 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4867 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4868 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4869 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4870 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4871 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4872 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4873 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4874 | } |
| 4875 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4876 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4877 | return device; |
| 4878 | } |
| 4879 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4880 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4881 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4882 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4883 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4884 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4885 | if (index >= 0) { |
| 4886 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4887 | if (patchDesc->mUid != mUidCached) { |
| 4888 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4889 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4890 | return inputDesc->mDevice; |
| 4891 | } |
| 4892 | } |
| 4893 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4894 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4895 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4896 | bool active; |
| 4897 | sp<DeviceDescriptor> deviceDesc = |
| 4898 | findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4899 | if (deviceDesc != nullptr) { |
| 4900 | return deviceDesc->type(); |
| 4901 | } |
| 4902 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4903 | // If we are not in call and no client is active on this input, this methods returns |
| 4904 | // 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] | 4905 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4906 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4907 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4908 | } |
| 4909 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4910 | device = getDeviceAndMixForInputSource(source); |
| 4911 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4912 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4913 | return device; |
| 4914 | } |
| 4915 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4916 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4917 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4918 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4919 | } |
| 4920 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4921 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4922 | return (uint32_t)getStrategy(stream); |
| 4923 | } |
| 4924 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4925 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4926 | // By checking the range of stream before calling getStrategy, we avoid |
| 4927 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4928 | // 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] | 4929 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4930 | return AUDIO_DEVICE_NONE; |
| 4931 | } |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4932 | audio_devices_t activeDevices = AUDIO_DEVICE_NONE; |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4933 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4934 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4935 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4936 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4937 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4938 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4939 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4940 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4941 | devices |= curDevices; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4942 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4943 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4944 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4945 | activeDevices |= outputDesc->device(); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4946 | } |
| 4947 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4948 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4949 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4950 | // Favor devices selected on active streams if any to report correct device in case of |
| 4951 | // explicit device selection |
| 4952 | if (activeDevices != AUDIO_DEVICE_NONE) { |
| 4953 | devices = activeDevices; |
| 4954 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4955 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4956 | and doesn't really need to.*/ |
| 4957 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4958 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4959 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4960 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4961 | return devices; |
| 4962 | } |
| 4963 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4964 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4965 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4966 | 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] | 4967 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4968 | } |
| 4969 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4970 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4971 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4972 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4973 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4974 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4975 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4976 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4977 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4978 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4979 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4980 | } |
| 4981 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4982 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4983 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4984 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4985 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4986 | updateDevicesAndOutputs(); |
| 4987 | break; |
| 4988 | default: |
| 4989 | break; |
| 4990 | } |
| 4991 | } |
| 4992 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4993 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4994 | |
| 4995 | // skip beacon mute management if a dedicated TTS output is available |
| 4996 | if (mTtsOutputAvailable) { |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5000 | switch(event) { |
| 5001 | case STARTING_OUTPUT: |
| 5002 | mBeaconMuteRefCount++; |
| 5003 | break; |
| 5004 | case STOPPING_OUTPUT: |
| 5005 | if (mBeaconMuteRefCount > 0) { |
| 5006 | mBeaconMuteRefCount--; |
| 5007 | } |
| 5008 | break; |
| 5009 | case STARTING_BEACON: |
| 5010 | mBeaconPlayingRefCount++; |
| 5011 | break; |
| 5012 | case STOPPING_BEACON: |
| 5013 | if (mBeaconPlayingRefCount > 0) { |
| 5014 | mBeaconPlayingRefCount--; |
| 5015 | } |
| 5016 | break; |
| 5017 | } |
| 5018 | |
| 5019 | if (mBeaconMuteRefCount > 0) { |
| 5020 | // any playback causes beacon to be muted |
| 5021 | return setBeaconMute(true); |
| 5022 | } else { |
| 5023 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5024 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5025 | } |
| 5026 | } |
| 5027 | |
| 5028 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5029 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5030 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5031 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5032 | if (mBeaconMuted != mute) { |
| 5033 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5034 | ALOGV("\t muting %d", mute); |
| 5035 | uint32_t maxLatency = 0; |
| 5036 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5037 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5038 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5039 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5040 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5041 | const uint32_t latency = desc->latency() * 2; |
| 5042 | if (latency > maxLatency) { |
| 5043 | maxLatency = latency; |
| 5044 | } |
| 5045 | } |
| 5046 | mBeaconMuted = mute; |
| 5047 | return maxLatency; |
| 5048 | } |
| 5049 | return 0; |
| 5050 | } |
| 5051 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5052 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5053 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5054 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5055 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5056 | // case the last active client route is used |
| 5057 | sp<DeviceDescriptor> deviceDesc = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5058 | if (deviceDesc != nullptr) { |
| 5059 | return deviceDesc->type(); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5060 | } |
| 5061 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5062 | if (fromCache) { |
| 5063 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5064 | strategy, mDeviceForStrategy[strategy]); |
| 5065 | return mDeviceForStrategy[strategy]; |
| 5066 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5067 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5068 | } |
| 5069 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5070 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5071 | { |
| 5072 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5073 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5074 | } |
| 5075 | mPreviousOutputs = mOutputs; |
| 5076 | } |
| 5077 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5078 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5079 | audio_devices_t prevDevice, |
| 5080 | uint32_t delayMs) |
| 5081 | { |
| 5082 | // mute/unmute strategies using an incompatible device combination |
| 5083 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5084 | // if unmuting, unmute only after the specified delay |
| 5085 | if (outputDesc->isDuplicated()) { |
| 5086 | return 0; |
| 5087 | } |
| 5088 | |
| 5089 | uint32_t muteWaitMs = 0; |
| 5090 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5091 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5092 | |
| 5093 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5094 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5095 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5096 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5097 | bool doMute = false; |
| 5098 | |
| 5099 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5100 | doMute = true; |
| 5101 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5102 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5103 | doMute = true; |
| 5104 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5105 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5106 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5107 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5108 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5109 | // skip output if it does not share any device with current output |
| 5110 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5111 | == AUDIO_DEVICE_NONE) { |
| 5112 | continue; |
| 5113 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5114 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5115 | mute ? "muting" : "unmuting", i, curDevice); |
| 5116 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5117 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5118 | if (mute) { |
| 5119 | // FIXME: should not need to double latency if volume could be applied |
| 5120 | // immediately by the audioflinger mixer. We must account for the delay |
| 5121 | // between now and the next time the audioflinger thread for this output |
| 5122 | // will process a buffer (which corresponds to one buffer size, |
| 5123 | // usually 1/2 or 1/4 of the latency). |
| 5124 | if (muteWaitMs < desc->latency() * 2) { |
| 5125 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5126 | } |
| 5127 | } |
| 5128 | } |
| 5129 | } |
| 5130 | } |
| 5131 | } |
| 5132 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5133 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5134 | // different per device volumes |
| 5135 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5136 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5137 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5138 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5139 | if (muteWaitMs < tempMuteWaitMs) { |
| 5140 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5141 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5142 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5143 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5144 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5145 | // make sure that we do not start the temporary mute period too early in case of |
| 5146 | // delayed device change |
| 5147 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5148 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5149 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5150 | } |
| 5151 | } |
| 5152 | } |
| 5153 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5154 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5155 | if (muteWaitMs > delayMs) { |
| 5156 | muteWaitMs -= delayMs; |
| 5157 | usleep(muteWaitMs * 1000); |
| 5158 | return muteWaitMs; |
| 5159 | } |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5163 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5164 | audio_devices_t device, |
| 5165 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5166 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5167 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5168 | const char *address, |
| 5169 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5170 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5171 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5172 | AudioParameter param; |
| 5173 | uint32_t muteWaitMs; |
| 5174 | |
| 5175 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5176 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5177 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5178 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5179 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5180 | return muteWaitMs; |
| 5181 | } |
| 5182 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5183 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5184 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5185 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5186 | return 0; |
| 5187 | } |
| 5188 | |
| 5189 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5190 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5191 | |
| 5192 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5193 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5194 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5195 | |
| 5196 | if (device != AUDIO_DEVICE_NONE) { |
| 5197 | outputDesc->mDevice = device; |
| 5198 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5199 | |
| 5200 | // if the outputs are not materially active, there is no need to mute. |
| 5201 | if (requiresMuteCheck) { |
| 5202 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5203 | } else { |
| 5204 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5205 | muteWaitMs = 0; |
| 5206 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | |
| 5208 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5209 | // the requested device is AUDIO_DEVICE_NONE |
| 5210 | // OR the requested device is the same as current device |
| 5211 | // AND force is not specified |
| 5212 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5213 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5214 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5215 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5216 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5217 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5218 | return muteWaitMs; |
| 5219 | } |
| 5220 | |
| 5221 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5222 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5223 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5224 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5225 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5226 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5227 | DeviceVector deviceList; |
| 5228 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5229 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5230 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5231 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5232 | device, String8(address)); |
| 5233 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5234 | } |
| 5235 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5236 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5237 | PatchBuilder patchBuilder; |
| 5238 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5239 | 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] | 5240 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5241 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5242 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5243 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5244 | |
| 5245 | // inform all input as well |
| 5246 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5247 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5248 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5249 | AudioParameter inputCmd = AudioParameter(); |
| 5250 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5251 | inputDescriptor->mIoHandle, device); |
| 5252 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5253 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5254 | inputCmd.toString(), |
| 5255 | delayMs); |
| 5256 | } |
| 5257 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5258 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5259 | |
| 5260 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5261 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5262 | |
| 5263 | return muteWaitMs; |
| 5264 | } |
| 5265 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5266 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5267 | int delayMs, |
| 5268 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5269 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5270 | ssize_t index; |
| 5271 | if (patchHandle) { |
| 5272 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5273 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5274 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5275 | } |
| 5276 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5277 | return INVALID_OPERATION; |
| 5278 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5279 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5280 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5281 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5282 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5283 | removeAudioPatch(patchDesc->mHandle); |
| 5284 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5285 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5286 | return status; |
| 5287 | } |
| 5288 | |
| 5289 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5290 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5291 | bool force, |
| 5292 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5293 | { |
| 5294 | status_t status = NO_ERROR; |
| 5295 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5296 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5297 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5298 | inputDesc->mDevice = device; |
| 5299 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5300 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5301 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5302 | PatchBuilder patchBuilder; |
| 5303 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5304 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5305 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5306 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5307 | auto result = usecase; |
| 5308 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5309 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5310 | } |
| 5311 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5312 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5313 | addSource(deviceList.itemAt(0)); |
| 5314 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5315 | } |
| 5316 | } |
| 5317 | return status; |
| 5318 | } |
| 5319 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5320 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5321 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5322 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5323 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5324 | ssize_t index; |
| 5325 | if (patchHandle) { |
| 5326 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5327 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5328 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5329 | } |
| 5330 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5331 | return INVALID_OPERATION; |
| 5332 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5333 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5334 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5335 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5336 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5337 | removeAudioPatch(patchDesc->mHandle); |
| 5338 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5339 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5340 | return status; |
| 5341 | } |
| 5342 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5343 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5344 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5345 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5346 | audio_format_t& format, |
| 5347 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5348 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5349 | { |
| 5350 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5351 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5352 | // |
| 5353 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5354 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5355 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5356 | sp<IOProfile> firstInexact; |
| 5357 | uint32_t updatedSamplingRate = 0; |
| 5358 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5359 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5360 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5361 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5362 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5363 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5364 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5365 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5366 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5367 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5368 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5369 | &channelMask /*updatedChannelMask*/, |
| 5370 | // FIXME ugly cast |
| 5371 | (audio_output_flags_t) flags, |
| 5372 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5373 | return profile; |
| 5374 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5375 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5376 | samplingRate, |
| 5377 | &updatedSamplingRate, |
| 5378 | format, |
| 5379 | &updatedFormat, |
| 5380 | channelMask, |
| 5381 | &updatedChannelMask, |
| 5382 | // FIXME ugly cast |
| 5383 | (audio_output_flags_t) flags, |
| 5384 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5385 | firstInexact = profile; |
| 5386 | } |
| 5387 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5388 | } |
| 5389 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5390 | if (firstInexact != nullptr) { |
| 5391 | samplingRate = updatedSamplingRate; |
| 5392 | format = updatedFormat; |
| 5393 | channelMask = updatedChannelMask; |
| 5394 | return firstInexact; |
| 5395 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5396 | return NULL; |
| 5397 | } |
| 5398 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5399 | |
| 5400 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5401 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5402 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5403 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5404 | // case the last active client route is used |
| 5405 | sp<DeviceDescriptor> deviceDesc = |
| 5406 | findPreferredDevice(mInputs, inputSource, mAvailableInputDevices); |
| 5407 | if (deviceDesc != nullptr) { |
| 5408 | return deviceDesc->type(); |
| 5409 | } |
| 5410 | |
| 5411 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5412 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5413 | audio_devices_t selectedDeviceFromMix = |
| 5414 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5415 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5416 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5417 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5418 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5419 | return getDeviceForInputSource(inputSource); |
| 5420 | } |
| 5421 | |
| 5422 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5423 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5424 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5425 | } |
| 5426 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5427 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5428 | int index, |
| 5429 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5430 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5431 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5432 | |
| 5433 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5434 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5435 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5436 | // the ringtone volume |
| 5437 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5438 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5439 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5440 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5441 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5442 | } |
| 5443 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5444 | // in-call: always cap volume by voice volume + some low headroom |
| 5445 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5446 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5447 | switch (stream) { |
| 5448 | case AUDIO_STREAM_SYSTEM: |
| 5449 | case AUDIO_STREAM_RING: |
| 5450 | case AUDIO_STREAM_MUSIC: |
| 5451 | case AUDIO_STREAM_ALARM: |
| 5452 | case AUDIO_STREAM_NOTIFICATION: |
| 5453 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5454 | case AUDIO_STREAM_DTMF: |
| 5455 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5456 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5457 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5458 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5459 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5460 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5461 | if (volumeDB > maxVoiceVolDb) { |
| 5462 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5463 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5464 | volumeDB = maxVoiceVolDb; |
| 5465 | } |
| 5466 | } break; |
| 5467 | default: |
| 5468 | break; |
| 5469 | } |
| 5470 | } |
| 5471 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5472 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5473 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5474 | // - always attenuate notifications volume by 6dB |
| 5475 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5476 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5477 | // - if music is playing, always limit the volume to current music volume, |
| 5478 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5479 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5480 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5481 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5482 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5483 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5484 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5485 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5486 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5487 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5488 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5489 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5490 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5491 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5492 | // when the phone is ringing we must consider that music could have been paused just before |
| 5493 | // by the music application and behave as if music was active if the last music track was |
| 5494 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5495 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5496 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5497 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5498 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5499 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5500 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5501 | musicDevice), |
| 5502 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5503 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5504 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5505 | if (volumeDB > minVolDB) { |
| 5506 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5507 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5508 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5509 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5510 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5511 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5512 | // intended to be played |
| 5513 | if ((volumeDB > -96.0f) && |
| 5514 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5515 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5516 | stream, device, |
| 5517 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5518 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5519 | } |
| 5520 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5521 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5522 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5523 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5524 | } |
| 5525 | } |
| 5526 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5527 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5528 | } |
| 5529 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5530 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5531 | audio_stream_type_t srcStream, |
| 5532 | audio_stream_type_t dstStream) |
| 5533 | { |
| 5534 | if (srcStream == dstStream) { |
| 5535 | return srcIndex; |
| 5536 | } |
| 5537 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5538 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5539 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5540 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5541 | |
| 5542 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5543 | } |
| 5544 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5545 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5546 | int index, |
| 5547 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5548 | audio_devices_t device, |
| 5549 | int delayMs, |
| 5550 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5551 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5552 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5553 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5554 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5555 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5556 | return NO_ERROR; |
| 5557 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5558 | audio_policy_forced_cfg_t forceUseForComm = |
| 5559 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5560 | // 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] | 5561 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5562 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5563 | 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] | 5564 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5565 | return INVALID_OPERATION; |
| 5566 | } |
| 5567 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5568 | if (device == AUDIO_DEVICE_NONE) { |
| 5569 | device = outputDesc->device(); |
| 5570 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5571 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5572 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5573 | if (outputDesc->isFixedVolume(device) || |
| 5574 | // Force VoIP volume to max for bluetooth SCO |
| 5575 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5576 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5577 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5578 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5579 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5580 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5581 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5582 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5583 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5584 | float voiceVolume; |
| 5585 | // 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] | 5586 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5587 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5588 | } else { |
| 5589 | voiceVolume = 1.0; |
| 5590 | } |
| 5591 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5592 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5593 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5594 | mLastVoiceVolume = voiceVolume; |
| 5595 | } |
| 5596 | } |
| 5597 | |
| 5598 | return NO_ERROR; |
| 5599 | } |
| 5600 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5601 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5602 | audio_devices_t device, |
| 5603 | int delayMs, |
| 5604 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5605 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5606 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5607 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5608 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5609 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5610 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5611 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5612 | device, |
| 5613 | delayMs, |
| 5614 | force); |
| 5615 | } |
| 5616 | } |
| 5617 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5618 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5619 | bool on, |
| 5620 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5621 | int delayMs, |
| 5622 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5623 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5624 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5625 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5626 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5627 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5628 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5633 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5634 | bool on, |
| 5635 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5636 | int delayMs, |
| 5637 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5638 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5639 | if (device == AUDIO_DEVICE_NONE) { |
| 5640 | device = outputDesc->device(); |
| 5641 | } |
| 5642 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5643 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5644 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5645 | |
| 5646 | if (on) { |
| 5647 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5648 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5649 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5650 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5651 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5652 | } |
| 5653 | } |
| 5654 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5655 | outputDesc->mMuteCount[stream]++; |
| 5656 | } else { |
| 5657 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5658 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5659 | return; |
| 5660 | } |
| 5661 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5662 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5663 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5664 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5665 | device, |
| 5666 | delayMs); |
| 5667 | } |
| 5668 | } |
| 5669 | } |
| 5670 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5671 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5672 | { |
| 5673 | // flags to stream type mapping |
| 5674 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5675 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5676 | } |
| 5677 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5678 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5679 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5680 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5681 | return AUDIO_STREAM_TTS; |
| 5682 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5683 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5684 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5685 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5686 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5687 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5688 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5689 | // has flags that map to a strategy? |
| 5690 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5691 | return true; |
| 5692 | } |
| 5693 | |
| 5694 | // has known usage? |
| 5695 | switch (paa->usage) { |
| 5696 | case AUDIO_USAGE_UNKNOWN: |
| 5697 | case AUDIO_USAGE_MEDIA: |
| 5698 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5699 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5700 | case AUDIO_USAGE_ALARM: |
| 5701 | case AUDIO_USAGE_NOTIFICATION: |
| 5702 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5703 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5704 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5705 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5706 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5707 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5708 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5709 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5710 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5711 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5712 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5713 | break; |
| 5714 | default: |
| 5715 | return false; |
| 5716 | } |
| 5717 | return true; |
| 5718 | } |
| 5719 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5720 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5721 | routing_strategy strategy, uint32_t inPastMs, |
| 5722 | nsecs_t sysTime) const |
| 5723 | { |
| 5724 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5725 | sysTime = systemTime(); |
| 5726 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5727 | 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] | 5728 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5729 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5730 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5731 | return true; |
| 5732 | } |
| 5733 | } |
| 5734 | return false; |
| 5735 | } |
| 5736 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5737 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5738 | routing_strategy strategy, uint32_t inPastMs, |
| 5739 | nsecs_t sysTime) const |
| 5740 | { |
| 5741 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5742 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5743 | if (outputDesc->sharesHwModuleWith(desc) |
| 5744 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5745 | return true; |
| 5746 | } |
| 5747 | } |
| 5748 | return false; |
| 5749 | } |
| 5750 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5751 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5752 | { |
| 5753 | return mEngine->getForceUse(usage); |
| 5754 | } |
| 5755 | |
| 5756 | bool AudioPolicyManager::isInCall() |
| 5757 | { |
| 5758 | return isStateInCall(mEngine->getPhoneState()); |
| 5759 | } |
| 5760 | |
| 5761 | bool AudioPolicyManager::isStateInCall(int state) |
| 5762 | { |
| 5763 | return is_state_in_call(state); |
| 5764 | } |
| 5765 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5766 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5767 | { |
| 5768 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5769 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5770 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5771 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5772 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5773 | } |
| 5774 | } |
| 5775 | |
| 5776 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5777 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5778 | bool release = false; |
| 5779 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5780 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5781 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5782 | source->ext.device.type == deviceDesc->type()) { |
| 5783 | release = true; |
| 5784 | } |
| 5785 | } |
| 5786 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5787 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5788 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5789 | sink->ext.device.type == deviceDesc->type()) { |
| 5790 | release = true; |
| 5791 | } |
| 5792 | } |
| 5793 | if (release) { |
| 5794 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5795 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5796 | } |
| 5797 | } |
| 5798 | } |
| 5799 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5800 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5801 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5802 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5803 | // TODO Set this based on Config properties. |
| 5804 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5805 | |
| 5806 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5807 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5808 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5809 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5810 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5811 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5812 | formats.clear(); |
| 5813 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5814 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5815 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5816 | // Always enable IEC61937 when in MANUAL mode. |
| 5817 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5818 | } else { // NEVER, AUTO or ALWAYS |
| 5819 | // Analyze original support for various formats. |
| 5820 | bool supportsAC3 = false; |
| 5821 | bool supportsOtherSurround = false; |
| 5822 | bool supportsIEC61937 = false; |
| 5823 | mSurroundFormats.clear(); |
| 5824 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5825 | audio_format_t format = formats[formatIndex]; |
| 5826 | switch (format) { |
| 5827 | case AUDIO_FORMAT_AC3: |
| 5828 | supportsAC3 = true; |
| 5829 | break; |
| 5830 | case AUDIO_FORMAT_E_AC3: |
| 5831 | case AUDIO_FORMAT_DTS: |
| 5832 | case AUDIO_FORMAT_DTS_HD: |
| 5833 | // If ALWAYS, remove all other surround formats here |
| 5834 | // since we will add them later. |
| 5835 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5836 | formats.removeAt(formatIndex); |
| 5837 | formatIndex--; |
| 5838 | } |
| 5839 | supportsOtherSurround = true; |
| 5840 | break; |
| 5841 | case AUDIO_FORMAT_IEC61937: |
| 5842 | supportsIEC61937 = true; |
| 5843 | break; |
| 5844 | default: |
| 5845 | break; |
| 5846 | } |
| 5847 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5848 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5849 | // Modify formats based on surround preferences. |
| 5850 | // If NEVER, remove support for surround formats. |
| 5851 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5852 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5853 | // Remove surround sound related formats. |
| 5854 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5855 | audio_format_t format = formats[formatIndex]; |
| 5856 | switch(format) { |
| 5857 | case AUDIO_FORMAT_AC3: |
| 5858 | case AUDIO_FORMAT_E_AC3: |
| 5859 | case AUDIO_FORMAT_DTS: |
| 5860 | case AUDIO_FORMAT_DTS_HD: |
| 5861 | case AUDIO_FORMAT_IEC61937: |
| 5862 | formats.removeAt(formatIndex); |
| 5863 | break; |
| 5864 | default: |
| 5865 | formatIndex++; // keep it |
| 5866 | break; |
| 5867 | } |
| 5868 | } |
| 5869 | supportsAC3 = false; |
| 5870 | supportsOtherSurround = false; |
| 5871 | supportsIEC61937 = false; |
| 5872 | } |
| 5873 | } else { // AUTO or ALWAYS |
| 5874 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5875 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5876 | && !supportsAC3) { |
| 5877 | formats.add(AUDIO_FORMAT_AC3); |
| 5878 | supportsAC3 = true; |
| 5879 | } |
| 5880 | |
| 5881 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5882 | // This assumes that if any of these formats are reported by the HAL |
| 5883 | // then the report is valid and should not be modified. |
| 5884 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5885 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5886 | formats.add(AUDIO_FORMAT_DTS); |
| 5887 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5888 | supportsOtherSurround = true; |
| 5889 | } |
| 5890 | |
| 5891 | // Add support for IEC61937 if any raw surround supported. |
| 5892 | // The HAL could do this but add it here, just in case. |
| 5893 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5894 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5895 | supportsIEC61937 = true; |
| 5896 | } |
| 5897 | |
| 5898 | // Add reported surround sound formats to enabled surround formats. |
| 5899 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5900 | audio_format_t format = formats[formatIndex]; |
| 5901 | switch(format) { |
| 5902 | case AUDIO_FORMAT_AC3: |
| 5903 | case AUDIO_FORMAT_E_AC3: |
| 5904 | case AUDIO_FORMAT_DTS: |
| 5905 | case AUDIO_FORMAT_DTS_HD: |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5906 | case AUDIO_FORMAT_AAC_LC: |
| 5907 | case AUDIO_FORMAT_DOLBY_TRUEHD: |
| 5908 | case AUDIO_FORMAT_E_AC3_JOC: |
| 5909 | mSurroundFormats.insert(format); |
Andy Hung | 320fd85 | 2018-10-09 14:06:37 -0700 | [diff] [blame] | 5910 | break; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5911 | default: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5912 | break; |
| 5913 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5914 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5915 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5916 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | // Modify the list of channel masks supported. |
| 5920 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5921 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5922 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5923 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5924 | |
| 5925 | // If NEVER, then remove support for channelMasks > stereo. |
| 5926 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5927 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5928 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5929 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5930 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5931 | channelMasks.removeAt(maskIndex); |
| 5932 | } else { |
| 5933 | maskIndex++; |
| 5934 | } |
| 5935 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5936 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5937 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5938 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5939 | bool supports5dot1 = false; |
| 5940 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5941 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5942 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5943 | supports5dot1 = true; |
| 5944 | break; |
| 5945 | } |
| 5946 | } |
| 5947 | // If not then add 5.1 support. |
| 5948 | if (!supports5dot1) { |
| 5949 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5950 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5951 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5952 | } |
| 5953 | } |
| 5954 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5955 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5956 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5957 | AudioProfileVector &profiles) |
| 5958 | { |
| 5959 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5960 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5961 | // Format MUST be checked first to update the list of AudioProfile |
| 5962 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5963 | reply = mpClientInterface->getParameters( |
| 5964 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5965 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5966 | AudioParameter repliedParameters(reply); |
| 5967 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5968 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5969 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5970 | return; |
| 5971 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5972 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5973 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5974 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5975 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5976 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5977 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5978 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5979 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5980 | ChannelsVector channelMasks; |
| 5981 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5982 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5983 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5984 | |
| 5985 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5986 | reply = mpClientInterface->getParameters( |
| 5987 | ioHandle, |
| 5988 | requestedParameters.toString() + ";" + |
| 5989 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5990 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5991 | AudioParameter repliedParameters(reply); |
| 5992 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5993 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5994 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5995 | } |
| 5996 | } |
| 5997 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5998 | reply = mpClientInterface->getParameters(ioHandle, |
| 5999 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6000 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6001 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6002 | AudioParameter repliedParameters(reply); |
| 6003 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6004 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6005 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6006 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6007 | filterSurroundChannelMasks(&channelMasks); |
| 6008 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6009 | } |
| 6010 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6011 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6012 | } |
| 6013 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6014 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6015 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6016 | audio_patch_handle_t *patchHandle, |
| 6017 | AudioIODescriptorInterface *ioDescriptor, |
| 6018 | const struct audio_patch *patch, |
| 6019 | int delayMs) |
| 6020 | { |
| 6021 | ssize_t index = mAudioPatches.indexOfKey( |
| 6022 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6023 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6024 | sp<AudioPatch> patchDesc; |
| 6025 | status_t status = installPatch( |
| 6026 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6027 | if (status == NO_ERROR) { |
| 6028 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6029 | } |
| 6030 | return status; |
| 6031 | } |
| 6032 | |
| 6033 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6034 | ssize_t index, |
| 6035 | audio_patch_handle_t *patchHandle, |
| 6036 | const struct audio_patch *patch, |
| 6037 | int delayMs, |
| 6038 | uid_t uid, |
| 6039 | sp<AudioPatch> *patchDescPtr) |
| 6040 | { |
| 6041 | sp<AudioPatch> patchDesc; |
| 6042 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6043 | if (index >= 0) { |
| 6044 | patchDesc = mAudioPatches.valueAt(index); |
| 6045 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6046 | } |
| 6047 | |
| 6048 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6049 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6050 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6051 | if (status == NO_ERROR) { |
| 6052 | if (index < 0) { |
| 6053 | patchDesc = new AudioPatch(patch, uid); |
| 6054 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6055 | } else { |
| 6056 | patchDesc->mPatch = *patch; |
| 6057 | } |
| 6058 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6059 | if (patchHandle) { |
| 6060 | *patchHandle = patchDesc->mHandle; |
| 6061 | } |
| 6062 | nextAudioPortGeneration(); |
| 6063 | mpClientInterface->onAudioPatchListUpdate(); |
| 6064 | } |
| 6065 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6066 | return status; |
| 6067 | } |
| 6068 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6069 | } // namespace android |