Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 18 | |
| 19 | // Need to keep the log statements even in production builds |
| 20 | // to enable VERBOSE logging dynamically. |
| 21 | // You can enable VERBOSE logging as follows: |
| 22 | // adb shell setprop log.tag.APM_AudioPolicyManager V |
| 23 | #define LOG_NDEBUG 0 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 24 | |
| 25 | //#define VERY_VERBOSE_LOGGING |
| 26 | #ifdef VERY_VERBOSE_LOGGING |
| 27 | #define ALOGVV ALOGV |
| 28 | #else |
| 29 | #define ALOGVV(a...) do { } while(0) |
| 30 | #endif |
| 31 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 32 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 |
| 33 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 34 | #define AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME \ |
| 35 | "audio_policy_configuration_a2dp_offload_disabled.xml" |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 36 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 37 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 38 | #include <math.h> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 39 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 40 | #include <vector> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 42 | #include <AudioPolicyManagerInterface.h> |
| 43 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 45 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 46 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 47 | #include <media/AudioPolicyHelper.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 49 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 50 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 51 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 52 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 53 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 54 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 55 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 56 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 57 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 58 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 59 | //FIXME: workaround for truncated touch sounds |
| 60 | // to be removed when the problem is handled by system UI |
| 61 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 62 | |
| 63 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 64 | // media / notification / system volume. |
| 65 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 66 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 67 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 68 | static const std::vector<audio_format_t> compressedFormatsOrder = {{ |
| 69 | AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 70 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 71 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
| 72 | static const std::vector<audio_channel_mask_t> surroundChannelMasksOrder = {{ |
| 73 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 74 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 75 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 76 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 77 | // ---------------------------------------------------------------------------- |
| 78 | // AudioPolicyInterface implementation |
| 79 | // ---------------------------------------------------------------------------- |
| 80 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 81 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 82 | audio_policy_dev_state_t state, |
| 83 | const char *device_address, |
| 84 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 85 | { |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 86 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); |
| 87 | nextAudioPortGeneration(); |
| 88 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 89 | } |
| 90 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 91 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 92 | audio_policy_dev_state_t state, |
| 93 | const String8 &device_address) |
| 94 | { |
| 95 | AudioParameter param(device_address); |
| 96 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 97 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 98 | param.addInt(key, device); |
| 99 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 100 | } |
| 101 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 102 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 103 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 104 | const char *device_address, |
| 105 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 106 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 107 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 108 | device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 109 | |
| 110 | // connect/disconnect only 1 device at a time |
| 111 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 112 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 113 | sp<DeviceDescriptor> devDesc = |
| 114 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 115 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 116 | // handle output devices |
| 117 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 118 | SortedVector <audio_io_handle_t> outputs; |
| 119 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 120 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 121 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 122 | // save a copy of the opened output descriptors before any output is opened or closed |
| 123 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 124 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 125 | switch (state) |
| 126 | { |
| 127 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 128 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 129 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 130 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 131 | return INVALID_OPERATION; |
| 132 | } |
| 133 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 134 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 135 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 136 | index = mAvailableOutputDevices.add(devDesc); |
| 137 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 138 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 139 | if (module == 0) { |
| 140 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 141 | device); |
| 142 | mAvailableOutputDevices.remove(devDesc); |
| 143 | return INVALID_OPERATION; |
| 144 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 145 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 146 | } else { |
| 147 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 148 | } |
| 149 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 150 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 151 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 152 | broadcastDeviceConnectionState(device, state, devDesc->address()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 153 | |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 154 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->address()) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 155 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 156 | |
| 157 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 158 | devDesc->address()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 159 | return INVALID_OPERATION; |
| 160 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 161 | // Propagate device availability to Engine |
| 162 | mEngine->setDeviceConnectionState(devDesc, state); |
| 163 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 164 | // outputs should never be empty here |
| 165 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 166 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 167 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 168 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 169 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 170 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 171 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 172 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 173 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 174 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 175 | return INVALID_OPERATION; |
| 176 | } |
| 177 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 178 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 179 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 180 | // Send Disconnect to HALs |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 181 | broadcastDeviceConnectionState(device, state, devDesc->address()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 182 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 184 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 185 | |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 186 | checkOutputsForDevice(devDesc, state, outputs, devDesc->address()); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 187 | |
| 188 | // Propagate device availability to Engine |
| 189 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 190 | } break; |
| 191 | |
| 192 | default: |
| 193 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 194 | return BAD_VALUE; |
| 195 | } |
| 196 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 197 | checkForDeviceAndOutputChanges([&]() { |
| 198 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 199 | if (!outputs.isEmpty()) { |
| 200 | for (audio_io_handle_t output : outputs) { |
| 201 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 202 | // close unused outputs after device disconnection or direct outputs that have been |
| 203 | // opened by checkOutputsForDevice() to query dynamic parameters |
| 204 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 205 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 206 | (desc->mDirectOpenCount == 0))) { |
| 207 | closeOutput(output); |
| 208 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 209 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 210 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 211 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 212 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 213 | return false; |
| 214 | }); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 215 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 216 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 217 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 218 | updateCallRouting(newDevice); |
| 219 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 220 | const audio_devices_t msdOutDevice = getModuleDeviceTypes( |
| 221 | mAvailableOutputDevices, AUDIO_HARDWARE_MODULE_ID_MSD); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 222 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 223 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 224 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 225 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 226 | // do not force device change on duplicated output because if device is 0, it will |
| 227 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 228 | // a valid device selection on those outputs. |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 229 | bool force = (msdOutDevice == AUDIO_DEVICE_NONE || msdOutDevice != desc->device()) |
| 230 | && !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 231 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 232 | // always force when disconnecting (a non-duplicated device) |
| 233 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 234 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 235 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 238 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 239 | cleanUpForDevice(devDesc); |
| 240 | } |
| 241 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 242 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 243 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 244 | } // end if is output device |
| 245 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 246 | // handle input devices |
| 247 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 248 | SortedVector <audio_io_handle_t> inputs; |
| 249 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 250 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 251 | switch (state) |
| 252 | { |
| 253 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 254 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 255 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 256 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 257 | return INVALID_OPERATION; |
| 258 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 259 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 260 | if (module == NULL) { |
| 261 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 262 | device); |
| 263 | return INVALID_OPERATION; |
| 264 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 265 | |
| 266 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 267 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 268 | broadcastDeviceConnectionState(device, state, devDesc->address()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 269 | |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 270 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->address()) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 271 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 272 | devDesc->address()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 273 | return INVALID_OPERATION; |
| 274 | } |
| 275 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 276 | index = mAvailableInputDevices.add(devDesc); |
| 277 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 278 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 279 | } else { |
| 280 | return NO_MEMORY; |
| 281 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 282 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 283 | // Propagate device availability to Engine |
| 284 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 285 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 286 | |
| 287 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 288 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 289 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 290 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 291 | return INVALID_OPERATION; |
| 292 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 293 | |
| 294 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 295 | |
| 296 | // Set Disconnect to HALs |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 297 | broadcastDeviceConnectionState(device, state, devDesc->address()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 298 | |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 299 | checkInputsForDevice(devDesc, state, inputs, devDesc->address()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 300 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 301 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 302 | // Propagate device availability to Engine |
| 303 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 304 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | |
| 306 | default: |
| 307 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 308 | return BAD_VALUE; |
| 309 | } |
| 310 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 311 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 312 | // As the input device list can impact the output device selection, update |
| 313 | // getDeviceForStrategy() cache |
| 314 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 315 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 316 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 317 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 318 | updateCallRouting(newDevice); |
| 319 | } |
| 320 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 321 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 322 | cleanUpForDevice(devDesc); |
| 323 | } |
| 324 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 325 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 326 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 327 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 328 | |
| 329 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 330 | return BAD_VALUE; |
| 331 | } |
| 332 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 333 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 334 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 335 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 336 | sp<DeviceDescriptor> devDesc = |
| 337 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 338 | (strlen(device_address) != 0)/*matchAddress*/); |
| 339 | |
| 340 | if (devDesc == 0) { |
| 341 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 342 | device, device_address); |
| 343 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 344 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 345 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 346 | DeviceVector *deviceVector; |
| 347 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 348 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 349 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 350 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 351 | deviceVector = &mAvailableInputDevices; |
| 352 | } else { |
| 353 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 354 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 355 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 356 | |
| 357 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 358 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 361 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 362 | const char *device_address, |
| 363 | const char *device_name) |
| 364 | { |
| 365 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 366 | String8 reply; |
| 367 | AudioParameter param; |
| 368 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 369 | |
| 370 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 371 | device, device_address, device_name); |
| 372 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 373 | // connect/disconnect only 1 device at a time |
| 374 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 375 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 376 | // Check if the device is currently connected |
| 377 | sp<DeviceDescriptor> devDesc = |
| 378 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 379 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 380 | if (index < 0) { |
| 381 | // Nothing to do: device is not connected |
| 382 | return NO_ERROR; |
| 383 | } |
| 384 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 385 | // For offloaded A2DP, Hw modules may have the capability to |
| 386 | // configure codecs. Check if any of the loaded hw modules |
| 387 | // supports this. |
| 388 | // If supported, send a set parameter to configure A2DP codecs |
| 389 | // and return. No need to toggle device state. |
| 390 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 391 | reply = mpClientInterface->getParameters( |
| 392 | AUDIO_IO_HANDLE_NONE, |
| 393 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 394 | AudioParameter repliedParameters(reply); |
| 395 | repliedParameters.getInt( |
| 396 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 397 | if (isReconfigA2dpSupported) { |
| 398 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 399 | param.add(key, String8("true")); |
| 400 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 401 | return NO_ERROR; |
| 402 | } |
| 403 | } |
| 404 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 405 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 406 | // This will force reading again the device configuration |
| 407 | status = setDeviceConnectionState(device, |
| 408 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 409 | device_address, device_name); |
| 410 | if (status != NO_ERROR) { |
| 411 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 412 | status); |
| 413 | return status; |
| 414 | } |
| 415 | |
| 416 | status = setDeviceConnectionState(device, |
| 417 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 418 | device_address, device_name); |
| 419 | if (status != NO_ERROR) { |
| 420 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 421 | status); |
| 422 | return status; |
| 423 | } |
| 424 | |
| 425 | return NO_ERROR; |
| 426 | } |
| 427 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 428 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 429 | { |
| 430 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 431 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 432 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 433 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 434 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 435 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 436 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 437 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 438 | |
| 439 | // release existing RX patch if any |
| 440 | if (mCallRxPatch != 0) { |
| 441 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 442 | mCallRxPatch.clear(); |
| 443 | } |
| 444 | // release TX patch if any |
| 445 | if (mCallTxPatch != 0) { |
| 446 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 447 | mCallTxPatch.clear(); |
| 448 | } |
| 449 | |
| 450 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 451 | // via setOutputDevice() on primary output. |
| 452 | // Otherwise, create two audio patches for TX and RX path. |
| 453 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 454 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 455 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 456 | // of it due to legacy implementation. If not, create a patch. |
| 457 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 458 | == AUDIO_DEVICE_NONE) { |
| 459 | createTxPatch = true; |
| 460 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 461 | } else { // create RX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 462 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 463 | createTxPatch = true; |
| 464 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 465 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 466 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 467 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 468 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 469 | return muteWaitMs; |
| 470 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 471 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 472 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
| 473 | bool isRx, audio_devices_t device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 474 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 475 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 476 | sp<DeviceDescriptor> txSourceDeviceDesc; |
| 477 | if (isRx) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 478 | patchBuilder.addSink(findDevice(mAvailableOutputDevices, device)). |
| 479 | addSource(findDevice(mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 480 | } else { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 481 | patchBuilder.addSource(txSourceDeviceDesc = findDevice(mAvailableInputDevices, device)). |
| 482 | addSink(findDevice(mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX; |
| 486 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 487 | audio_io_handle_t output = selectOutput(outputs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 488 | // request to reuse existing output stream if one is already opened to reach the target device |
| 489 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 490 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 491 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 492 | "%s() %#x device output %d is duplicated", __func__, outputDevice, output); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 493 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 497 | // terminate active capture if on the same HW module as the call TX source device |
| 498 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 499 | // call TX device but this information is not in the audio patch and logic here must be |
| 500 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 501 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 502 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 503 | closeActiveClients(activeDesc); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 504 | } |
| 505 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 506 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 507 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 508 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 509 | status_t status = mpClientInterface->createAudioPatch( |
| 510 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 511 | ALOGW_IF(status != NO_ERROR, |
| 512 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 513 | sp<AudioPatch> audioPatch; |
| 514 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 515 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 516 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 517 | audioPatch->mUid = mUidCached; |
| 518 | } |
| 519 | return audioPatch; |
| 520 | } |
| 521 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 522 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 523 | const DeviceVector& devices, audio_devices_t device) const { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 524 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 525 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 526 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 527 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 530 | audio_devices_t AudioPolicyManager::getModuleDeviceTypes( |
| 531 | const DeviceVector& devices, const char *moduleId) const { |
| 532 | sp<HwModule> mod = mHwModules.getModuleFromName(moduleId); |
| 533 | return mod != 0 ? devices.getDeviceTypesFromHwModule(mod->getHandle()) : AUDIO_DEVICE_NONE; |
| 534 | } |
| 535 | |
| 536 | bool AudioPolicyManager::isDeviceOfModule( |
| 537 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 538 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 539 | if (module != 0) { |
| 540 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 541 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 542 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 543 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 544 | } |
| 545 | return false; |
| 546 | } |
| 547 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 548 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 549 | { |
| 550 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 551 | // store previous phone state for management of sonification strategy below |
| 552 | int oldState = mEngine->getPhoneState(); |
| 553 | |
| 554 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 555 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | return; |
| 557 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 558 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 559 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 561 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 562 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | } |
| 564 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 565 | /** |
| 566 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 567 | * routing command. |
| 568 | */ |
| 569 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 570 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 | |
| 572 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 573 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 574 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 575 | int delayMs = 0; |
| 576 | if (isStateInCall(state)) { |
| 577 | nsecs_t sysTime = systemTime(); |
| 578 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 579 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 580 | // mute media and sonification strategies and delay device switch by the largest |
| 581 | // latency of any output where either strategy is active. |
| 582 | // 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] | 583 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 584 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 585 | sysTime) || |
| 586 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 587 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 588 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 589 | (delayMs < (int)desc->latency()*2)) { |
| 590 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 592 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 593 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 594 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 595 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 596 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 597 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 598 | } |
| 599 | } |
| 600 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 601 | if (hasPrimaryOutput()) { |
| 602 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 603 | // the device returned is not necessarily reachable via this output |
| 604 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 605 | // force routing command to audio hardware when ending call |
| 606 | // even if no device change is needed |
| 607 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 608 | rxDevice = mPrimaryOutput->device(); |
| 609 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 610 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 611 | if (state == AUDIO_MODE_IN_CALL) { |
| 612 | updateCallRouting(rxDevice, delayMs); |
| 613 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 614 | if (mCallRxPatch != 0) { |
| 615 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 616 | mCallRxPatch.clear(); |
| 617 | } |
| 618 | if (mCallTxPatch != 0) { |
| 619 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 620 | mCallTxPatch.clear(); |
| 621 | } |
| 622 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 623 | } else { |
| 624 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 625 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 626 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 627 | |
| 628 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 629 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 630 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 631 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 632 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 633 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | if (isStateInCall(state)) { |
| 638 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 639 | // force reevaluating accessibility routing when call starts |
| 640 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // 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] | 644 | if (state == AUDIO_MODE_RINGTONE && |
| 645 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 646 | mLimitRingtoneVolume = true; |
| 647 | } else { |
| 648 | mLimitRingtoneVolume = false; |
| 649 | } |
| 650 | } |
| 651 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 652 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 653 | return mEngine->getPhoneState(); |
| 654 | } |
| 655 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 656 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 657 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 658 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 659 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 660 | if (config == mEngine->getForceUse(usage)) { |
| 661 | return; |
| 662 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 663 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 664 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 665 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 666 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 667 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 668 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 669 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 670 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 671 | |
| 672 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 673 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 674 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 675 | //FIXME: workaround for truncated touch sounds |
| 676 | // to be removed when the problem is handled by system UI |
| 677 | uint32_t delayMs = 0; |
| 678 | uint32_t waitMs = 0; |
| 679 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 680 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 681 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 682 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 683 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 684 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 685 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 686 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 687 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 688 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 689 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 690 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 691 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 692 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 694 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 698 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 699 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 700 | // 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] | 701 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 702 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 703 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 704 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 705 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 706 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 707 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 710 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 711 | { |
| 712 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 713 | } |
| 714 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 715 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 716 | // search to profiles for direct outputs. |
| 717 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
| 718 | audio_devices_t device, |
| 719 | uint32_t samplingRate, |
| 720 | audio_format_t format, |
| 721 | audio_channel_mask_t channelMask, |
| 722 | audio_output_flags_t flags, |
| 723 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 724 | { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 725 | if (directOnly) { |
| 726 | // only retain flags that will drive the direct output profile selection |
| 727 | // if explicitly requested |
| 728 | static const uint32_t kRelevantFlags = |
| 729 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 730 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
| 731 | flags = |
| 732 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 733 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 734 | |
| 735 | sp<IOProfile> profile; |
| 736 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 737 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 738 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 739 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 740 | samplingRate, NULL /*updatedSamplingRate*/, |
| 741 | format, NULL /*updatedFormat*/, |
| 742 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 743 | flags)) { |
| 744 | continue; |
| 745 | } |
| 746 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 747 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 748 | continue; |
| 749 | } |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 750 | if (!directOnly) return curProfile; |
| 751 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 752 | // to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 753 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 754 | continue; |
| 755 | } |
| 756 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 757 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 758 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 759 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 760 | } |
| 761 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 762 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 765 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 766 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 767 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 768 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 769 | |
| 770 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 771 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 772 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 773 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 774 | // and AudioSystem::getOutputSamplingRate(). |
| 775 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 776 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 777 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 778 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 779 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 780 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 783 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 784 | const audio_attributes_t *srcAttr, |
| 785 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 786 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 787 | if (srcAttr != NULL) { |
| 788 | if (!isValidAttributes(srcAttr)) { |
| 789 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 790 | __func__, |
| 791 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 792 | srcAttr->tags); |
| 793 | return BAD_VALUE; |
| 794 | } |
| 795 | *dstAttr = *srcAttr; |
| 796 | } else { |
| 797 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 798 | ALOGE("%s: invalid stream type", __func__); |
| 799 | return BAD_VALUE; |
| 800 | } |
| 801 | stream_type_to_audio_attributes(srcStream, dstAttr); |
| 802 | } |
| 803 | return NO_ERROR; |
| 804 | } |
| 805 | |
| 806 | status_t AudioPolicyManager::getOutputForAttrInt(audio_attributes_t *resultAttr, |
| 807 | audio_io_handle_t *output, |
| 808 | audio_session_t session, |
| 809 | const audio_attributes_t *attr, |
| 810 | audio_stream_type_t *stream, |
| 811 | uid_t uid, |
| 812 | const audio_config_t *config, |
| 813 | audio_output_flags_t *flags, |
| 814 | audio_port_handle_t *selectedDeviceId) |
| 815 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 816 | DeviceVector outputDevices; |
| 817 | routing_strategy strategy; |
| 818 | audio_devices_t device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 819 | const audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 820 | audio_devices_t msdDevice = |
| 821 | getModuleDeviceTypes(mAvailableOutputDevices, AUDIO_HARDWARE_MODULE_ID_MSD); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 822 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 823 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 824 | if (status != NO_ERROR) { |
| 825 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 826 | } |
| 827 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 828 | ALOGV("%s usage=%d, content=%d, tag=%s flags=%08x" |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 829 | " session %d selectedDeviceId %d", |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 830 | __func__, |
| 831 | resultAttr->usage, resultAttr->content_type, resultAttr->tags, resultAttr->flags, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 832 | session, requestedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 833 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 834 | *stream = streamTypefromAttributesInt(resultAttr); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 835 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 836 | strategy = getStrategyForAttr(resultAttr); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 837 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 838 | // First check for explicit routing (eg. setPreferredDevice) |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 839 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 840 | sp<DeviceDescriptor> deviceDesc = |
| 841 | mAvailableOutputDevices.getDeviceFromId(requestedDeviceId); |
| 842 | device = deviceDesc->type(); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 843 | } else { |
| 844 | // If no explict route, is there a matching dynamic policy that applies? |
| 845 | sp<SwAudioOutputDescriptor> desc; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 846 | if (mPolicyMixes.getOutputForAttr(*resultAttr, uid, desc) == NO_ERROR) { |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 847 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 848 | if (!audio_has_proportional_frames(config->format)) { |
| 849 | return BAD_VALUE; |
| 850 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 851 | *stream = streamTypefromAttributesInt(resultAttr); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 852 | *output = desc->mIoHandle; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 853 | AudioMix *mix = desc->mPolicyMix; |
| 854 | sp<DeviceDescriptor> deviceDesc = |
| 855 | mAvailableOutputDevices.getDevice(mix->mDeviceType, mix->mDeviceAddress); |
| 856 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 857 | ALOGV("%s returns output %d", __func__, *output); |
| 858 | return NO_ERROR; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | // Virtual sources must always be dynamicaly or explicitly routed |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 862 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 863 | ALOGW("%s no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE", __func__); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 864 | return BAD_VALUE; |
| 865 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 866 | device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 867 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 868 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 869 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 870 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 873 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 874 | // chosen by the engine if not. |
| 875 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 876 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 877 | // TODO: Remove check of AUDIO_STREAM_MUSIC once migration is completed on the app side. |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 878 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 879 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 880 | audio_is_linear_pcm(config->format) && |
| 881 | isInCall()) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 882 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 883 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 884 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 885 | // 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] | 886 | device = mEngine->getDeviceForStrategy(strategy); |
| 887 | } |
| 888 | } |
| 889 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 890 | ALOGV("%s device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 891 | "flags %#x", |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 892 | __func__, device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 893 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 894 | *output = AUDIO_IO_HANDLE_NONE; |
| 895 | if (msdDevice != AUDIO_DEVICE_NONE) { |
| 896 | *output = getOutputForDevice(msdDevice, session, *stream, config, flags); |
| 897 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(device) == NO_ERROR) { |
| 898 | ALOGV("%s() Using MSD device 0x%x instead of device 0x%x", |
| 899 | __func__, msdDevice, device); |
| 900 | device = msdDevice; |
| 901 | } else { |
| 902 | *output = AUDIO_IO_HANDLE_NONE; |
| 903 | } |
| 904 | } |
| 905 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 906 | *output = getOutputForDevice(device, session, *stream, config, flags); |
| 907 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 908 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 909 | return INVALID_OPERATION; |
| 910 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 911 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 912 | outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 913 | *selectedDeviceId = getFirstDeviceId(outputDevices); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 914 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 915 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 916 | |
| 917 | return NO_ERROR; |
| 918 | } |
| 919 | |
| 920 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 921 | audio_io_handle_t *output, |
| 922 | audio_session_t session, |
| 923 | audio_stream_type_t *stream, |
| 924 | uid_t uid, |
| 925 | const audio_config_t *config, |
| 926 | audio_output_flags_t *flags, |
| 927 | audio_port_handle_t *selectedDeviceId, |
| 928 | audio_port_handle_t *portId) |
| 929 | { |
| 930 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 931 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 932 | return INVALID_OPERATION; |
| 933 | } |
| 934 | const audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
| 935 | audio_attributes_t resultAttr; |
| 936 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
| 937 | config, flags, selectedDeviceId); |
| 938 | if (status != NO_ERROR) { |
| 939 | return status; |
| 940 | } |
| 941 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 942 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 943 | .format = config->format, |
| 944 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 945 | *portId = AudioPort::getNextUniqueId(); |
| 946 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 947 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 948 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 949 | requestedDeviceId, *stream, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 950 | getStrategyForAttr(&resultAttr), |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 951 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 952 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 953 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 954 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 955 | ALOGV("%s returns output %d selectedDeviceId %d for port ID %d", |
| 956 | __func__, *output, requestedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 957 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 958 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 962 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 963 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 964 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 965 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 966 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 967 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 968 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 969 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 970 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 971 | // open a direct output if required by specified parameters |
| 972 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 973 | // and all common behaviors are driven by checking only the direct flag |
| 974 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 975 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 976 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 977 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 978 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 979 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 980 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 981 | // only allow deep buffering for music stream type |
| 982 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 983 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 984 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 985 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 986 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 987 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 988 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 989 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 990 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 991 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 992 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 993 | audio_is_linear_pcm(config->format) && |
| 994 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 995 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 996 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 997 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 998 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 999 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1000 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1001 | sp<IOProfile> profile; |
| 1002 | |
| 1003 | // 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] | 1004 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1005 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1006 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1007 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1008 | goto non_direct_output; |
| 1009 | } |
| 1010 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1011 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1012 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1013 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1014 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1015 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1016 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1017 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1018 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1019 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1020 | profile = getProfileForOutput(device, |
| 1021 | config->sample_rate, |
| 1022 | config->format, |
| 1023 | config->channel_mask, |
| 1024 | (audio_output_flags_t)*flags, |
| 1025 | true /* directOnly */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1028 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1029 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1030 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1031 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1032 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1033 | // reuse direct output if currently open by the same client |
| 1034 | // and configured with same parameters |
| 1035 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1036 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1037 | (config->channel_mask == desc->mChannelMask) && |
| 1038 | (session == desc->mDirectClientSession)) { |
| 1039 | desc->mDirectOpenCount++; |
| 1040 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 1041 | mOutputs.keyAt(i), session); |
| 1042 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1046 | |
| 1047 | if (!profile->canOpenNewIo()) { |
| 1048 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1050 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1051 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1052 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1053 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1054 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 1055 | String8 address = getFirstDeviceAddress(outputDevices); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1056 | |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 1057 | // MSD patch may be using the only output stream that can service this request. Release |
| 1058 | // MSD patch to prioritize this request over any active output on MSD. |
| 1059 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 1060 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1061 | const auto& patch = msdPatches[i]; |
| 1062 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1063 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1064 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 1065 | (sink->ext.device.type & device) != AUDIO_DEVICE_NONE && |
| 1066 | (address.isEmpty() || strncmp(sink->ext.device.address, address.string(), |
| 1067 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 1068 | releaseAudioPatch(patch->mHandle, mUidCached); |
| 1069 | break; |
| 1070 | } |
| 1071 | } |
| 1072 | } |
| 1073 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1074 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1075 | |
| 1076 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1077 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1078 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1079 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1080 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1081 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1082 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1083 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1084 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1085 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1086 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1087 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1088 | // 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] | 1089 | if (audio_is_linear_pcm(config->format) && |
| 1090 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1091 | goto non_direct_output; |
| 1092 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1093 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1094 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1095 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1096 | outputDesc->mDirectClientSession = session; |
| 1097 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1098 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1099 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1100 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1101 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1102 | return output; |
| 1103 | } |
| 1104 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1105 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1106 | |
| 1107 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1108 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1109 | 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] | 1110 | return AUDIO_IO_HANDLE_NONE; |
| 1111 | } |
| 1112 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1113 | // ignoring channel mask due to downmix capability in mixer |
| 1114 | |
| 1115 | // open a non direct output |
| 1116 | |
| 1117 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1118 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1119 | // get which output is suitable for the specified stream. The actual |
| 1120 | // routing change will happen when startOutput() will be called |
| 1121 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1122 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1123 | // 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] | 1124 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1125 | output = selectOutput(outputs, *flags, config->format, |
| 1126 | config->channel_mask, config->sample_rate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1127 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1128 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1129 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1130 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1131 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1132 | return output; |
| 1133 | } |
| 1134 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1135 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1136 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1137 | if (msdModule != 0) { |
| 1138 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1139 | msdModule->getHandle()); |
| 1140 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1141 | } |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1145 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1146 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1147 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1148 | if (msdModule != 0) { |
| 1149 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1150 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1151 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1152 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1153 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1154 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1155 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1156 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | return msdPatches; |
| 1161 | } |
| 1162 | |
| 1163 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1164 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1165 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1166 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1167 | if (msdModule == nullptr) { |
| 1168 | ALOGE("%s() unable to get MSD module", __func__); |
| 1169 | return NO_INIT; |
| 1170 | } |
| 1171 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1172 | if (deviceModule == nullptr) { |
| 1173 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1174 | return NO_INIT; |
| 1175 | } |
| 1176 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1177 | if (inputProfiles.isEmpty()) { |
| 1178 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1179 | return NO_INIT; |
| 1180 | } |
| 1181 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1182 | if (outputProfiles.isEmpty()) { |
| 1183 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1184 | return NO_INIT; |
| 1185 | } |
| 1186 | AudioProfileVector msdProfiles; |
| 1187 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1188 | for (const auto &inProfile : inputProfiles) { |
| 1189 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1190 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1191 | } |
| 1192 | } |
| 1193 | AudioProfileVector deviceProfiles; |
| 1194 | for (const auto &outProfile : outputProfiles) { |
| 1195 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1196 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1197 | } |
| 1198 | } |
| 1199 | struct audio_config_base bestSinkConfig; |
| 1200 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1201 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1202 | &bestSinkConfig); |
| 1203 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1204 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1205 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1206 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1207 | } |
| 1208 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1209 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1210 | sinkConfig->format = bestSinkConfig.format; |
| 1211 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1212 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1213 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1214 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1215 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1216 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1217 | sourceConfig->format = bestSinkConfig.format; |
| 1218 | // Copy input stream directly without any processing (e.g. resampling). |
| 1219 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1220 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1221 | if (hwAvSync) { |
| 1222 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1223 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1224 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1225 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1226 | } |
| 1227 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1228 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1229 | sinkConfig->config_mask |= config_mask; |
| 1230 | sourceConfig->config_mask |= config_mask; |
| 1231 | return NO_ERROR; |
| 1232 | } |
| 1233 | |
| 1234 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1235 | { |
| 1236 | PatchBuilder patchBuilder; |
| 1237 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1238 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1239 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1240 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1241 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1242 | // For now, we just forcefully try with HwAvSync first. |
| 1243 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1244 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1245 | getBestMsdAudioProfileFor( |
| 1246 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1247 | if (res == NO_ERROR) { |
| 1248 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1249 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1250 | } |
| 1251 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1252 | " supporting PCM format conversion.", __func__); |
| 1253 | return patchBuilder; |
| 1254 | } |
| 1255 | |
| 1256 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1257 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1258 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1259 | // Use media strategy for unspecified output device. This should only |
| 1260 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1261 | // therefore invalidate explicit routing requests. |
| 1262 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1263 | } |
| 1264 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1265 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1266 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1267 | if (!msdPatches.isEmpty()) { |
| 1268 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1269 | "The current MSD prototype only supports one output patch"); |
| 1270 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1271 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1272 | return NO_ERROR; |
| 1273 | } |
| 1274 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1275 | } |
| 1276 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1277 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1278 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1279 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1280 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1281 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1282 | return status; |
| 1283 | } |
| 1284 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1285 | 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] | 1286 | audio_output_flags_t flags, |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1287 | audio_format_t format, |
| 1288 | audio_channel_mask_t channelMask, |
| 1289 | uint32_t samplingRate) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1290 | { |
| 1291 | // select one output among several that provide a path to a particular device or set of |
| 1292 | // devices (the list was previously build by getOutputsForDevice()). |
| 1293 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1294 | // 1: the output supporting haptic playback when requesting haptic playback |
| 1295 | // 2: the output with the highest number of requested policy flags |
| 1296 | // 3: the output with the bit depth the closest to the requested one |
| 1297 | // 4: the primary output |
| 1298 | // 5: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 | |
| 1300 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1301 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1302 | } |
| 1303 | if (outputs.size() == 1) { |
| 1304 | return outputs[0]; |
| 1305 | } |
| 1306 | |
| 1307 | int maxCommonFlags = 0; |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1308 | const size_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1309 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1310 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1311 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1312 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1313 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1314 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1316 | for (audio_io_handle_t output : outputs) { |
| 1317 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1318 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1319 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1320 | continue; |
| 1321 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1322 | // If haptic channel is specified, use the haptic output if present. |
| 1323 | // When using haptic output, same audio format and sample rate are required. |
| 1324 | if (hapticChannelCount > 0) { |
| 1325 | // If haptic channel is specified, use the first output that |
| 1326 | // support haptic playback. |
| 1327 | if (audio_channel_count_from_out_mask( |
| 1328 | outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) >= hapticChannelCount |
| 1329 | && format == outputDesc->mFormat |
| 1330 | && samplingRate == outputDesc->mSamplingRate) { |
| 1331 | return output; |
| 1332 | } |
| 1333 | } else { |
| 1334 | // When haptic channel is not specified, skip haptic output. |
| 1335 | if (outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 1336 | continue; |
| 1337 | } |
| 1338 | } |
| 1339 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1340 | // if a valid format is specified, skip output if not compatible |
| 1341 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1342 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1343 | continue; |
| 1344 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1345 | if (AudioPort::isBetterFormatMatch( |
| 1346 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1347 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1348 | bestFormat = outputDesc->mFormat; |
| 1349 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1350 | } |
| 1351 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1352 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1353 | if (commonFlags >= maxCommonFlags) { |
| 1354 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1355 | if (format != AUDIO_FORMAT_INVALID |
| 1356 | && AudioPort::isBetterFormatMatch( |
| 1357 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1358 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1359 | bestFormatForFlags = outputDesc->mFormat; |
| 1360 | } |
| 1361 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1362 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1363 | maxCommonFlags = commonFlags; |
| 1364 | bestFormatForFlags = outputDesc->mFormat; |
| 1365 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1366 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1367 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1368 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1369 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | } |
| 1373 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1374 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1375 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1376 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1377 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1378 | return outputForFormat; |
| 1379 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1380 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1381 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | return outputs[0]; |
| 1385 | } |
| 1386 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1387 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1388 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1389 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1390 | |
| 1391 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1392 | if (outputDesc == 0) { |
| 1393 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1394 | return BAD_VALUE; |
| 1395 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1396 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1397 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1398 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1399 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1400 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1401 | status_t status = outputDesc->start(); |
| 1402 | if (status != NO_ERROR) { |
| 1403 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1404 | } |
| 1405 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1406 | uint32_t delayMs; |
| 1407 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1408 | |
| 1409 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1410 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1411 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1412 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1413 | if (delayMs != 0) { |
| 1414 | usleep(delayMs * 1000); |
| 1415 | } |
| 1416 | |
| 1417 | return status; |
| 1418 | } |
| 1419 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1420 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1421 | const sp<TrackClientDescriptor>& client, |
| 1422 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1423 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1424 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1425 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1426 | |
| 1427 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1428 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1429 | if (stream == AUDIO_STREAM_TTS) { |
| 1430 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1431 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1432 | return INVALID_OPERATION; |
| 1433 | } else { |
| 1434 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1435 | } |
| 1436 | } else { |
| 1437 | // some playback other than beacon starts |
| 1438 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1439 | } |
| 1440 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1441 | // 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] | 1442 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1443 | bool force = !outputDesc->isActive() && |
| 1444 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1445 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1446 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 1447 | AudioMix *policyMix = NULL; |
| 1448 | const char *address = NULL; |
| 1449 | if (outputDesc->mPolicyMix != NULL) { |
| 1450 | policyMix = outputDesc->mPolicyMix; |
| 1451 | address = policyMix->mDeviceAddress.string(); |
| 1452 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1453 | device = policyMix->mDeviceType; |
| 1454 | } else { |
| 1455 | device = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1456 | } |
| 1457 | } |
| 1458 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1459 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1460 | // It covers a common case when there is no materially active audio |
| 1461 | // and muting would result in unnecessary delay and dropped audio. |
| 1462 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1463 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1464 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1465 | // increment usage count for this stream on the requested output: |
| 1466 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1467 | // 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] | 1468 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1469 | |
| 1470 | if (client->hasPreferredDevice(true)) { |
| 1471 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 1472 | if (device != outputDesc->device()) { |
| 1473 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1474 | } |
| 1475 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1476 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1477 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1478 | selectOutputForMusicEffects(); |
| 1479 | } |
| 1480 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1481 | if (outputDesc->streamActiveCount(stream) == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1482 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1483 | if (device == AUDIO_DEVICE_NONE) { |
| 1484 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1485 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1486 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1487 | routing_strategy strategy = getStrategy(stream); |
| 1488 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1489 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1490 | (beaconMuteLatency > 0); |
| 1491 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1492 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1493 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1494 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1495 | // An output has a shared device if |
| 1496 | // - managed by the same hw module |
| 1497 | // - supports the currently selected device |
| 1498 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1499 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1500 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1501 | // force a device change if any other output is: |
| 1502 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1503 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1504 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1505 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1506 | // 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] | 1507 | // change the device currently selected by the other output. |
| 1508 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1509 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1510 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1511 | force = true; |
| 1512 | } |
| 1513 | // 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] | 1514 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1515 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1516 | const uint32_t latencyMs = desc->latency(); |
| 1517 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1518 | |
| 1519 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1520 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1521 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1522 | |
| 1523 | // Require mute check if another output is on a shared device |
| 1524 | // and currently active to have proper drain and avoid pops. |
| 1525 | // Note restoring AudioTracks onto this output needs to invoke |
| 1526 | // a volume ramp if there is no mute. |
| 1527 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1528 | } |
| 1529 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1530 | |
| 1531 | const uint32_t muteWaitMs = |
| 1532 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1533 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1534 | // apply volume rules for current stream and device if necessary |
| 1535 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1536 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1537 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1538 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1539 | |
| 1540 | // update the outputs if starting an output with a stream that can affect notification |
| 1541 | // routing |
| 1542 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1543 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1544 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1545 | if (strategy == STRATEGY_SONIFICATION) { |
| 1546 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1547 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1548 | |
| 1549 | if (waitMs > muteWaitMs) { |
| 1550 | *delayMs = waitMs - muteWaitMs; |
| 1551 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1552 | |
| 1553 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1554 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1555 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1556 | // change occurs after the MixerThread starts and causes a stream volume |
| 1557 | // glitch. |
| 1558 | // |
| 1559 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1560 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1561 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1562 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1563 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1564 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1565 | } |
| 1566 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1567 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1568 | // of type MIX_TYPE_RECORDERS |
| 1569 | if (audio_is_remote_submix_device(device) && policyMix != NULL && |
| 1570 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1571 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1572 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1573 | address, |
| 1574 | "remote-submix"); |
| 1575 | } |
| 1576 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1577 | return NO_ERROR; |
| 1578 | } |
| 1579 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1580 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1581 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1582 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1583 | |
| 1584 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1585 | if (outputDesc == 0) { |
| 1586 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1587 | return BAD_VALUE; |
| 1588 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1589 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1590 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1591 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1592 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1593 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1594 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1595 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1596 | if (status == NO_ERROR ) { |
| 1597 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1598 | } |
| 1599 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1602 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1603 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1604 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1605 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1606 | audio_stream_type_t stream = client->stream(); |
| 1607 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1608 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1609 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1610 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1611 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1612 | // Automatically disable the remote submix input when output is stopped on a |
| 1613 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1614 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1615 | outputDesc->mPolicyMix != NULL && |
| 1616 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1617 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1618 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1619 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1620 | "remote-submix"); |
| 1621 | } |
| 1622 | } |
| 1623 | bool forceDeviceUpdate = false; |
| 1624 | if (client->hasPreferredDevice(true)) { |
| 1625 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1626 | forceDeviceUpdate = true; |
| 1627 | } |
| 1628 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1629 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1630 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1631 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1632 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1633 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1634 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1635 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1636 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1637 | // the track stop() command is received and at that time the audio track buffer can |
| 1638 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1639 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1640 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1641 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1642 | |
| 1643 | // force restoring the device selection on other active outputs if it differs from the |
| 1644 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1645 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1646 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1647 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1648 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1649 | desc->isActive() && |
| 1650 | outputDesc->sharesHwModuleWith(desc) && |
| 1651 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1652 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1653 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1654 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1655 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1656 | newDevice2, |
| 1657 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1658 | delayMs); |
| 1659 | // re-apply device specific volume if not done by setOutputDevice() |
| 1660 | if (!force) { |
| 1661 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1662 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1666 | handleNotificationRoutingForStream(stream); |
| 1667 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1668 | |
| 1669 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1670 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1671 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1672 | } |
| 1673 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1674 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1675 | selectOutputForMusicEffects(); |
| 1676 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1677 | return NO_ERROR; |
| 1678 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1679 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1680 | return INVALID_OPERATION; |
| 1681 | } |
| 1682 | } |
| 1683 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1684 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1685 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1686 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1687 | |
| 1688 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1689 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1690 | // If an output descriptor is closed due to a device routing change, |
| 1691 | // then there are race conditions with releaseOutput from tracks |
| 1692 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1693 | // destroyed shortly thereafter. |
| 1694 | // |
| 1695 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1696 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1697 | return; |
| 1698 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1699 | |
| 1700 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1701 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1702 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1703 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1704 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1705 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1706 | return; |
| 1707 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1708 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1709 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1710 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1711 | } |
| 1712 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1713 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1714 | // otherwise there may be inaccurate stream reference counts. |
| 1715 | // This is checked in outputDesc->removeClient below. |
| 1716 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1719 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1720 | audio_io_handle_t *input, |
| 1721 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1722 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1723 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1724 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1725 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1726 | input_type_t *inputType, |
| 1727 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1728 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1729 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1730 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1731 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1732 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1733 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1734 | // handle legacy remote submix case where the address was not always specified |
| 1735 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1736 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1737 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1738 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1739 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1740 | sp<AudioInputDescriptor> inputDesc; |
| 1741 | sp<RecordClientDescriptor> clientDesc; |
| 1742 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1743 | bool isSoundTrigger; |
| 1744 | audio_devices_t device; |
| 1745 | |
| 1746 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1747 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1748 | return INVALID_OPERATION; |
| 1749 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1750 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1751 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1752 | inputSource = AUDIO_SOURCE_MIC; |
| 1753 | } |
| 1754 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1755 | // Explicit routing? |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 1756 | sp<DeviceDescriptor> deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1757 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1758 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1759 | // possible |
| 1760 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1761 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1762 | ssize_t index = mInputs.indexOfKey(*input); |
| 1763 | if (index < 0) { |
| 1764 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1765 | status = BAD_VALUE; |
| 1766 | goto error; |
| 1767 | } |
| 1768 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1769 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1770 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1771 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1772 | status = BAD_VALUE; |
| 1773 | goto error; |
| 1774 | } |
| 1775 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1776 | // 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] | 1777 | // corresponds to a new client and is only permitted from the same UID. |
| 1778 | // 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] | 1779 | if (clients.size() > 1) { |
| 1780 | for (const auto& client : clients) { |
| 1781 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1782 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1783 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1784 | // to an actual client |
| 1785 | if (client == *clients.cbegin()) { |
| 1786 | continue; |
| 1787 | } |
| 1788 | if (uid != client->uid() && !client->isSilenced()) { |
| 1789 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1790 | uid, client->portId(), client->uid()); |
| 1791 | status = INVALID_OPERATION; |
| 1792 | goto error; |
| 1793 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1794 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1795 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1796 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1797 | device = inputDesc->mDevice; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1798 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1799 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1800 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | *input = AUDIO_IO_HANDLE_NONE; |
| 1804 | *inputType = API_INPUT_INVALID; |
| 1805 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1806 | halInputSource = inputSource; |
| 1807 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1808 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1809 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1810 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1811 | if (status != NO_ERROR) { |
| 1812 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1813 | } |
| 1814 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1815 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1816 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1817 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1818 | if (deviceDesc != 0) { |
| 1819 | device = deviceDesc->type(); |
| 1820 | } else { |
| 1821 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
| 1822 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1823 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1824 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1825 | status = BAD_VALUE; |
| 1826 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1827 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1828 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1829 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1830 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1831 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1832 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1833 | // know about it and is therefore considered "legacy" |
| 1834 | *inputType = API_INPUT_LEGACY; |
| 1835 | } else { |
| 1836 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1837 | // an external policy |
| 1838 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1839 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1840 | } else if (audio_is_remote_submix_device(device)) { |
| 1841 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1842 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1843 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1844 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1845 | } else { |
| 1846 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1847 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1848 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1849 | } |
| 1850 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1851 | *input = getInputForDevice(device, address, session, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1852 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1853 | policyMix); |
| 1854 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1855 | status = INVALID_OPERATION; |
| 1856 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1857 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1858 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1859 | exit: |
| 1860 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1861 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 1862 | *selectedDeviceId = getFirstDeviceId(inputDevices); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1863 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1864 | isSoundTrigger = inputSource == AUDIO_SOURCE_HOTWORD && |
| 1865 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1866 | *portId = AudioPort::getNextUniqueId(); |
| 1867 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1868 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1869 | *attr, *config, requestedDeviceId, |
| 1870 | inputSource,flags, isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1871 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1872 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1873 | |
| 1874 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1875 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1876 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1877 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1878 | |
| 1879 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1880 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
| 1883 | |
| 1884 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1885 | String8 address, |
| 1886 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1887 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1888 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1889 | audio_input_flags_t flags, |
| 1890 | AudioMix *policyMix) |
| 1891 | { |
| 1892 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1893 | audio_source_t halInputSource = inputSource; |
| 1894 | bool isSoundTrigger = false; |
| 1895 | |
| 1896 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1897 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1898 | if (index >= 0) { |
| 1899 | input = mSoundTriggerSessions.valueFor(session); |
| 1900 | isSoundTrigger = true; |
| 1901 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1902 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1903 | } else { |
| 1904 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1905 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1906 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1907 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1908 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1911 | // find a compatible input profile (not necessarily identical in parameters) |
| 1912 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1913 | // sampling rate and flags may be updated by getInputProfile |
| 1914 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1915 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1916 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1917 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1918 | audio_input_flags_t profileFlags = flags; |
| 1919 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1920 | 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] | 1921 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1922 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1923 | profileFlags); |
| 1924 | if (profile != 0) { |
| 1925 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1926 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1927 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1928 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1929 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1930 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1931 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1932 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1933 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1934 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1935 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1936 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1937 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1938 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1939 | if (samplingRate == 0) { |
| 1940 | samplingRate = profileSamplingRate; |
| 1941 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1942 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1943 | if (profile->getModuleHandle() == 0) { |
| 1944 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1945 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1948 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1949 | for (size_t i = 0; i < mInputs.size(); ) { |
| 1950 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 1951 | if (desc->mProfile != profile) { |
| 1952 | continue; |
| 1953 | } |
| 1954 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 1955 | // else |
| 1956 | // reuse input if active client app is not in IDLE state |
| 1957 | // |
| 1958 | RecordClientVector clients = desc->clientsList(); |
| 1959 | bool doClose = false; |
| 1960 | for (const auto& client : clients) { |
| 1961 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 1962 | continue; |
| 1963 | } |
| 1964 | if (client->isSoundTrigger()) { |
| 1965 | if (session == client->session()) { |
| 1966 | return desc->mIoHandle; |
| 1967 | } |
| 1968 | continue; |
| 1969 | } |
| 1970 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 1971 | return desc->mIoHandle; |
| 1972 | } |
| 1973 | doClose = true; |
| 1974 | } |
| 1975 | if (doClose) { |
| 1976 | closeInput(desc->mIoHandle); |
| 1977 | } else { |
| 1978 | i++; |
| 1979 | } |
| 1980 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1983 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1984 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1985 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1986 | lConfig.sample_rate = profileSamplingRate; |
| 1987 | lConfig.channel_mask = profileChannelMask; |
| 1988 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1989 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1990 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1991 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1992 | // the inputs vector must be of size >= 1, but we don't want to crash here |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 1993 | address = getFirstDeviceAddress(inputDevices); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1994 | } |
| 1995 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1996 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1997 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1998 | |
| 1999 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2000 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2001 | (profileSamplingRate != lConfig.sample_rate) || |
| 2002 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2003 | (profileChannelMask != lConfig.channel_mask)) { |
| 2004 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2005 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2006 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2007 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2008 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2009 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2010 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2013 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2014 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2015 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2016 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2017 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2018 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2021 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2022 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2023 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2024 | |
| 2025 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2026 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2027 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2028 | return BAD_VALUE; |
| 2029 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2030 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2031 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2032 | if (client->active()) { |
| 2033 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2034 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2035 | } |
| 2036 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2037 | audio_session_t session = client->session(); |
| 2038 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2039 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2040 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2041 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2042 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2043 | status_t status = inputDesc->start(); |
| 2044 | if (status != NO_ERROR) { |
| 2045 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2046 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2047 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2048 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2049 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2050 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2051 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2052 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2053 | // primary HW module |
| 2054 | audio_devices_t device = getNewInputDevice(inputDesc); |
| 2055 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2057 | if (inputDesc->activeCount() == 1) { |
| 2058 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2059 | if ((inputDesc->mPolicyMix != NULL) |
| 2060 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2061 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2062 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2063 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2064 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2065 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2066 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2067 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2068 | SoundTrigger::setCaptureState(true); |
| 2069 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2070 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2071 | // automatically enable the remote submix output when input is started if not |
| 2072 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2073 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2074 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2075 | String8 address = String8(""); |
| 2076 | if (inputDesc->mPolicyMix == NULL) { |
| 2077 | address = String8("0"); |
| 2078 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2079 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2080 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2081 | if (address != "") { |
| 2082 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2083 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2084 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2085 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2086 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2087 | } |
| 2088 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2089 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2090 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2091 | return NO_ERROR; |
| 2092 | } |
| 2093 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2094 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2095 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2096 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2097 | |
| 2098 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2099 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2100 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2101 | return BAD_VALUE; |
| 2102 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2103 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2104 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2105 | if (!client->active()) { |
| 2106 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2107 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2110 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2111 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2112 | inputDesc->stop(); |
| 2113 | if (inputDesc->isActive()) { |
| 2114 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2115 | } else { |
| 2116 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2117 | if ((inputDesc->mPolicyMix != NULL) |
| 2118 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2119 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2120 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2121 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2122 | |
| 2123 | // automatically disable the remote submix output when input is stopped if not |
| 2124 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2125 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2126 | String8 address = String8(""); |
| 2127 | if (inputDesc->mPolicyMix == NULL) { |
| 2128 | address = String8("0"); |
| 2129 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2130 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2131 | } |
| 2132 | if (address != "") { |
| 2133 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2134 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2135 | address, "remote-submix"); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | audio_devices_t device = inputDesc->mDevice; |
| 2140 | resetInputDevice(input); |
| 2141 | |
| 2142 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2143 | // primary HW module |
| 2144 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2145 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2146 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2147 | SoundTrigger::setCaptureState(false); |
| 2148 | } |
| 2149 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2150 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2151 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2154 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2155 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2156 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2157 | |
| 2158 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2159 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2160 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2161 | return; |
| 2162 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2163 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2164 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2165 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2166 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2167 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2168 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2169 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2170 | if (inputDesc->getClientCount() > 0) { |
| 2171 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2172 | return; |
| 2173 | } |
| 2174 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2175 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2176 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2177 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2178 | } |
| 2179 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2180 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2181 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2182 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2183 | |
| 2184 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2185 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2189 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2190 | { |
| 2191 | stopInput(portId); |
| 2192 | releaseInput(portId); |
| 2193 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2194 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2195 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2196 | bool patchRemoved = false; |
| 2197 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2198 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2199 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2200 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2201 | if (patch_index >= 0) { |
| 2202 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2203 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2204 | mAudioPatches.removeItemsAt(patch_index); |
| 2205 | patchRemoved = true; |
| 2206 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2207 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2208 | } |
| 2209 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2210 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2211 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2212 | |
| 2213 | if (patchRemoved) { |
| 2214 | mpClientInterface->onAudioPatchListUpdate(); |
| 2215 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2218 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2219 | int indexMin, |
| 2220 | int indexMax) |
| 2221 | { |
| 2222 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2223 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2224 | |
| 2225 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2226 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2227 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2228 | continue; |
| 2229 | } |
| 2230 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2231 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2234 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2235 | int index, |
| 2236 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2237 | { |
| 2238 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2239 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2240 | // app that has MODIFY_PHONE_STATE permission. |
| 2241 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2242 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2243 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2244 | return BAD_VALUE; |
| 2245 | } |
| 2246 | if (!audio_is_output_device(device)) { |
| 2247 | return BAD_VALUE; |
| 2248 | } |
| 2249 | |
| 2250 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2251 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2252 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2253 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2254 | stream, device, index); |
| 2255 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2256 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2257 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2258 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2259 | continue; |
| 2260 | } |
| 2261 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2262 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2263 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2264 | // update volume on all outputs and streams matching the following: |
| 2265 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2266 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2267 | // the requested device |
| 2268 | // - For non default requested device, currently selected device on the output is either the |
| 2269 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2270 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2271 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2272 | status_t status = NO_ERROR; |
| 2273 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2274 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2275 | audio_devices_t curDevice = desc->device(); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2276 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2277 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2278 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2279 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2280 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2281 | continue; |
| 2282 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2283 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2284 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2285 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2286 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2287 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2288 | continue; |
| 2289 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2290 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2291 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2292 | curStreamDevice |= device; |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2293 | applyVolume = (Volume::getDeviceForVolume(curDevice) & curStreamDevice) != 0; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2294 | } else { |
| 2295 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2296 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2297 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2298 | // rescale index before applying to curStream as ranges may be different for |
| 2299 | // stream and curStream |
| 2300 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2301 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2302 | //FIXME: workaround for truncated touch sounds |
| 2303 | // delayed volume change for system stream to be removed when the problem is |
| 2304 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2305 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2306 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2307 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2308 | if (volStatus != NO_ERROR) { |
| 2309 | status = volStatus; |
| 2310 | } |
| 2311 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2312 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2313 | } |
| 2314 | return status; |
| 2315 | } |
| 2316 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2317 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2318 | int *index, |
| 2319 | audio_devices_t device) |
| 2320 | { |
| 2321 | if (index == NULL) { |
| 2322 | return BAD_VALUE; |
| 2323 | } |
| 2324 | if (!audio_is_output_device(device)) { |
| 2325 | return BAD_VALUE; |
| 2326 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2327 | // 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] | 2328 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2329 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2330 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2331 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2332 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2333 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2334 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2335 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2336 | return NO_ERROR; |
| 2337 | } |
| 2338 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2339 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2340 | { |
| 2341 | // select one output among several suitable for global effects. |
| 2342 | // The priority is as follows: |
| 2343 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2344 | // AudioFlinger will invalidate the track and the offloaded output |
| 2345 | // will be closed causing the effect to be moved to a PCM output. |
| 2346 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2347 | // 3: The primary output |
| 2348 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2349 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2350 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2351 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2352 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2353 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2354 | if (outputs.size() == 0) { |
| 2355 | return AUDIO_IO_HANDLE_NONE; |
| 2356 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2357 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2358 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2359 | bool activeOnly = true; |
| 2360 | |
| 2361 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2362 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2363 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2364 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2365 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2366 | for (audio_io_handle_t output : outputs) { |
| 2367 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2368 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2369 | continue; |
| 2370 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2371 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2372 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2373 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2374 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2375 | } |
| 2376 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2377 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2378 | } |
| 2379 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2380 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2381 | } |
| 2382 | } |
| 2383 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2384 | output = outputOffloaded; |
| 2385 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2386 | output = outputDeepBuffer; |
| 2387 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2388 | output = outputPrimary; |
| 2389 | } else { |
| 2390 | output = outputs[0]; |
| 2391 | } |
| 2392 | activeOnly = false; |
| 2393 | } |
| 2394 | |
| 2395 | if (output != mMusicEffectOutput) { |
| 2396 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2397 | mMusicEffectOutput = output; |
| 2398 | } |
| 2399 | |
| 2400 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2401 | return output; |
| 2402 | } |
| 2403 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2404 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2405 | { |
| 2406 | return selectOutputForMusicEffects(); |
| 2407 | } |
| 2408 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2409 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2410 | audio_io_handle_t io, |
| 2411 | uint32_t strategy, |
| 2412 | int session, |
| 2413 | int id) |
| 2414 | { |
| 2415 | ssize_t index = mOutputs.indexOfKey(io); |
| 2416 | if (index < 0) { |
| 2417 | index = mInputs.indexOfKey(io); |
| 2418 | if (index < 0) { |
| 2419 | ALOGW("registerEffect() unknown io %d", io); |
| 2420 | return INVALID_OPERATION; |
| 2421 | } |
| 2422 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2423 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2424 | } |
| 2425 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2426 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 2427 | { |
| 2428 | if (mEffects.getEffect(id) == nullptr) { |
| 2429 | return INVALID_OPERATION; |
| 2430 | } |
| 2431 | |
| 2432 | if (mEffects.isEffectEnabled(id)) { |
| 2433 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 2434 | setEffectEnabled(id, false); |
| 2435 | } |
| 2436 | return mEffects.unregisterEffect(id); |
| 2437 | } |
| 2438 | |
| 2439 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 2440 | { |
| 2441 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 2442 | if (effect == nullptr) { |
| 2443 | return INVALID_OPERATION; |
| 2444 | } |
| 2445 | |
| 2446 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 2447 | if (status == NO_ERROR) { |
| 2448 | mInputs.trackEffectEnabled(effect, enabled); |
| 2449 | } |
| 2450 | return status; |
| 2451 | } |
| 2452 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2453 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2454 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2455 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2456 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2457 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2458 | continue; |
| 2459 | } |
| 2460 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2461 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2462 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2466 | { |
| 2467 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2468 | } |
| 2469 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2470 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2471 | { |
| 2472 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2473 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2474 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2475 | return true; |
| 2476 | } |
| 2477 | } |
| 2478 | return false; |
| 2479 | } |
| 2480 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2481 | // Register a list of custom mixes with their attributes and format. |
| 2482 | // When a mix is registered, corresponding input and output profiles are |
| 2483 | // added to the remote submix hw module. The profile contains only the |
| 2484 | // parameters (sampling rate, format...) specified by the mix. |
| 2485 | // The corresponding input remote submix device is also connected. |
| 2486 | // |
| 2487 | // When a remote submix device is connected, the address is checked to select the |
| 2488 | // appropriate profile and the corresponding input or output stream is opened. |
| 2489 | // |
| 2490 | // When capture starts, getInputForAttr() will: |
| 2491 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2492 | // - 2 if none found, getDeviceForInputSource() will: |
| 2493 | // - 2.1 look for a mix matching the attributes source |
| 2494 | // - 2.2 if none found, default to device selection by policy rules |
| 2495 | // At this time, the corresponding output remote submix device is also connected |
| 2496 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2497 | // after AudioTracks are invalidated |
| 2498 | // |
| 2499 | // When playback starts, getOutputForAttr() will: |
| 2500 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2501 | // - 2 if none found, look for a mix matching the attributes usage |
| 2502 | // - 3 if none found, default to device and output selection by policy rules. |
| 2503 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2504 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2505 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2506 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2507 | status_t res = NO_ERROR; |
| 2508 | |
| 2509 | sp<HwModule> rSubmixModule; |
| 2510 | // examine each mix's route type |
| 2511 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2512 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2513 | // 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] | 2514 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2515 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2516 | break; |
| 2517 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2518 | 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] | 2519 | 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] | 2520 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2521 | rSubmixModule = mHwModules.getModuleFromName( |
| 2522 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2523 | if (rSubmixModule == 0) { |
| 2524 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2525 | i); |
| 2526 | res = INVALID_OPERATION; |
| 2527 | break; |
| 2528 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2529 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2530 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2531 | String8 address = mix.mDeviceAddress; |
| 2532 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2533 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2534 | } else { |
| 2535 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2536 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2537 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2538 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2539 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2540 | res = INVALID_OPERATION; |
| 2541 | break; |
| 2542 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2543 | audio_config_t outputConfig = mix.mFormat; |
| 2544 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2545 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2546 | // stereo and let audio flinger do the channel conversion if needed. |
| 2547 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2548 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2549 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2550 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2551 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2552 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2553 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2554 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2555 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2556 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2557 | address.string(), "remote-submix"); |
| 2558 | } else { |
| 2559 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2560 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2561 | address.string(), "remote-submix"); |
| 2562 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2563 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2564 | String8 address = mix.mDeviceAddress; |
| 2565 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2566 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2567 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2568 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2569 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2570 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2571 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2572 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2573 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2574 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2575 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2576 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2577 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2578 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2579 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2580 | } else { |
| 2581 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2582 | } |
| 2583 | break; |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | if (res != NO_ERROR) { |
| 2588 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2589 | i, device, address.string()); |
| 2590 | res = INVALID_OPERATION; |
| 2591 | break; |
| 2592 | } else if (!foundOutput) { |
| 2593 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2594 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2595 | res = INVALID_OPERATION; |
| 2596 | break; |
| 2597 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2598 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2599 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2600 | if (res != NO_ERROR) { |
| 2601 | unregisterPolicyMixes(mixes); |
| 2602 | } |
| 2603 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2607 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2608 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2609 | status_t res = NO_ERROR; |
| 2610 | sp<HwModule> rSubmixModule; |
| 2611 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2612 | for (const auto& mix : mixes) { |
| 2613 | 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] | 2614 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2615 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2616 | rSubmixModule = mHwModules.getModuleFromName( |
| 2617 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2618 | if (rSubmixModule == 0) { |
| 2619 | res = INVALID_OPERATION; |
| 2620 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2621 | } |
| 2622 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2623 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2624 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2625 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2626 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2627 | res = INVALID_OPERATION; |
| 2628 | continue; |
| 2629 | } |
| 2630 | |
| 2631 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2632 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2633 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2634 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2635 | address.string(), "remote-submix"); |
| 2636 | } |
| 2637 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2638 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2639 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2640 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2641 | address.string(), "remote-submix"); |
| 2642 | } |
| 2643 | rSubmixModule->removeOutputProfile(address); |
| 2644 | rSubmixModule->removeInputProfile(address); |
| 2645 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2646 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2647 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2648 | res = INVALID_OPERATION; |
| 2649 | continue; |
| 2650 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2651 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2652 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2653 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2654 | } |
| 2655 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 2656 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 2657 | { |
| 2658 | size_t i = 0; |
| 2659 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 2660 | for (const auto& fmt : mManualSurroundFormats) { |
| 2661 | if (i++ != 0) dst->append(", "); |
| 2662 | std::string sfmt; |
| 2663 | FormatConverter::toString(fmt, sfmt); |
| 2664 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 2665 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 2666 | } |
| 2667 | } |
| 2668 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2669 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2670 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2671 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2672 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2673 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2674 | std::string stateLiteral; |
| 2675 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2676 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2677 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2678 | "communications", "media", "record", "dock", "system", |
| 2679 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2680 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2681 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 2682 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 2683 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 2684 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 2685 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 2686 | dst->append(" (MANUAL: "); |
| 2687 | dumpManualSurroundFormats(dst); |
| 2688 | dst->append(")"); |
| 2689 | } |
| 2690 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2691 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2692 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2693 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2694 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2695 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2696 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2697 | mHwModulesAll.dump(dst); |
| 2698 | mOutputs.dump(dst); |
| 2699 | mInputs.dump(dst); |
| 2700 | mVolumeCurves->dump(dst); |
| 2701 | mEffects.dump(dst); |
| 2702 | mAudioPatches.dump(dst); |
| 2703 | mPolicyMixes.dump(dst); |
| 2704 | mAudioSources.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | status_t AudioPolicyManager::dump(int fd) |
| 2708 | { |
| 2709 | String8 result; |
| 2710 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2711 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2712 | return NO_ERROR; |
| 2713 | } |
| 2714 | |
| 2715 | // This function checks for the parameters which can be offloaded. |
| 2716 | // This can be enhanced depending on the capability of the DSP and policy |
| 2717 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2718 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2719 | { |
| 2720 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2721 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2722 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2723 | offloadInfo.format, |
| 2724 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2725 | offloadInfo.has_video); |
| 2726 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2727 | if (mMasterMono) { |
| 2728 | return false; // no offloading if mono is set. |
| 2729 | } |
| 2730 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2731 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2732 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2733 | ALOGV("offload disabled by audio.offload.disable"); |
| 2734 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | // Check if stream type is music, then only allow offload as of now. |
| 2738 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2739 | { |
| 2740 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2741 | return false; |
| 2742 | } |
| 2743 | |
| 2744 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2745 | const bool allowOffloadWithVideo = |
| 2746 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2747 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2748 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2749 | return false; |
| 2750 | } |
| 2751 | |
| 2752 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2753 | const int min_duration_secs = property_get_int32( |
| 2754 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2755 | if (min_duration_secs >= 0) { |
| 2756 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2757 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2758 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2759 | return false; |
| 2760 | } |
| 2761 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2762 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2763 | return false; |
| 2764 | } |
| 2765 | |
| 2766 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2767 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2768 | // detects there is an active non offloadable effect. |
| 2769 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2770 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2771 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2772 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2773 | return false; |
| 2774 | } |
| 2775 | |
| 2776 | // See if there is a profile to support this. |
| 2777 | // AUDIO_DEVICE_NONE |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2778 | sp<IOProfile> profile = getProfileForOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2779 | offloadInfo.sample_rate, |
| 2780 | offloadInfo.format, |
| 2781 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2782 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 2783 | true /* directOnly */); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2784 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2785 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2788 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 2789 | const audio_attributes_t& attributes) { |
| 2790 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
| 2791 | audio_attributes_flags_to_audio_output_flags(attributes.flags, output_flags); |
| 2792 | sp<IOProfile> profile = getProfileForOutput(AUDIO_DEVICE_NONE /*ignore device */, |
| 2793 | config.sample_rate, |
| 2794 | config.format, |
| 2795 | config.channel_mask, |
| 2796 | output_flags, |
| 2797 | true /* directOnly */); |
| 2798 | ALOGV("%s() profile %sfound with name: %s, " |
| 2799 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 2800 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 2801 | (profile != 0 ? profile->getTagName().string() : "null"), |
| 2802 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 2803 | return (profile != 0); |
| 2804 | } |
| 2805 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2806 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2807 | audio_port_type_t type, |
| 2808 | unsigned int *num_ports, |
| 2809 | struct audio_port *ports, |
| 2810 | unsigned int *generation) |
| 2811 | { |
| 2812 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2813 | generation == NULL) { |
| 2814 | return BAD_VALUE; |
| 2815 | } |
| 2816 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2817 | if (ports == NULL) { |
| 2818 | *num_ports = 0; |
| 2819 | } |
| 2820 | |
| 2821 | size_t portsWritten = 0; |
| 2822 | size_t portsMax = *num_ports; |
| 2823 | *num_ports = 0; |
| 2824 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2825 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2826 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2827 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2828 | for (const auto& dev : mAvailableOutputDevices) { |
| 2829 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2830 | continue; |
| 2831 | } |
| 2832 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2833 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2834 | } |
| 2835 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2836 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2837 | } |
| 2838 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2839 | for (const auto& dev : mAvailableInputDevices) { |
| 2840 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2841 | continue; |
| 2842 | } |
| 2843 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2844 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2845 | } |
| 2846 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2847 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2851 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2852 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2853 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2854 | } |
| 2855 | *num_ports += mInputs.size(); |
| 2856 | } |
| 2857 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2858 | size_t numOutputs = 0; |
| 2859 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2860 | if (!mOutputs[i]->isDuplicated()) { |
| 2861 | numOutputs++; |
| 2862 | if (portsWritten < portsMax) { |
| 2863 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2864 | } |
| 2865 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2866 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2867 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2871 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2872 | return NO_ERROR; |
| 2873 | } |
| 2874 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2875 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2876 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2877 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2878 | return BAD_VALUE; |
| 2879 | } |
| 2880 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2881 | if (dev != 0) { |
| 2882 | dev->toAudioPort(port); |
| 2883 | return NO_ERROR; |
| 2884 | } |
| 2885 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2886 | if (dev != 0) { |
| 2887 | dev->toAudioPort(port); |
| 2888 | return NO_ERROR; |
| 2889 | } |
| 2890 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2891 | if (out != 0) { |
| 2892 | out->toAudioPort(port); |
| 2893 | return NO_ERROR; |
| 2894 | } |
| 2895 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2896 | if (in != 0) { |
| 2897 | in->toAudioPort(port); |
| 2898 | return NO_ERROR; |
| 2899 | } |
| 2900 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2901 | } |
| 2902 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2903 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2904 | audio_patch_handle_t *handle, |
| 2905 | uid_t uid) |
| 2906 | { |
| 2907 | ALOGV("createAudioPatch()"); |
| 2908 | |
| 2909 | if (handle == NULL || patch == NULL) { |
| 2910 | return BAD_VALUE; |
| 2911 | } |
| 2912 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2913 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2914 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2915 | return BAD_VALUE; |
| 2916 | } |
| 2917 | // only one source per audio patch supported for now |
| 2918 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2919 | return INVALID_OPERATION; |
| 2920 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2921 | |
| 2922 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2923 | return INVALID_OPERATION; |
| 2924 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2925 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2926 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2927 | return INVALID_OPERATION; |
| 2928 | } |
| 2929 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2930 | |
| 2931 | sp<AudioPatch> patchDesc; |
| 2932 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2933 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2934 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2935 | patch->sources[0].role, |
| 2936 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2937 | #if LOG_NDEBUG == 0 |
| 2938 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2939 | 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] | 2940 | patch->sinks[i].role, |
| 2941 | patch->sinks[i].type); |
| 2942 | } |
| 2943 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2944 | |
| 2945 | if (index >= 0) { |
| 2946 | patchDesc = mAudioPatches.valueAt(index); |
| 2947 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2948 | mUidCached, patchDesc->mUid, uid); |
| 2949 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2950 | return INVALID_OPERATION; |
| 2951 | } |
| 2952 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2953 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2954 | } |
| 2955 | |
| 2956 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2957 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2958 | if (outputDesc == NULL) { |
| 2959 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2960 | return BAD_VALUE; |
| 2961 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2962 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2963 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2964 | if (patchDesc != 0) { |
| 2965 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2966 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2967 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2968 | return BAD_VALUE; |
| 2969 | } |
| 2970 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2971 | DeviceVector devices; |
| 2972 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2973 | // Only support mix to devices connection |
| 2974 | // TODO add support for mix to mix connection |
| 2975 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2976 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2977 | return INVALID_OPERATION; |
| 2978 | } |
| 2979 | sp<DeviceDescriptor> devDesc = |
| 2980 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2981 | if (devDesc == 0) { |
| 2982 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2983 | return BAD_VALUE; |
| 2984 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2985 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2986 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 2987 | devDesc->address(), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2988 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2989 | NULL, // updatedSamplingRate |
| 2990 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2991 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2992 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2993 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2994 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2995 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2996 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2997 | return INVALID_OPERATION; |
| 2998 | } |
| 2999 | devices.add(devDesc); |
| 3000 | } |
| 3001 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3002 | return INVALID_OPERATION; |
| 3003 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3004 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3005 | // TODO: reconfigure output format and channels here |
| 3006 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3007 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3008 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3009 | index = mAudioPatches.indexOfKey(*handle); |
| 3010 | if (index >= 0) { |
| 3011 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3012 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3013 | } |
| 3014 | patchDesc = mAudioPatches.valueAt(index); |
| 3015 | patchDesc->mUid = uid; |
| 3016 | ALOGV("createAudioPatch() success"); |
| 3017 | } else { |
| 3018 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3019 | return INVALID_OPERATION; |
| 3020 | } |
| 3021 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3022 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3023 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3024 | // only one sink supported when connecting an input device to a mix |
| 3025 | if (patch->num_sinks > 1) { |
| 3026 | return INVALID_OPERATION; |
| 3027 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3028 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3029 | if (inputDesc == NULL) { |
| 3030 | return BAD_VALUE; |
| 3031 | } |
| 3032 | if (patchDesc != 0) { |
| 3033 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3034 | return BAD_VALUE; |
| 3035 | } |
| 3036 | } |
| 3037 | sp<DeviceDescriptor> devDesc = |
| 3038 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3039 | if (devDesc == 0) { |
| 3040 | return BAD_VALUE; |
| 3041 | } |
| 3042 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3043 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3044 | devDesc->address(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3045 | patch->sinks[0].sample_rate, |
| 3046 | NULL, /*updatedSampleRate*/ |
| 3047 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3048 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3049 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3050 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3051 | // FIXME for the parameter type, |
| 3052 | // and the NONE |
| 3053 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3054 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3055 | return INVALID_OPERATION; |
| 3056 | } |
| 3057 | // TODO: reconfigure output format and channels here |
| 3058 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3059 | devDesc->type(), inputDesc->mIoHandle); |
| 3060 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3061 | index = mAudioPatches.indexOfKey(*handle); |
| 3062 | if (index >= 0) { |
| 3063 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3064 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3065 | } |
| 3066 | patchDesc = mAudioPatches.valueAt(index); |
| 3067 | patchDesc->mUid = uid; |
| 3068 | ALOGV("createAudioPatch() success"); |
| 3069 | } else { |
| 3070 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3071 | return INVALID_OPERATION; |
| 3072 | } |
| 3073 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3074 | // device to device connection |
| 3075 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3076 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3077 | return BAD_VALUE; |
| 3078 | } |
| 3079 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3080 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3081 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3082 | if (srcDeviceDesc == 0) { |
| 3083 | return BAD_VALUE; |
| 3084 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3085 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3086 | //update source and sink with our own data as the data passed in the patch may |
| 3087 | // be incomplete. |
| 3088 | struct audio_patch newPatch = *patch; |
| 3089 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3090 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3091 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3092 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3093 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3094 | return INVALID_OPERATION; |
| 3095 | } |
| 3096 | |
| 3097 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3098 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3099 | if (sinkDeviceDesc == 0) { |
| 3100 | return BAD_VALUE; |
| 3101 | } |
| 3102 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3103 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3104 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3105 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3106 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 3107 | // - audio HAL version is >= 3.0 but no route has been declared between devices |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3108 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3109 | (srcDeviceDesc->getModuleVersionMajor() < 3) || |
| 3110 | !srcDeviceDesc->getModule()->supportsPatch(srcDeviceDesc, sinkDeviceDesc)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3111 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3112 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3113 | return INVALID_OPERATION; |
| 3114 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3115 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3116 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3117 | // if the sink device is reachable via an opened output stream, request to go via |
| 3118 | // this output stream by adding a second source to the patch description |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 3119 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3120 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3121 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3122 | if (outputDesc->isDuplicated()) { |
| 3123 | return INVALID_OPERATION; |
| 3124 | } |
| 3125 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3126 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3127 | newPatch.num_sources = 2; |
| 3128 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3129 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3130 | } |
| 3131 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3132 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3133 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3134 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3135 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3136 | status); |
| 3137 | return INVALID_OPERATION; |
| 3138 | } |
| 3139 | } else { |
| 3140 | return BAD_VALUE; |
| 3141 | } |
| 3142 | } else { |
| 3143 | return BAD_VALUE; |
| 3144 | } |
| 3145 | return NO_ERROR; |
| 3146 | } |
| 3147 | |
| 3148 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3149 | uid_t uid) |
| 3150 | { |
| 3151 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3152 | |
| 3153 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3154 | |
| 3155 | if (index < 0) { |
| 3156 | return BAD_VALUE; |
| 3157 | } |
| 3158 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3159 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3160 | mUidCached, patchDesc->mUid, uid); |
| 3161 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3162 | return INVALID_OPERATION; |
| 3163 | } |
| 3164 | |
| 3165 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3166 | patchDesc->mUid = mUidCached; |
| 3167 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3168 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3169 | if (outputDesc == NULL) { |
| 3170 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3171 | return BAD_VALUE; |
| 3172 | } |
| 3173 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3174 | setOutputDevice(outputDesc, |
| 3175 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3176 | true, |
| 3177 | 0, |
| 3178 | NULL); |
| 3179 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3180 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3181 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3182 | if (inputDesc == NULL) { |
| 3183 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3184 | return BAD_VALUE; |
| 3185 | } |
| 3186 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3187 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3188 | true, |
| 3189 | NULL); |
| 3190 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3191 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3192 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3193 | status, patchDesc->mAfPatchHandle); |
| 3194 | removeAudioPatch(patchDesc->mHandle); |
| 3195 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3196 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3197 | } else { |
| 3198 | return BAD_VALUE; |
| 3199 | } |
| 3200 | } else { |
| 3201 | return BAD_VALUE; |
| 3202 | } |
| 3203 | return NO_ERROR; |
| 3204 | } |
| 3205 | |
| 3206 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3207 | struct audio_patch *patches, |
| 3208 | unsigned int *generation) |
| 3209 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3210 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3211 | return BAD_VALUE; |
| 3212 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3213 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3214 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3217 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3218 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3219 | ALOGV("setAudioPortConfig()"); |
| 3220 | |
| 3221 | if (config == NULL) { |
| 3222 | return BAD_VALUE; |
| 3223 | } |
| 3224 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3225 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3226 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3227 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3230 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3231 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3232 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3233 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3234 | if (outputDesc == NULL) { |
| 3235 | return BAD_VALUE; |
| 3236 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3237 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3238 | "setAudioPortConfig() called on duplicated output %d", |
| 3239 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3240 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3241 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3242 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3243 | if (inputDesc == NULL) { |
| 3244 | return BAD_VALUE; |
| 3245 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3246 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3247 | } else { |
| 3248 | return BAD_VALUE; |
| 3249 | } |
| 3250 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3251 | sp<DeviceDescriptor> deviceDesc; |
| 3252 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3253 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3254 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3255 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3256 | } else { |
| 3257 | return BAD_VALUE; |
| 3258 | } |
| 3259 | if (deviceDesc == NULL) { |
| 3260 | return BAD_VALUE; |
| 3261 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3262 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3263 | } else { |
| 3264 | return BAD_VALUE; |
| 3265 | } |
| 3266 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3267 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3268 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3269 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3270 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3271 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3272 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3273 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3274 | if (status != NO_ERROR) { |
| 3275 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3276 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3277 | |
| 3278 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3279 | } |
| 3280 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3281 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3282 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3283 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3284 | clearAudioPatches(uid); |
| 3285 | clearSessionRoutes(uid); |
| 3286 | } |
| 3287 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3288 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3289 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3290 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3291 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3292 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3293 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3294 | } |
| 3295 | } |
| 3296 | } |
| 3297 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3298 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3299 | audio_io_handle_t ouptutToSkip) |
| 3300 | { |
| 3301 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3302 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3303 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3304 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3305 | continue; |
| 3306 | } |
| 3307 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3308 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3309 | continue; |
| 3310 | } |
| 3311 | // If the default device for this strategy is on another output mix, |
| 3312 | // invalidate all tracks in this strategy to force re connection. |
| 3313 | // Otherwise select new device on the output mix. |
| 3314 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3315 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3316 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3317 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3318 | } |
| 3319 | } |
| 3320 | } else { |
| 3321 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3322 | setOutputDevice(outputDesc, newDevice, false); |
| 3323 | } |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3328 | { |
| 3329 | // remove output routes associated with this uid |
| 3330 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3331 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3332 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3333 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3334 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3335 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3336 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | } |
| 3340 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3341 | for (const auto& strategy : affectedStrategies) { |
| 3342 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | // remove input routes associated with this uid |
| 3346 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3347 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3348 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3349 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3350 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3351 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3352 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | // reroute inputs if necessary |
| 3357 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3358 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3359 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3360 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3361 | inputsToClose.add(inputDesc->mIoHandle); |
| 3362 | } |
| 3363 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3364 | for (const auto& input : inputsToClose) { |
| 3365 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3366 | } |
| 3367 | } |
| 3368 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3369 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3370 | { |
| 3371 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3372 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3373 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3374 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3375 | } |
| 3376 | } |
| 3377 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3378 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3379 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3380 | audio_io_handle_t *ioHandle, |
| 3381 | audio_devices_t *device) |
| 3382 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3383 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3384 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3385 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3386 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3387 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3388 | } |
| 3389 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3390 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3391 | const audio_attributes_t *attributes, |
| 3392 | audio_port_handle_t *portId, |
| 3393 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3394 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3395 | ALOGV("%s", __FUNCTION__); |
| 3396 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3397 | |
| 3398 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3399 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3400 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3401 | return BAD_VALUE; |
| 3402 | } |
| 3403 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3404 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3405 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3406 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3407 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3408 | return INVALID_OPERATION; |
| 3409 | } |
| 3410 | |
| 3411 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3412 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3413 | String8(source->ext.device.address)); |
| 3414 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3415 | 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] | 3416 | return BAD_VALUE; |
| 3417 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3418 | |
| 3419 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3420 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3421 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3422 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3423 | |
| 3424 | sp<SourceClientDescriptor> sourceDesc = |
| 3425 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3426 | streamTypefromAttributesInt(attributes), |
| 3427 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3428 | |
| 3429 | status_t status = connectAudioSource(sourceDesc); |
| 3430 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3431 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3432 | } |
| 3433 | return status; |
| 3434 | } |
| 3435 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3436 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3437 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3438 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3439 | |
| 3440 | // make sure we only have one patch per source. |
| 3441 | disconnectAudioSource(sourceDesc); |
| 3442 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3443 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3444 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3445 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3446 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3447 | |
| 3448 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3449 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3450 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3451 | |
| 3452 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3453 | |
François Gaffie | 83d789d | 2018-05-29 13:29:19 +0200 | [diff] [blame] | 3454 | if (srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) && |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3455 | srcDeviceDesc->getModuleVersionMajor() >= 3 && |
| 3456 | sinkDeviceDesc->getModule()->supportsPatch(srcDeviceDesc, sinkDeviceDesc) && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3457 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
François Gaffie | 83d789d | 2018-05-29 13:29:19 +0200 | [diff] [blame] | 3458 | ALOGV("%s Device to Device route supported by >=3.0 HAL", __FUNCTION__); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3459 | // TODO: may explicitly specify whether we should use HW or SW patch |
François Gaffie | 83d789d | 2018-05-29 13:29:19 +0200 | [diff] [blame] | 3460 | // create patch between src device and output device |
| 3461 | // create Hwoutput and add to mHwOutputs |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3462 | } else { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3463 | audio_attributes_t resultAttr; |
| 3464 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3465 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3466 | config.sample_rate = sourceDesc->config().sample_rate; |
| 3467 | config.channel_mask = sourceDesc->config().channel_mask; |
| 3468 | config.format = sourceDesc->config().format; |
| 3469 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 3470 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3471 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, |
| 3472 | &attributes, &stream, sourceDesc->uid(), &config, &flags, &selectedDeviceId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3473 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3474 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3475 | return INVALID_OPERATION; |
| 3476 | } |
| 3477 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3478 | if (outputDesc->isDuplicated()) { |
| 3479 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3480 | return INVALID_OPERATION; |
| 3481 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3482 | status_t status = outputDesc->start(); |
| 3483 | if (status != NO_ERROR) { |
| 3484 | return status; |
| 3485 | } |
| 3486 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3487 | // create a special patch with no sink and two sources: |
| 3488 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3489 | // be connected as well as the stream type for volume control |
| 3490 | // - the sink is defined by whatever output device is currently selected for the output |
| 3491 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3492 | PatchBuilder patchBuilder; |
| 3493 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3494 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3495 | &afPatchHandle, |
| 3496 | 0); |
| 3497 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3498 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3499 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3500 | if (status != NO_ERROR) { |
| 3501 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3502 | __FUNCTION__, status); |
| 3503 | return INVALID_OPERATION; |
| 3504 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3505 | |
| 3506 | if (outputDesc->getClient(sourceDesc->portId()) != nullptr) { |
| 3507 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 3508 | return INVALID_OPERATION; |
| 3509 | } |
| 3510 | outputDesc->addClient(sourceDesc); |
| 3511 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3512 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3513 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3514 | |
| 3515 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3516 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3517 | return status; |
| 3518 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3519 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3520 | if (delayMs != 0) { |
| 3521 | usleep(delayMs * 1000); |
| 3522 | } |
| 3523 | } |
| 3524 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3525 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3526 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3527 | |
| 3528 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3529 | } |
| 3530 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3531 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3532 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3533 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3534 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3535 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3536 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3537 | return BAD_VALUE; |
| 3538 | } |
| 3539 | status_t status = disconnectAudioSource(sourceDesc); |
| 3540 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3541 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3542 | return status; |
| 3543 | } |
| 3544 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3545 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3546 | { |
| 3547 | if (mMasterMono == mono) { |
| 3548 | return NO_ERROR; |
| 3549 | } |
| 3550 | mMasterMono = mono; |
| 3551 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3552 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3553 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3554 | // |
| 3555 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3556 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3557 | // play back offloaded. |
| 3558 | if (mMasterMono) { |
| 3559 | Vector<audio_io_handle_t> offloaded; |
| 3560 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3561 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3562 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3563 | offloaded.push(desc->mIoHandle); |
| 3564 | } |
| 3565 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3566 | for (const auto& handle : offloaded) { |
| 3567 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | // update master mono for all remaining outputs |
| 3571 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3572 | updateMono(mOutputs.keyAt(i)); |
| 3573 | } |
| 3574 | return NO_ERROR; |
| 3575 | } |
| 3576 | |
| 3577 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3578 | { |
| 3579 | *mono = mMasterMono; |
| 3580 | return NO_ERROR; |
| 3581 | } |
| 3582 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3583 | float AudioPolicyManager::getStreamVolumeDB( |
| 3584 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3585 | { |
| 3586 | return computeVolume(stream, index, device); |
| 3587 | } |
| 3588 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3589 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3590 | audio_format_t *surroundFormats, |
| 3591 | bool *surroundFormatsEnabled, |
| 3592 | bool reported) |
| 3593 | { |
| 3594 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3595 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3596 | return BAD_VALUE; |
| 3597 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3598 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p reported %d", |
| 3599 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3600 | |
| 3601 | size_t formatsWritten = 0; |
| 3602 | size_t formatsMax = *numSurroundFormats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3603 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3604 | if (reported) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3605 | // Return formats from all device profiles that have already been resolved by |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3606 | // checkOutputsForDevice(). |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3607 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 3608 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 3609 | FormatVector supportedFormats = |
| 3610 | device->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3611 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
| 3612 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
| 3613 | formats.insert(supportedFormats[j]); |
| 3614 | } else { |
| 3615 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3616 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3617 | formats.insert(pair.first); |
| 3618 | break; |
| 3619 | } |
| 3620 | } |
| 3621 | } |
| 3622 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3623 | } |
| 3624 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3625 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3626 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3627 | } |
| 3628 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3629 | *numSurroundFormats = formats.size(); |
| 3630 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3631 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3632 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3633 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3634 | surroundFormats[formatsWritten] = format; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3635 | bool formatEnabled = true; |
| 3636 | switch (forceUse) { |
| 3637 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
| 3638 | formatEnabled = mManualSurroundFormats.count(format) != 0; |
| 3639 | break; |
| 3640 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 3641 | formatEnabled = false; |
| 3642 | break; |
| 3643 | default: // AUTO or ALWAYS => true |
| 3644 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3645 | } |
| 3646 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3647 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3648 | } |
| 3649 | return NO_ERROR; |
| 3650 | } |
| 3651 | |
| 3652 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3653 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3654 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3655 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3656 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3657 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3658 | return BAD_VALUE; |
| 3659 | } |
| 3660 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3661 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 3662 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3663 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3664 | return INVALID_OPERATION; |
| 3665 | } |
| 3666 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3667 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3668 | return NO_ERROR; |
| 3669 | } |
| 3670 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3671 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3672 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3673 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3674 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3675 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3676 | } |
| 3677 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3678 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3679 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3680 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3685 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3686 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3687 | AUDIO_DEVICE_OUT_HDMI); |
| 3688 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3689 | // Simulate reconnection to update enabled surround sound formats. |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3690 | String8 address = hdmiOutputDevices[i]->address(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3691 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3692 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3693 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3694 | address.c_str(), |
| 3695 | name.c_str()); |
| 3696 | if (status != NO_ERROR) { |
| 3697 | continue; |
| 3698 | } |
| 3699 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3700 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3701 | address.c_str(), |
| 3702 | name.c_str()); |
| 3703 | profileUpdated |= (status == NO_ERROR); |
| 3704 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3705 | // FIXME: Why doing this for input HDMI devices if we don't augment their reported formats? |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3706 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3707 | AUDIO_DEVICE_IN_HDMI); |
| 3708 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3709 | // Simulate reconnection to update enabled surround sound formats. |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3710 | String8 address = hdmiInputDevices[i]->address(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3711 | String8 name = hdmiInputDevices[i]->getName(); |
| 3712 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3713 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3714 | address.c_str(), |
| 3715 | name.c_str()); |
| 3716 | if (status != NO_ERROR) { |
| 3717 | continue; |
| 3718 | } |
| 3719 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3720 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3721 | address.c_str(), |
| 3722 | name.c_str()); |
| 3723 | profileUpdated |= (status == NO_ERROR); |
| 3724 | } |
| 3725 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3726 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3727 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3728 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3729 | } |
| 3730 | |
| 3731 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3732 | } |
| 3733 | |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3734 | void AudioPolicyManager::setAppState(uid_t uid, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3735 | { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3736 | ALOGV("%s(uid:%d, state:%d)", __func__, uid, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3737 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame^] | 3738 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3739 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
| 3740 | RecordClientVector clients = inputDesc->clientsList(false /*activeOnly*/); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3741 | for (const auto& client : clients) { |
| 3742 | if (uid == client->uid()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3743 | client->setAppState(state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3744 | } |
| 3745 | } |
| 3746 | } |
| 3747 | } |
| 3748 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 3749 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 3750 | { |
| 3751 | for (const auto& hwModule : mHwModules) { |
| 3752 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 3753 | for (const auto &outProfile : outputProfiles) { |
| 3754 | struct audio_port audioPort; |
| 3755 | outProfile->toAudioPort(&audioPort); |
| 3756 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 3757 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 3758 | return true; |
| 3759 | } |
| 3760 | } |
| 3761 | } |
| 3762 | } |
| 3763 | return false; |
| 3764 | } |
| 3765 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3766 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3767 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3768 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3769 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3770 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3771 | if (patchDesc == 0) { |
| 3772 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3773 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3774 | return BAD_VALUE; |
| 3775 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3776 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3777 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3778 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3779 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3780 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3781 | if (status == NO_ERROR) { |
| 3782 | swOutputDesc->stop(); |
| 3783 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3784 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3785 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3786 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3787 | if (hwOutputDesc != 0) { |
| 3788 | // release patch between src device and output device |
| 3789 | // close Hwoutput and remove from mHwOutputs |
| 3790 | } else { |
| 3791 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3792 | } |
| 3793 | } |
| 3794 | return NO_ERROR; |
| 3795 | } |
| 3796 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3797 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3798 | audio_io_handle_t output, routing_strategy strategy) |
| 3799 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3800 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3801 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3802 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3803 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3804 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3805 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3806 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3807 | source = sourceDesc; |
| 3808 | break; |
| 3809 | } |
| 3810 | } |
| 3811 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3812 | } |
| 3813 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3814 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3815 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3816 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3817 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3818 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3819 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3820 | } |
| 3821 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3822 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3823 | static const char *kConfigLocationList[] = |
| 3824 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3825 | static const int kConfigLocationListSize = |
| 3826 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3827 | |
| 3828 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3829 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3830 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3831 | status_t ret; |
| 3832 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3833 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3834 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3835 | // A2DP offload supported but disabled: try to use special XML file |
| 3836 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3837 | } |
| 3838 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3839 | |
| 3840 | for (const char* fileName : fileNames) { |
| 3841 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3842 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3843 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3844 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3845 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3846 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3847 | return ret; |
| 3848 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3849 | } |
| 3850 | } |
| 3851 | return ret; |
| 3852 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3853 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3854 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3855 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3856 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3857 | 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] | 3858 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3859 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3860 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3861 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3862 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3863 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3864 | mAudioPortGeneration(1), |
| 3865 | mBeaconMuteRefCount(0), |
| 3866 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3867 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3868 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3869 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3870 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3871 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3872 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3873 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3874 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3875 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3876 | { |
| 3877 | loadConfig(); |
| 3878 | initialize(); |
| 3879 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3880 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3881 | // This check is to catch any legacy platform updating to Q without having |
| 3882 | // switched to XML since its deprecation on O. |
| 3883 | // TODO: after Q release, remove this check and flag as XML is now the only |
| 3884 | // option and all legacy platform should have transitioned to XML. |
| 3885 | #ifndef USE_XML_AUDIO_POLICY_CONF |
| 3886 | #error Audio policy no longer supports legacy .conf configuration format |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3887 | #endif |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3888 | |
| 3889 | void AudioPolicyManager::loadConfig() { |
| 3890 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3891 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3892 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3893 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3894 | } |
| 3895 | |
| 3896 | status_t AudioPolicyManager::initialize() { |
| 3897 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3898 | |
| 3899 | // 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] | 3900 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3901 | if (!engineInstance) { |
| 3902 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3903 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3904 | } |
| 3905 | // Retrieve the Policy Manager Interface |
| 3906 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3907 | if (mEngine == NULL) { |
| 3908 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3909 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3910 | } |
| 3911 | mEngine->setObserver(this); |
| 3912 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3913 | if (status != NO_ERROR) { |
| 3914 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3915 | return status; |
| 3916 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3917 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3918 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3919 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3920 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3921 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3922 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3923 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3924 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3925 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3926 | continue; |
| 3927 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3928 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3929 | // open all output streams needed to access attached devices |
| 3930 | // except for direct output streams that are only opened when they are actually |
| 3931 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3932 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3933 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3934 | if (!outProfile->canOpenNewIo()) { |
| 3935 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3936 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3937 | continue; |
| 3938 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3939 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3940 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3941 | continue; |
| 3942 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3943 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3944 | mTtsOutputAvailable = true; |
| 3945 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3946 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3947 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3948 | continue; |
| 3949 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3950 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3951 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3952 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3953 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3954 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3955 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3956 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3957 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3958 | if ((profileType & outputDeviceTypes) == 0) { |
| 3959 | continue; |
| 3960 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3961 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3962 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3963 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3964 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 3965 | profileType); |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3966 | String8 address = getFirstDeviceAddress(devicesForType); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3967 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3968 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3969 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3970 | |
| 3971 | if (status != NO_ERROR) { |
| 3972 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3973 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3974 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3975 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3976 | for (const auto& dev : supportedDevices) { |
| 3977 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3978 | // 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] | 3979 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3980 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3984 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3985 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3986 | } |
| 3987 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3988 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3989 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3990 | true, |
| 3991 | 0, |
| 3992 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3993 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3994 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3995 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3996 | // open input streams needed to access attached devices to validate |
| 3997 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3998 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3999 | if (!inProfile->canOpenNewIo()) { |
| 4000 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4001 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4002 | continue; |
| 4003 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4004 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4005 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4006 | continue; |
| 4007 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4008 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4009 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4010 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4011 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4012 | if ((profileType & inputDeviceTypes) == 0) { |
| 4013 | continue; |
| 4014 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4015 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4016 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4017 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4018 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4019 | // the inputs vector must be of size >= 1, but we don't want to crash here |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 4020 | String8 address = getFirstDeviceAddress(inputDevices); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4021 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4022 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4023 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4024 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4025 | status_t status = inputDesc->open(nullptr, |
| 4026 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4027 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4028 | AUDIO_SOURCE_MIC, |
| 4029 | AUDIO_INPUT_FLAG_NONE, |
| 4030 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4031 | |
| 4032 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4033 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4034 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4035 | // 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] | 4036 | if (index >= 0) { |
| 4037 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4038 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4039 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4040 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4041 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4042 | } |
| 4043 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4044 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4045 | } else { |
| 4046 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4047 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4048 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4049 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4050 | } |
| 4051 | } |
| 4052 | // make sure all attached devices have been allocated a unique ID |
| 4053 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4054 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4055 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4056 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4057 | continue; |
| 4058 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4059 | // The device is now validated and can be appended to the available devices of the engine |
| 4060 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4061 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4062 | i++; |
| 4063 | } |
| 4064 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4065 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4066 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4067 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4068 | continue; |
| 4069 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4070 | // The device is now validated and can be appended to the available devices of the engine |
| 4071 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4072 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4073 | i++; |
| 4074 | } |
| 4075 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4076 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4077 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4078 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4079 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4080 | // If microphones address is empty, set it according to device type |
| 4081 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 4082 | if (mAvailableInputDevices[i]->address().isEmpty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4083 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 4084 | mAvailableInputDevices[i]->address() = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4085 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 4086 | mAvailableInputDevices[i]->address() = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4087 | } |
| 4088 | } |
| 4089 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4090 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4091 | if (mPrimaryOutput == 0) { |
| 4092 | ALOGE("Failed to open primary output"); |
| 4093 | status = NO_INIT; |
| 4094 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4095 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4096 | // Silence ALOGV statements |
| 4097 | property_set("log.tag." LOG_TAG, "D"); |
| 4098 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4099 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4100 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4101 | } |
| 4102 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4103 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4104 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4105 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4106 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | } |
| 4108 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4109 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4110 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4111 | mAvailableOutputDevices.clear(); |
| 4112 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4113 | mOutputs.clear(); |
| 4114 | mInputs.clear(); |
| 4115 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4116 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4117 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4118 | } |
| 4119 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4120 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4121 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4122 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4123 | } |
| 4124 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4125 | // --- |
| 4126 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4127 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4128 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4129 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4130 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4131 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4132 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4133 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4134 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4135 | } |
| 4136 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4137 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4138 | { |
| 4139 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4140 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4141 | } |
| 4142 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4143 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4144 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4145 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4146 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4147 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4148 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4149 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4150 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4151 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4152 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4153 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4154 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4155 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4156 | if (devDesc != 0) { |
| 4157 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4158 | desc->mIoHandle, address.string()); |
| 4159 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4160 | } |
| 4161 | } |
| 4162 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4163 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4164 | audio_policy_dev_state_t state, |
| 4165 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4166 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4167 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4168 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4169 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4170 | |
| 4171 | if (audio_device_is_digital(device)) { |
| 4172 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4173 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4174 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4175 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4176 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4177 | // first list already open outputs that can be routed to this device |
| 4178 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4179 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4180 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4181 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4182 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4183 | outputs.add(mOutputs.keyAt(i)); |
| 4184 | } else { |
| 4185 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4186 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4187 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4188 | } |
| 4189 | } |
| 4190 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4191 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4192 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4193 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4194 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4195 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4196 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4197 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4198 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4199 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4200 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4201 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4202 | } |
| 4203 | } |
| 4204 | } |
| 4205 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4206 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4207 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4208 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4209 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4210 | return BAD_VALUE; |
| 4211 | } |
| 4212 | |
| 4213 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4214 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4215 | 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] | 4216 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4217 | |
| 4218 | // nothing to do if one output is already opened for this profile |
| 4219 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4220 | for (j = 0; j < outputs.size(); j++) { |
| 4221 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4222 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4223 | // matching profile: save the sample rates, format and channel masks supported |
| 4224 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4225 | if (audio_device_is_digital(device)) { |
| 4226 | devDesc->importAudioPort(profile); |
| 4227 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4228 | break; |
| 4229 | } |
| 4230 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4231 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4232 | continue; |
| 4233 | } |
| 4234 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4235 | if (!profile->canOpenNewIo()) { |
| 4236 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4237 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4238 | continue; |
| 4239 | } |
| 4240 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4241 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4242 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4243 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4244 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4245 | status_t status = desc->open(nullptr, device, address, |
| 4246 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4247 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4248 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4249 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4250 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4251 | char *param = audio_device_address_to_parameter(device, address); |
| 4252 | mpClientInterface->setParameters(output, String8(param)); |
| 4253 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4254 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 4255 | updateAudioProfiles(devDesc, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4256 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4257 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4258 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4259 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4260 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4261 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4262 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4263 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4264 | profile->pickAudioProfile( |
| 4265 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4266 | config.offload_info.sample_rate = config.sample_rate; |
| 4267 | config.offload_info.channel_mask = config.channel_mask; |
| 4268 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4269 | |
| 4270 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4271 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4272 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4273 | output = AUDIO_IO_HANDLE_NONE; |
| 4274 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4275 | } |
| 4276 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4277 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4278 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4279 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4280 | sp<AudioPolicyMix> policyMix; |
| 4281 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4282 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4283 | address.string()); |
| 4284 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4285 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4286 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4287 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4288 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4289 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4290 | // no duplicated output for direct outputs and |
| 4291 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4292 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4294 | //TODO: configure audio effect output stage here |
| 4295 | |
| 4296 | // 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] | 4297 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4298 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4299 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4300 | &duplicatedOutput); |
| 4301 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4302 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4303 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4304 | } else { |
| 4305 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4306 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4307 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4308 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4309 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4310 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4311 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | } |
| 4313 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4314 | } else { |
| 4315 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4316 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4317 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4318 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4319 | profiles.removeAt(profile_index); |
| 4320 | profile_index--; |
| 4321 | } else { |
| 4322 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4323 | // Load digital format info only for digital devices |
| 4324 | if (audio_device_is_digital(device)) { |
| 4325 | devDesc->importAudioPort(profile); |
| 4326 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4327 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4328 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4329 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4330 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4331 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4332 | NULL/*patch handle*/, address.string()); |
| 4333 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4334 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4335 | } |
| 4336 | } |
| 4337 | |
| 4338 | if (profiles.isEmpty()) { |
| 4339 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4340 | return BAD_VALUE; |
| 4341 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4342 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4343 | // check if one opened output is not needed any more after disconnecting one device |
| 4344 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4345 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4346 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4347 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4348 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4349 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4350 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4351 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4352 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4353 | mOutputs.keyAt(i)); |
| 4354 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4355 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4356 | } |
| 4357 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4358 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4359 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4360 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4361 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4362 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4363 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4364 | "clearing direct output profile %zu on module %s", |
| 4365 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4366 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4367 | } |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | return NO_ERROR; |
| 4372 | } |
| 4373 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4374 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4375 | audio_policy_dev_state_t state, |
| 4376 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4377 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4378 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4379 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4380 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4381 | |
| 4382 | if (audio_device_is_digital(device)) { |
| 4383 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4384 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4385 | } |
| 4386 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4387 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4388 | // first list already open inputs that can be routed to this device |
| 4389 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4390 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4391 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4392 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4393 | inputs.add(mInputs.keyAt(input_index)); |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4398 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4399 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4400 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4401 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4402 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4403 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4404 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4405 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4406 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4407 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4408 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4409 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4410 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4411 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4412 | } |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4417 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4418 | return BAD_VALUE; |
| 4419 | } |
| 4420 | |
| 4421 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4422 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4423 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4424 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4425 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4426 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4427 | // nothing to do if one input is already opened for this profile |
| 4428 | size_t input_index; |
| 4429 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4430 | desc = mInputs.valueAt(input_index); |
| 4431 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4432 | if (audio_device_is_digital(device)) { |
| 4433 | devDesc->importAudioPort(profile); |
| 4434 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4435 | break; |
| 4436 | } |
| 4437 | } |
| 4438 | if (input_index != mInputs.size()) { |
| 4439 | continue; |
| 4440 | } |
| 4441 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4442 | if (!profile->canOpenNewIo()) { |
| 4443 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4444 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4445 | continue; |
| 4446 | } |
| 4447 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4448 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4449 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4450 | status_t status = desc->open(nullptr, |
| 4451 | device, |
| 4452 | address, |
| 4453 | AUDIO_SOURCE_MIC, |
| 4454 | AUDIO_INPUT_FLAG_NONE, |
| 4455 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4456 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4457 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4458 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4459 | char *param = audio_device_address_to_parameter(device, address); |
| 4460 | mpClientInterface->setParameters(input, String8(param)); |
| 4461 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4462 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 4463 | updateAudioProfiles(devDesc, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4464 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4465 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4466 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4467 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4468 | } |
| 4469 | |
| 4470 | if (input != 0) { |
| 4471 | addInput(input, desc); |
| 4472 | } |
| 4473 | } // endif input != 0 |
| 4474 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4475 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4476 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4477 | profiles.removeAt(profile_index); |
| 4478 | profile_index--; |
| 4479 | } else { |
| 4480 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4481 | if (audio_device_is_digital(device)) { |
| 4482 | devDesc->importAudioPort(profile); |
| 4483 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4484 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4485 | } |
| 4486 | } // end scan profiles |
| 4487 | |
| 4488 | if (profiles.isEmpty()) { |
| 4489 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4490 | return BAD_VALUE; |
| 4491 | } |
| 4492 | } else { |
| 4493 | // Disconnect |
| 4494 | // check if one opened input is not needed any more after disconnecting one device |
| 4495 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4496 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4497 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4498 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4499 | mInputs.keyAt(input_index)); |
| 4500 | inputs.add(mInputs.keyAt(input_index)); |
| 4501 | } |
| 4502 | } |
| 4503 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4504 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4505 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4506 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4507 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4508 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4509 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4510 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4511 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4512 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4513 | } |
| 4514 | } |
| 4515 | } |
| 4516 | } // end disconnect |
| 4517 | |
| 4518 | return NO_ERROR; |
| 4519 | } |
| 4520 | |
| 4521 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4522 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4523 | { |
| 4524 | ALOGV("closeOutput(%d)", output); |
| 4525 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4526 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4527 | if (outputDesc == NULL) { |
| 4528 | ALOGW("closeOutput() unknown output %d", output); |
| 4529 | return; |
| 4530 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4531 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4532 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4533 | // look for duplicated outputs connected to the output being removed. |
| 4534 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4535 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | if (dupOutputDesc->isDuplicated() && |
| 4537 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4538 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4539 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4540 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4541 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4542 | } else { |
| 4543 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4544 | } |
| 4545 | // As all active tracks on duplicated output will be deleted, |
| 4546 | // and as they were also referenced on the other output, the reference |
| 4547 | // count for their stream type must be adjusted accordingly on |
| 4548 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4549 | const bool wasActive = outputDesc2->isActive(); |
| 4550 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4551 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4552 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4553 | // stop() will be a no op if the output is still active but is needed in case all |
| 4554 | // active streams refcounts where cleared above |
| 4555 | if (wasActive) { |
| 4556 | outputDesc2->stop(); |
| 4557 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4558 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4559 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4560 | |
| 4561 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4562 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4563 | } |
| 4564 | } |
| 4565 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4566 | nextAudioPortGeneration(); |
| 4567 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4568 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4569 | if (index >= 0) { |
| 4570 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4571 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4572 | mAudioPatches.removeItemsAt(index); |
| 4573 | mpClientInterface->onAudioPatchListUpdate(); |
| 4574 | } |
| 4575 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4576 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4577 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4578 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4579 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4580 | |
| 4581 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4582 | // no direct outputs are open. |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4583 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4584 | bool directOutputOpen = false; |
| 4585 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4586 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4587 | directOutputOpen = true; |
| 4588 | break; |
| 4589 | } |
| 4590 | } |
| 4591 | if (!directOutputOpen) { |
| 4592 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4593 | setMsdPatch(); |
| 4594 | } |
| 4595 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4599 | { |
| 4600 | ALOGV("closeInput(%d)", input); |
| 4601 | |
| 4602 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4603 | if (inputDesc == NULL) { |
| 4604 | ALOGW("closeInput() unknown input %d", input); |
| 4605 | return; |
| 4606 | } |
| 4607 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4608 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4609 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4610 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4611 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4612 | if (index >= 0) { |
| 4613 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4614 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4615 | mAudioPatches.removeItemsAt(index); |
| 4616 | mpClientInterface->onAudioPatchListUpdate(); |
| 4617 | } |
| 4618 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4619 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4620 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4621 | |
| 4622 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4623 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4624 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4625 | SoundTrigger::setCaptureState(false); |
| 4626 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4627 | } |
| 4628 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4629 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4630 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4631 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4632 | { |
| 4633 | SortedVector<audio_io_handle_t> outputs; |
| 4634 | |
| 4635 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4636 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4637 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4638 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4639 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4640 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4641 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4642 | outputs.add(openOutputs.keyAt(i)); |
| 4643 | } |
| 4644 | } |
| 4645 | return outputs; |
| 4646 | } |
| 4647 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4648 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4649 | { |
| 4650 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4651 | // output is suspended before any tracks are moved to it |
| 4652 | checkA2dpSuspend(); |
| 4653 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4654 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4655 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 4656 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4657 | setMsdPatch(); |
| 4658 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4661 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4662 | { |
| 4663 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4664 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4665 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4666 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4667 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4668 | // also take into account external policy-related changes: add all outputs which are |
| 4669 | // associated with policies in the "before" and "after" output vectors |
| 4670 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4671 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4672 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4673 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4674 | srcOutputs.add(desc->mIoHandle); |
| 4675 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4676 | } |
| 4677 | } |
| 4678 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4679 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4680 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4681 | dstOutputs.add(desc->mIoHandle); |
| 4682 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4683 | } |
| 4684 | } |
| 4685 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4686 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4687 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4688 | // audio from invalidated tracks will be rendered when unmuting |
| 4689 | uint32_t maxLatency = 0; |
| 4690 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4691 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4692 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4693 | maxLatency = desc->latency(); |
| 4694 | } |
| 4695 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4696 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4697 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4698 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4699 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4700 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4701 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4702 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4703 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4704 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4705 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4706 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4707 | if (source != 0){ |
| 4708 | connectAudioSource(source); |
| 4709 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | // Move effects associated to this strategy from previous output to new output |
| 4713 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4714 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4715 | } |
| 4716 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4717 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4718 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4719 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4720 | } |
| 4721 | } |
| 4722 | } |
| 4723 | } |
| 4724 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4725 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4727 | 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] | 4728 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4729 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4730 | 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] | 4731 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4732 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4733 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4734 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4735 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4736 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4737 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4738 | } |
| 4739 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4740 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4741 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4742 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4743 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4744 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4745 | return; |
| 4746 | } |
| 4747 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4748 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4749 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4750 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4751 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4752 | |
| 4753 | // if suspended, restore A2DP output if: |
| 4754 | // ((SCO device is NOT connected) || |
| 4755 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4756 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4757 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4758 | // if not suspended, suspend A2DP output if: |
| 4759 | // (SCO device is connected) && |
| 4760 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4761 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4762 | // |
| 4763 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4764 | if (!isScoConnected || |
| 4765 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4766 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4767 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4768 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4769 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4770 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4771 | |
| 4772 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4773 | mA2dpSuspended = false; |
| 4774 | } |
| 4775 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4776 | if (isScoConnected && |
| 4777 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4778 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4779 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4780 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4781 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4782 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4783 | |
| 4784 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4785 | mA2dpSuspended = true; |
| 4786 | } |
| 4787 | } |
| 4788 | } |
| 4789 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4790 | template <class IoDescriptor, class Filter> |
| 4791 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4792 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4793 | { |
| 4794 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4795 | auto activeClientsWithRoute = |
| 4796 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4797 | active = activeClients.size() > 0; |
| 4798 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4799 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4800 | } |
| 4801 | return nullptr; |
| 4802 | } |
| 4803 | |
| 4804 | template <class IoCollection, class Filter> |
| 4805 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4806 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4807 | { |
| 4808 | sp<DeviceDescriptor> device; |
| 4809 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4810 | auto desc = ioCollection.valueAt(i); |
| 4811 | bool active; |
| 4812 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4813 | if (active && curDevice == nullptr) { |
| 4814 | return nullptr; |
| 4815 | } else if (curDevice != nullptr) { |
| 4816 | device = curDevice; |
| 4817 | } |
| 4818 | } |
| 4819 | return device; |
| 4820 | } |
| 4821 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4822 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4823 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4824 | { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4825 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4826 | if (index >= 0) { |
| 4827 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4828 | if (patchDesc->mUid != mUidCached) { |
| 4829 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4830 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4831 | return outputDesc->device(); |
| 4832 | } |
| 4833 | } |
| 4834 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4835 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4836 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4837 | bool active; // unused |
| 4838 | sp<DeviceDescriptor> deviceDesc = |
| 4839 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
| 4840 | if (deviceDesc != nullptr) { |
| 4841 | return deviceDesc->type(); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4844 | // 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] | 4845 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4846 | // use device for strategy enforced audible |
| 4847 | // 2: we are in call or the strategy phone is active on the output: |
| 4848 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4849 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4850 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4851 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4852 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4853 | // 5: the strategy accessibility is active on the output: |
| 4854 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4855 | // 6: the strategy "respectful" sonification is active on the output: |
| 4856 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4857 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4858 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4859 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4860 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4861 | // 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] | 4862 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4863 | |
| 4864 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4865 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4866 | // as opposed to all streams on the same audio HAL module. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4867 | audio_devices_t device = AUDIO_DEVICE_NONE; |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4868 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4869 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4870 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4871 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4872 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4873 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4874 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4875 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4876 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4877 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4878 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4879 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4880 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4881 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4882 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4883 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4884 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4885 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4886 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4887 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4888 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4889 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4890 | } |
| 4891 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4892 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4893 | return device; |
| 4894 | } |
| 4895 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4896 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4897 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4898 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4899 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4900 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4901 | if (index >= 0) { |
| 4902 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4903 | if (patchDesc->mUid != mUidCached) { |
| 4904 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4905 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4906 | return inputDesc->mDevice; |
| 4907 | } |
| 4908 | } |
| 4909 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4910 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4911 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4912 | bool active; |
| 4913 | sp<DeviceDescriptor> deviceDesc = |
| 4914 | findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4915 | if (deviceDesc != nullptr) { |
| 4916 | return deviceDesc->type(); |
| 4917 | } |
| 4918 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4919 | // If we are not in call and no client is active on this input, this methods returns |
| 4920 | // AUDIO_DEVICE_NONE, causing the patch on the input stream to be released. |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 4921 | audio_source_t source = inputDesc->source(); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4922 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4923 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4924 | } |
| 4925 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4926 | device = getDeviceAndMixForInputSource(source); |
| 4927 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4928 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4929 | return device; |
| 4930 | } |
| 4931 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4932 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4933 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4934 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4935 | } |
| 4936 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4937 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4938 | return (uint32_t)getStrategy(stream); |
| 4939 | } |
| 4940 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4941 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4942 | // By checking the range of stream before calling getStrategy, we avoid |
| 4943 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4944 | // 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] | 4945 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4946 | return AUDIO_DEVICE_NONE; |
| 4947 | } |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4948 | audio_devices_t activeDevices = AUDIO_DEVICE_NONE; |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4949 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4950 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4951 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4952 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4953 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4954 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4955 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4956 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4957 | devices |= curDevices; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4958 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4959 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4960 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4961 | activeDevices |= outputDesc->device(); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4962 | } |
| 4963 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4965 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4966 | // Favor devices selected on active streams if any to report correct device in case of |
| 4967 | // explicit device selection |
| 4968 | if (activeDevices != AUDIO_DEVICE_NONE) { |
| 4969 | devices = activeDevices; |
| 4970 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4971 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4972 | and doesn't really need to.*/ |
| 4973 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4974 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4975 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4976 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4977 | return devices; |
| 4978 | } |
| 4979 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4980 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4981 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4982 | 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] | 4983 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4984 | } |
| 4985 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4986 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4987 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4988 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4989 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4990 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4991 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4992 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4993 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4994 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4995 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4996 | } |
| 4997 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4998 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4999 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5000 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5001 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 5002 | updateDevicesAndOutputs(); |
| 5003 | break; |
| 5004 | default: |
| 5005 | break; |
| 5006 | } |
| 5007 | } |
| 5008 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5009 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5010 | |
| 5011 | // skip beacon mute management if a dedicated TTS output is available |
| 5012 | if (mTtsOutputAvailable) { |
| 5013 | return 0; |
| 5014 | } |
| 5015 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5016 | switch(event) { |
| 5017 | case STARTING_OUTPUT: |
| 5018 | mBeaconMuteRefCount++; |
| 5019 | break; |
| 5020 | case STOPPING_OUTPUT: |
| 5021 | if (mBeaconMuteRefCount > 0) { |
| 5022 | mBeaconMuteRefCount--; |
| 5023 | } |
| 5024 | break; |
| 5025 | case STARTING_BEACON: |
| 5026 | mBeaconPlayingRefCount++; |
| 5027 | break; |
| 5028 | case STOPPING_BEACON: |
| 5029 | if (mBeaconPlayingRefCount > 0) { |
| 5030 | mBeaconPlayingRefCount--; |
| 5031 | } |
| 5032 | break; |
| 5033 | } |
| 5034 | |
| 5035 | if (mBeaconMuteRefCount > 0) { |
| 5036 | // any playback causes beacon to be muted |
| 5037 | return setBeaconMute(true); |
| 5038 | } else { |
| 5039 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5040 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5041 | } |
| 5042 | } |
| 5043 | |
| 5044 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5045 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5046 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5047 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5048 | if (mBeaconMuted != mute) { |
| 5049 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5050 | ALOGV("\t muting %d", mute); |
| 5051 | uint32_t maxLatency = 0; |
| 5052 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5053 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5054 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5055 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5056 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5057 | const uint32_t latency = desc->latency() * 2; |
| 5058 | if (latency > maxLatency) { |
| 5059 | maxLatency = latency; |
| 5060 | } |
| 5061 | } |
| 5062 | mBeaconMuted = mute; |
| 5063 | return maxLatency; |
| 5064 | } |
| 5065 | return 0; |
| 5066 | } |
| 5067 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5068 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5069 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5070 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5071 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5072 | // case the last active client route is used |
| 5073 | sp<DeviceDescriptor> deviceDesc = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5074 | if (deviceDesc != nullptr) { |
| 5075 | return deviceDesc->type(); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5076 | } |
| 5077 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5078 | if (fromCache) { |
| 5079 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5080 | strategy, mDeviceForStrategy[strategy]); |
| 5081 | return mDeviceForStrategy[strategy]; |
| 5082 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5083 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5084 | } |
| 5085 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5086 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5087 | { |
| 5088 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5089 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5090 | } |
| 5091 | mPreviousOutputs = mOutputs; |
| 5092 | } |
| 5093 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5094 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5095 | audio_devices_t prevDevice, |
| 5096 | uint32_t delayMs) |
| 5097 | { |
| 5098 | // mute/unmute strategies using an incompatible device combination |
| 5099 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5100 | // if unmuting, unmute only after the specified delay |
| 5101 | if (outputDesc->isDuplicated()) { |
| 5102 | return 0; |
| 5103 | } |
| 5104 | |
| 5105 | uint32_t muteWaitMs = 0; |
| 5106 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5107 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5108 | |
| 5109 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5110 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5111 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5112 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5113 | bool doMute = false; |
| 5114 | |
| 5115 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5116 | doMute = true; |
| 5117 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5118 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5119 | doMute = true; |
| 5120 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5121 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5122 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5123 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5124 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5125 | // skip output if it does not share any device with current output |
| 5126 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5127 | == AUDIO_DEVICE_NONE) { |
| 5128 | continue; |
| 5129 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5130 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5131 | mute ? "muting" : "unmuting", i, curDevice); |
| 5132 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5133 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5134 | if (mute) { |
| 5135 | // FIXME: should not need to double latency if volume could be applied |
| 5136 | // immediately by the audioflinger mixer. We must account for the delay |
| 5137 | // between now and the next time the audioflinger thread for this output |
| 5138 | // will process a buffer (which corresponds to one buffer size, |
| 5139 | // usually 1/2 or 1/4 of the latency). |
| 5140 | if (muteWaitMs < desc->latency() * 2) { |
| 5141 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5142 | } |
| 5143 | } |
| 5144 | } |
| 5145 | } |
| 5146 | } |
| 5147 | } |
| 5148 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5149 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5150 | // different per device volumes |
| 5151 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5152 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5153 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5154 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5155 | if (muteWaitMs < tempMuteWaitMs) { |
| 5156 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5157 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5158 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5159 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5160 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5161 | // make sure that we do not start the temporary mute period too early in case of |
| 5162 | // delayed device change |
| 5163 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5164 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5165 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5166 | } |
| 5167 | } |
| 5168 | } |
| 5169 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5170 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5171 | if (muteWaitMs > delayMs) { |
| 5172 | muteWaitMs -= delayMs; |
| 5173 | usleep(muteWaitMs * 1000); |
| 5174 | return muteWaitMs; |
| 5175 | } |
| 5176 | return 0; |
| 5177 | } |
| 5178 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5179 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5180 | audio_devices_t device, |
| 5181 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5182 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5183 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5184 | const char *address, |
| 5185 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5186 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5187 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5188 | AudioParameter param; |
| 5189 | uint32_t muteWaitMs; |
| 5190 | |
| 5191 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5192 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5193 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5194 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5195 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5196 | return muteWaitMs; |
| 5197 | } |
| 5198 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5199 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5200 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5201 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5202 | return 0; |
| 5203 | } |
| 5204 | |
| 5205 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5206 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | |
| 5208 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5209 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5210 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5211 | |
| 5212 | if (device != AUDIO_DEVICE_NONE) { |
| 5213 | outputDesc->mDevice = device; |
| 5214 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5215 | |
| 5216 | // if the outputs are not materially active, there is no need to mute. |
| 5217 | if (requiresMuteCheck) { |
| 5218 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5219 | } else { |
| 5220 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5221 | muteWaitMs = 0; |
| 5222 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5223 | |
| 5224 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5225 | // the requested device is AUDIO_DEVICE_NONE |
| 5226 | // OR the requested device is the same as current device |
| 5227 | // AND force is not specified |
| 5228 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5229 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5230 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5231 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5232 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5233 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5234 | return muteWaitMs; |
| 5235 | } |
| 5236 | |
| 5237 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5238 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5239 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5240 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5241 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5242 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5243 | DeviceVector deviceList; |
| 5244 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5245 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5246 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5247 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5248 | device, String8(address)); |
| 5249 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5250 | } |
| 5251 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5252 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5253 | PatchBuilder patchBuilder; |
| 5254 | patchBuilder.addSource(outputDesc); |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 5255 | ALOG_ASSERT(deviceList.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 5256 | for (const auto &device : deviceList) { |
| 5257 | patchBuilder.addSink(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5258 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5259 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5260 | } |
| 5261 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5262 | |
| 5263 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5264 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5265 | |
| 5266 | return muteWaitMs; |
| 5267 | } |
| 5268 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5269 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5270 | int delayMs, |
| 5271 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5272 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5273 | ssize_t index; |
| 5274 | if (patchHandle) { |
| 5275 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5276 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5277 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5278 | } |
| 5279 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5280 | return INVALID_OPERATION; |
| 5281 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5282 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5283 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5284 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5285 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5286 | removeAudioPatch(patchDesc->mHandle); |
| 5287 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5288 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5289 | return status; |
| 5290 | } |
| 5291 | |
| 5292 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5293 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5294 | bool force, |
| 5295 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5296 | { |
| 5297 | status_t status = NO_ERROR; |
| 5298 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5299 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5300 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5301 | inputDesc->mDevice = device; |
| 5302 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5303 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5304 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5305 | PatchBuilder patchBuilder; |
| 5306 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5307 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5308 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5309 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5310 | auto result = usecase; |
| 5311 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5312 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5313 | } |
| 5314 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5315 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5316 | addSource(deviceList.itemAt(0)); |
| 5317 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5318 | } |
| 5319 | } |
| 5320 | return status; |
| 5321 | } |
| 5322 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5323 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5324 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5325 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5326 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5327 | ssize_t index; |
| 5328 | if (patchHandle) { |
| 5329 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5330 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5331 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5332 | } |
| 5333 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5334 | return INVALID_OPERATION; |
| 5335 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5336 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5337 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5338 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5339 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5340 | removeAudioPatch(patchDesc->mHandle); |
| 5341 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5342 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5343 | return status; |
| 5344 | } |
| 5345 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5346 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5347 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5348 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5349 | audio_format_t& format, |
| 5350 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5351 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5352 | { |
| 5353 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5354 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5355 | // |
| 5356 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5357 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5358 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5359 | sp<IOProfile> firstInexact; |
| 5360 | uint32_t updatedSamplingRate = 0; |
| 5361 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5362 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5363 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5364 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5365 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5366 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5367 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5368 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5369 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5370 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5371 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5372 | &channelMask /*updatedChannelMask*/, |
| 5373 | // FIXME ugly cast |
| 5374 | (audio_output_flags_t) flags, |
| 5375 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5376 | return profile; |
| 5377 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5378 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5379 | samplingRate, |
| 5380 | &updatedSamplingRate, |
| 5381 | format, |
| 5382 | &updatedFormat, |
| 5383 | channelMask, |
| 5384 | &updatedChannelMask, |
| 5385 | // FIXME ugly cast |
| 5386 | (audio_output_flags_t) flags, |
| 5387 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5388 | firstInexact = profile; |
| 5389 | } |
| 5390 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5391 | } |
| 5392 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5393 | if (firstInexact != nullptr) { |
| 5394 | samplingRate = updatedSamplingRate; |
| 5395 | format = updatedFormat; |
| 5396 | channelMask = updatedChannelMask; |
| 5397 | return firstInexact; |
| 5398 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5399 | return NULL; |
| 5400 | } |
| 5401 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5402 | |
| 5403 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5404 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5405 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5406 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5407 | // case the last active client route is used |
| 5408 | sp<DeviceDescriptor> deviceDesc = |
| 5409 | findPreferredDevice(mInputs, inputSource, mAvailableInputDevices); |
| 5410 | if (deviceDesc != nullptr) { |
| 5411 | return deviceDesc->type(); |
| 5412 | } |
| 5413 | |
| 5414 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5415 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5416 | audio_devices_t selectedDeviceFromMix = |
| 5417 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5418 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5419 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5420 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5421 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5422 | return getDeviceForInputSource(inputSource); |
| 5423 | } |
| 5424 | |
| 5425 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5426 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5427 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5428 | } |
| 5429 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5430 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5431 | int index, |
| 5432 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5433 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5434 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5435 | |
| 5436 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5437 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5438 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5439 | // the ringtone volume |
| 5440 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5441 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5442 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5443 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5444 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5445 | } |
| 5446 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5447 | // in-call: always cap volume by voice volume + some low headroom |
| 5448 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5449 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5450 | switch (stream) { |
| 5451 | case AUDIO_STREAM_SYSTEM: |
| 5452 | case AUDIO_STREAM_RING: |
| 5453 | case AUDIO_STREAM_MUSIC: |
| 5454 | case AUDIO_STREAM_ALARM: |
| 5455 | case AUDIO_STREAM_NOTIFICATION: |
| 5456 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5457 | case AUDIO_STREAM_DTMF: |
| 5458 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5459 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5460 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5461 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5462 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5463 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5464 | if (volumeDB > maxVoiceVolDb) { |
| 5465 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5466 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5467 | volumeDB = maxVoiceVolDb; |
| 5468 | } |
| 5469 | } break; |
| 5470 | default: |
| 5471 | break; |
| 5472 | } |
| 5473 | } |
| 5474 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5475 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5476 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5477 | // - always attenuate notifications volume by 6dB |
| 5478 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5479 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5480 | // - if music is playing, always limit the volume to current music volume, |
| 5481 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5482 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5483 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5484 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5485 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5486 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5487 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5488 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5489 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5490 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5491 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5492 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5493 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5494 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5495 | // when the phone is ringing we must consider that music could have been paused just before |
| 5496 | // by the music application and behave as if music was active if the last music track was |
| 5497 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5498 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5499 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5500 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5501 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5502 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5503 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5504 | musicDevice), |
| 5505 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5506 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5507 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5508 | if (volumeDB > minVolDB) { |
| 5509 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5510 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5511 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5512 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5513 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5514 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5515 | // intended to be played |
| 5516 | if ((volumeDB > -96.0f) && |
| 5517 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5518 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5519 | stream, device, |
| 5520 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5521 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5522 | } |
| 5523 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5524 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5525 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5526 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5527 | } |
| 5528 | } |
| 5529 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5530 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5531 | } |
| 5532 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5533 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5534 | audio_stream_type_t srcStream, |
| 5535 | audio_stream_type_t dstStream) |
| 5536 | { |
| 5537 | if (srcStream == dstStream) { |
| 5538 | return srcIndex; |
| 5539 | } |
| 5540 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5541 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5542 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5543 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5544 | |
| 5545 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5546 | } |
| 5547 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5548 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5549 | int index, |
| 5550 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5551 | audio_devices_t device, |
| 5552 | int delayMs, |
| 5553 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5554 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5555 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5556 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5557 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5558 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5559 | return NO_ERROR; |
| 5560 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5561 | audio_policy_forced_cfg_t forceUseForComm = |
| 5562 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5563 | // 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] | 5564 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5565 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5566 | 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] | 5567 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5568 | return INVALID_OPERATION; |
| 5569 | } |
| 5570 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5571 | if (device == AUDIO_DEVICE_NONE) { |
| 5572 | device = outputDesc->device(); |
| 5573 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5574 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5575 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5576 | if (outputDesc->isFixedVolume(device) || |
| 5577 | // Force VoIP volume to max for bluetooth SCO |
| 5578 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5579 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5580 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5581 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5582 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5583 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5584 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5585 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5586 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5587 | float voiceVolume; |
| 5588 | // 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] | 5589 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5590 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5591 | } else { |
| 5592 | voiceVolume = 1.0; |
| 5593 | } |
| 5594 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5595 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5596 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5597 | mLastVoiceVolume = voiceVolume; |
| 5598 | } |
| 5599 | } |
| 5600 | |
| 5601 | return NO_ERROR; |
| 5602 | } |
| 5603 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5604 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5605 | audio_devices_t device, |
| 5606 | int delayMs, |
| 5607 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5608 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5609 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5610 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5611 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5612 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5613 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5614 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5615 | device, |
| 5616 | delayMs, |
| 5617 | force); |
| 5618 | } |
| 5619 | } |
| 5620 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5621 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5622 | bool on, |
| 5623 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5624 | int delayMs, |
| 5625 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5626 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5627 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5628 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5629 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5630 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5631 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5632 | } |
| 5633 | } |
| 5634 | } |
| 5635 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5636 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5637 | bool on, |
| 5638 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5639 | int delayMs, |
| 5640 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5641 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5642 | if (device == AUDIO_DEVICE_NONE) { |
| 5643 | device = outputDesc->device(); |
| 5644 | } |
| 5645 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5646 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5647 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5648 | |
| 5649 | if (on) { |
| 5650 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5651 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5652 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5653 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5654 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5655 | } |
| 5656 | } |
| 5657 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5658 | outputDesc->mMuteCount[stream]++; |
| 5659 | } else { |
| 5660 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5661 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5662 | return; |
| 5663 | } |
| 5664 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5665 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5666 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5667 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5668 | device, |
| 5669 | delayMs); |
| 5670 | } |
| 5671 | } |
| 5672 | } |
| 5673 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5674 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5675 | { |
| 5676 | // flags to stream type mapping |
| 5677 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5678 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5679 | } |
| 5680 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5681 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5682 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5683 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5684 | return AUDIO_STREAM_TTS; |
| 5685 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5686 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5687 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5688 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5689 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5690 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5691 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5692 | // has flags that map to a strategy? |
| 5693 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5694 | return true; |
| 5695 | } |
| 5696 | |
| 5697 | // has known usage? |
| 5698 | switch (paa->usage) { |
| 5699 | case AUDIO_USAGE_UNKNOWN: |
| 5700 | case AUDIO_USAGE_MEDIA: |
| 5701 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5702 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5703 | case AUDIO_USAGE_ALARM: |
| 5704 | case AUDIO_USAGE_NOTIFICATION: |
| 5705 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5706 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5707 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5708 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5709 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5710 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5711 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5712 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5713 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5714 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5715 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5716 | break; |
| 5717 | default: |
| 5718 | return false; |
| 5719 | } |
| 5720 | return true; |
| 5721 | } |
| 5722 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5723 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5724 | routing_strategy strategy, uint32_t inPastMs, |
| 5725 | nsecs_t sysTime) const |
| 5726 | { |
| 5727 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5728 | sysTime = systemTime(); |
| 5729 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5730 | 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] | 5731 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5732 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5733 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5734 | return true; |
| 5735 | } |
| 5736 | } |
| 5737 | return false; |
| 5738 | } |
| 5739 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5740 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5741 | routing_strategy strategy, uint32_t inPastMs, |
| 5742 | nsecs_t sysTime) const |
| 5743 | { |
| 5744 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5745 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5746 | if (outputDesc->sharesHwModuleWith(desc) |
| 5747 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5748 | return true; |
| 5749 | } |
| 5750 | } |
| 5751 | return false; |
| 5752 | } |
| 5753 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5754 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5755 | { |
| 5756 | return mEngine->getForceUse(usage); |
| 5757 | } |
| 5758 | |
| 5759 | bool AudioPolicyManager::isInCall() |
| 5760 | { |
| 5761 | return isStateInCall(mEngine->getPhoneState()); |
| 5762 | } |
| 5763 | |
| 5764 | bool AudioPolicyManager::isStateInCall(int state) |
| 5765 | { |
| 5766 | return is_state_in_call(state); |
| 5767 | } |
| 5768 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5769 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5770 | { |
| 5771 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5772 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5773 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5774 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5775 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5776 | } |
| 5777 | } |
| 5778 | |
| 5779 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5780 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5781 | bool release = false; |
| 5782 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5783 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5784 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5785 | source->ext.device.type == deviceDesc->type()) { |
| 5786 | release = true; |
| 5787 | } |
| 5788 | } |
| 5789 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5790 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5791 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5792 | sink->ext.device.type == deviceDesc->type()) { |
| 5793 | release = true; |
| 5794 | } |
| 5795 | } |
| 5796 | if (release) { |
| 5797 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5798 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5799 | } |
| 5800 | } |
| 5801 | } |
| 5802 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5803 | void AudioPolicyManager::modifySurroundFormats( |
| 5804 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5805 | std::unordered_set<audio_format_t> enforcedSurround( |
| 5806 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5807 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 5808 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5809 | allSurround.insert(pair.first); |
| 5810 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 5811 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5812 | |
| 5813 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5814 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5815 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5816 | // This is the resulting set of formats depending on the surround mode: |
| 5817 | // 'all surround' = allSurround |
| 5818 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 5819 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 5820 | // 'manual surround' = mManualSurroundFormats |
| 5821 | // AUTO: formats v 'enforced surround' |
| 5822 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 5823 | // NEVER: formats ^ 'non-surround' |
| 5824 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5825 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5826 | std::unordered_set<audio_format_t> formatSet; |
| 5827 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 5828 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5829 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5830 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5831 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5832 | formatSet.insert(*formatIter); |
| 5833 | } |
| 5834 | } |
| 5835 | } else { |
| 5836 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 5837 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5838 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5839 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5840 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5841 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5842 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 5843 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 5844 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5845 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5846 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 5847 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5848 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5849 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5850 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5851 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5852 | for (const auto& format : formatSet) { |
| 5853 | formatsPtr->push(format); |
| 5854 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5855 | } |
| 5856 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5857 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5858 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5859 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5860 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5861 | |
| 5862 | // If NEVER, then remove support for channelMasks > stereo. |
| 5863 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5864 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5865 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5866 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5867 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5868 | channelMasks.removeAt(maskIndex); |
| 5869 | } else { |
| 5870 | maskIndex++; |
| 5871 | } |
| 5872 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5873 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5874 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5875 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5876 | bool supports5dot1 = false; |
| 5877 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5878 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5879 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5880 | supports5dot1 = true; |
| 5881 | break; |
| 5882 | } |
| 5883 | } |
| 5884 | // If not then add 5.1 support. |
| 5885 | if (!supports5dot1) { |
| 5886 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5887 | ALOGI("%s: force MANUAL or ALWAYS, so adding channelMask for 5.1 surround", __func__); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5888 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5889 | } |
| 5890 | } |
| 5891 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5892 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5893 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5894 | AudioProfileVector &profiles) |
| 5895 | { |
| 5896 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5897 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5898 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5899 | // Format MUST be checked first to update the list of AudioProfile |
| 5900 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5901 | reply = mpClientInterface->getParameters( |
| 5902 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5903 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5904 | AudioParameter repliedParameters(reply); |
| 5905 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5906 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5907 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5908 | return; |
| 5909 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5910 | FormatVector formats = formatsFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5911 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5912 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5913 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5914 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5915 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5916 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5917 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5918 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5919 | ChannelsVector channelMasks; |
| 5920 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5921 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5922 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5923 | |
| 5924 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5925 | reply = mpClientInterface->getParameters( |
| 5926 | ioHandle, |
| 5927 | requestedParameters.toString() + ";" + |
| 5928 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5929 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5930 | AudioParameter repliedParameters(reply); |
| 5931 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5932 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5933 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5934 | } |
| 5935 | } |
| 5936 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5937 | reply = mpClientInterface->getParameters(ioHandle, |
| 5938 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5939 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5940 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5941 | AudioParameter repliedParameters(reply); |
| 5942 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5943 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5944 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5945 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5946 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5947 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5948 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5949 | } |
| 5950 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5951 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5952 | } |
| 5953 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5954 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5955 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5956 | audio_patch_handle_t *patchHandle, |
| 5957 | AudioIODescriptorInterface *ioDescriptor, |
| 5958 | const struct audio_patch *patch, |
| 5959 | int delayMs) |
| 5960 | { |
| 5961 | ssize_t index = mAudioPatches.indexOfKey( |
| 5962 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 5963 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 5964 | sp<AudioPatch> patchDesc; |
| 5965 | status_t status = installPatch( |
| 5966 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 5967 | if (status == NO_ERROR) { |
| 5968 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 5969 | } |
| 5970 | return status; |
| 5971 | } |
| 5972 | |
| 5973 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5974 | ssize_t index, |
| 5975 | audio_patch_handle_t *patchHandle, |
| 5976 | const struct audio_patch *patch, |
| 5977 | int delayMs, |
| 5978 | uid_t uid, |
| 5979 | sp<AudioPatch> *patchDescPtr) |
| 5980 | { |
| 5981 | sp<AudioPatch> patchDesc; |
| 5982 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5983 | if (index >= 0) { |
| 5984 | patchDesc = mAudioPatches.valueAt(index); |
| 5985 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5986 | } |
| 5987 | |
| 5988 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 5989 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 5990 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 5991 | if (status == NO_ERROR) { |
| 5992 | if (index < 0) { |
| 5993 | patchDesc = new AudioPatch(patch, uid); |
| 5994 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5995 | } else { |
| 5996 | patchDesc->mPatch = *patch; |
| 5997 | } |
| 5998 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 5999 | if (patchHandle) { |
| 6000 | *patchHandle = patchDesc->mHandle; |
| 6001 | } |
| 6002 | nextAudioPortGeneration(); |
| 6003 | mpClientInterface->onAudioPatchListUpdate(); |
| 6004 | } |
| 6005 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6006 | return status; |
| 6007 | } |
| 6008 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6009 | } // namespace android |