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...) |
| 152 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 153 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 154 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != 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, |
| 158 | devDesc->mAddress); |
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 | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 181 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
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 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 186 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
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...) |
| 268 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 269 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 270 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 271 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 272 | devDesc->mAddress); |
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 | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 297 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 298 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 299 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
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); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 913 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 914 | : AUDIO_PORT_HANDLE_NONE; |
| 915 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 916 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 917 | |
| 918 | return NO_ERROR; |
| 919 | } |
| 920 | |
| 921 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 922 | audio_io_handle_t *output, |
| 923 | audio_session_t session, |
| 924 | audio_stream_type_t *stream, |
| 925 | uid_t uid, |
| 926 | const audio_config_t *config, |
| 927 | audio_output_flags_t *flags, |
| 928 | audio_port_handle_t *selectedDeviceId, |
| 929 | audio_port_handle_t *portId) |
| 930 | { |
| 931 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 932 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 933 | return INVALID_OPERATION; |
| 934 | } |
| 935 | const audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
| 936 | audio_attributes_t resultAttr; |
| 937 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
| 938 | config, flags, selectedDeviceId); |
| 939 | if (status != NO_ERROR) { |
| 940 | return status; |
| 941 | } |
| 942 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 943 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 944 | .format = config->format, |
| 945 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 946 | *portId = AudioPort::getNextUniqueId(); |
| 947 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 948 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 949 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 950 | requestedDeviceId, *stream, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 951 | getStrategyForAttr(&resultAttr), |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 952 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 953 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 954 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 955 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 956 | ALOGV("%s returns output %d selectedDeviceId %d for port ID %d", |
| 957 | __func__, *output, requestedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 958 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 959 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 963 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 964 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 965 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 966 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 967 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 968 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 969 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 970 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 971 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 972 | // open a direct output if required by specified parameters |
| 973 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 974 | // and all common behaviors are driven by checking only the direct flag |
| 975 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 976 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 977 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 978 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 979 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 980 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 981 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 982 | // only allow deep buffering for music stream type |
| 983 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 984 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 985 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 986 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 987 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 988 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 989 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 990 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 991 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 992 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 993 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 994 | audio_is_linear_pcm(config->format) && |
| 995 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 996 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 997 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 998 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 999 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1000 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1001 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1002 | sp<IOProfile> profile; |
| 1003 | |
| 1004 | // 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] | 1005 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1006 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1007 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1008 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1009 | goto non_direct_output; |
| 1010 | } |
| 1011 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1012 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1013 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1014 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1015 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1016 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1017 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1018 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1019 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1020 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1021 | profile = getProfileForOutput(device, |
| 1022 | config->sample_rate, |
| 1023 | config->format, |
| 1024 | config->channel_mask, |
| 1025 | (audio_output_flags_t)*flags, |
| 1026 | true /* directOnly */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1029 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1030 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1031 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1032 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1033 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1034 | // reuse direct output if currently open by the same client |
| 1035 | // and configured with same parameters |
| 1036 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1037 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1038 | (config->channel_mask == desc->mChannelMask) && |
| 1039 | (session == desc->mDirectClientSession)) { |
| 1040 | desc->mDirectOpenCount++; |
| 1041 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 1042 | mOutputs.keyAt(i), session); |
| 1043 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1047 | |
| 1048 | if (!profile->canOpenNewIo()) { |
| 1049 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1050 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1051 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1052 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1053 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1054 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1055 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1056 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 1057 | : String8(""); |
| 1058 | |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 1059 | // MSD patch may be using the only output stream that can service this request. Release |
| 1060 | // MSD patch to prioritize this request over any active output on MSD. |
| 1061 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 1062 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1063 | const auto& patch = msdPatches[i]; |
| 1064 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1065 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1066 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 1067 | (sink->ext.device.type & device) != AUDIO_DEVICE_NONE && |
| 1068 | (address.isEmpty() || strncmp(sink->ext.device.address, address.string(), |
| 1069 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 1070 | releaseAudioPatch(patch->mHandle, mUidCached); |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1076 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1077 | |
| 1078 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1079 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1080 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1081 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1082 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1083 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1084 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1085 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1086 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1087 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1088 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1089 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1090 | // 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] | 1091 | if (audio_is_linear_pcm(config->format) && |
| 1092 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1093 | goto non_direct_output; |
| 1094 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1095 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1096 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1097 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1098 | outputDesc->mDirectClientSession = session; |
| 1099 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1100 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1101 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1102 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1103 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1104 | return output; |
| 1105 | } |
| 1106 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1107 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1108 | |
| 1109 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1110 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1111 | 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] | 1112 | return AUDIO_IO_HANDLE_NONE; |
| 1113 | } |
| 1114 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1115 | // ignoring channel mask due to downmix capability in mixer |
| 1116 | |
| 1117 | // open a non direct output |
| 1118 | |
| 1119 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1120 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1121 | // get which output is suitable for the specified stream. The actual |
| 1122 | // routing change will happen when startOutput() will be called |
| 1123 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1124 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1125 | // 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] | 1126 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1127 | output = selectOutput(outputs, *flags, config->format, |
| 1128 | config->channel_mask, config->sample_rate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1129 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1130 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1131 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1132 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1133 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1134 | return output; |
| 1135 | } |
| 1136 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1137 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1138 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1139 | if (msdModule != 0) { |
| 1140 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1141 | msdModule->getHandle()); |
| 1142 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1143 | } |
| 1144 | return 0; |
| 1145 | } |
| 1146 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1147 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1148 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1149 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1150 | if (msdModule != 0) { |
| 1151 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1152 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1153 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1154 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1155 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1156 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1157 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1158 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | return msdPatches; |
| 1163 | } |
| 1164 | |
| 1165 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1166 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1167 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1168 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1169 | if (msdModule == nullptr) { |
| 1170 | ALOGE("%s() unable to get MSD module", __func__); |
| 1171 | return NO_INIT; |
| 1172 | } |
| 1173 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1174 | if (deviceModule == nullptr) { |
| 1175 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1176 | return NO_INIT; |
| 1177 | } |
| 1178 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1179 | if (inputProfiles.isEmpty()) { |
| 1180 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1181 | return NO_INIT; |
| 1182 | } |
| 1183 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1184 | if (outputProfiles.isEmpty()) { |
| 1185 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1186 | return NO_INIT; |
| 1187 | } |
| 1188 | AudioProfileVector msdProfiles; |
| 1189 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1190 | for (const auto &inProfile : inputProfiles) { |
| 1191 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1192 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1193 | } |
| 1194 | } |
| 1195 | AudioProfileVector deviceProfiles; |
| 1196 | for (const auto &outProfile : outputProfiles) { |
| 1197 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1198 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1199 | } |
| 1200 | } |
| 1201 | struct audio_config_base bestSinkConfig; |
| 1202 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1203 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1204 | &bestSinkConfig); |
| 1205 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1206 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1207 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1208 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1209 | } |
| 1210 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1211 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1212 | sinkConfig->format = bestSinkConfig.format; |
| 1213 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1214 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1215 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1216 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1217 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1218 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1219 | sourceConfig->format = bestSinkConfig.format; |
| 1220 | // Copy input stream directly without any processing (e.g. resampling). |
| 1221 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1222 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1223 | if (hwAvSync) { |
| 1224 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1225 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1226 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1227 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1228 | } |
| 1229 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1230 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1231 | sinkConfig->config_mask |= config_mask; |
| 1232 | sourceConfig->config_mask |= config_mask; |
| 1233 | return NO_ERROR; |
| 1234 | } |
| 1235 | |
| 1236 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1237 | { |
| 1238 | PatchBuilder patchBuilder; |
| 1239 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1240 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1241 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1242 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1243 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1244 | // For now, we just forcefully try with HwAvSync first. |
| 1245 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1246 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1247 | getBestMsdAudioProfileFor( |
| 1248 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1249 | if (res == NO_ERROR) { |
| 1250 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1251 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1252 | } |
| 1253 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1254 | " supporting PCM format conversion.", __func__); |
| 1255 | return patchBuilder; |
| 1256 | } |
| 1257 | |
| 1258 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1259 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1260 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1261 | // Use media strategy for unspecified output device. This should only |
| 1262 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1263 | // therefore invalidate explicit routing requests. |
| 1264 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1265 | } |
| 1266 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1267 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1268 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1269 | if (!msdPatches.isEmpty()) { |
| 1270 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1271 | "The current MSD prototype only supports one output patch"); |
| 1272 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1273 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1274 | return NO_ERROR; |
| 1275 | } |
| 1276 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1277 | } |
| 1278 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1279 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1280 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1281 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1282 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1283 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1284 | return status; |
| 1285 | } |
| 1286 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1287 | 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] | 1288 | audio_output_flags_t flags, |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1289 | audio_format_t format, |
| 1290 | audio_channel_mask_t channelMask, |
| 1291 | uint32_t samplingRate) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1292 | { |
| 1293 | // select one output among several that provide a path to a particular device or set of |
| 1294 | // devices (the list was previously build by getOutputsForDevice()). |
| 1295 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1296 | // 1: the output supporting haptic playback when requesting haptic playback |
| 1297 | // 2: the output with the highest number of requested policy flags |
| 1298 | // 3: the output with the bit depth the closest to the requested one |
| 1299 | // 4: the primary output |
| 1300 | // 5: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1301 | |
| 1302 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1303 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1304 | } |
| 1305 | if (outputs.size() == 1) { |
| 1306 | return outputs[0]; |
| 1307 | } |
| 1308 | |
| 1309 | int maxCommonFlags = 0; |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1310 | const size_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1311 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1312 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1313 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1314 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1315 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1316 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1317 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1318 | for (audio_io_handle_t output : outputs) { |
| 1319 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1320 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1321 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1322 | continue; |
| 1323 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1324 | // If haptic channel is specified, use the haptic output if present. |
| 1325 | // When using haptic output, same audio format and sample rate are required. |
| 1326 | if (hapticChannelCount > 0) { |
| 1327 | // If haptic channel is specified, use the first output that |
| 1328 | // support haptic playback. |
| 1329 | if (audio_channel_count_from_out_mask( |
| 1330 | outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) >= hapticChannelCount |
| 1331 | && format == outputDesc->mFormat |
| 1332 | && samplingRate == outputDesc->mSamplingRate) { |
| 1333 | return output; |
| 1334 | } |
| 1335 | } else { |
| 1336 | // When haptic channel is not specified, skip haptic output. |
| 1337 | if (outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 1338 | continue; |
| 1339 | } |
| 1340 | } |
| 1341 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1342 | // if a valid format is specified, skip output if not compatible |
| 1343 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1344 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1345 | continue; |
| 1346 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1347 | if (AudioPort::isBetterFormatMatch( |
| 1348 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1349 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1350 | bestFormat = outputDesc->mFormat; |
| 1351 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1354 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1355 | if (commonFlags >= maxCommonFlags) { |
| 1356 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1357 | if (format != AUDIO_FORMAT_INVALID |
| 1358 | && AudioPort::isBetterFormatMatch( |
| 1359 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1360 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1361 | bestFormatForFlags = outputDesc->mFormat; |
| 1362 | } |
| 1363 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1364 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1365 | maxCommonFlags = commonFlags; |
| 1366 | bestFormatForFlags = outputDesc->mFormat; |
| 1367 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1368 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1369 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1370 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1371 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1376 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1377 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1378 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1379 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1380 | return outputForFormat; |
| 1381 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1382 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1383 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | return outputs[0]; |
| 1387 | } |
| 1388 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1389 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1390 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1391 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1392 | |
| 1393 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1394 | if (outputDesc == 0) { |
| 1395 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1396 | return BAD_VALUE; |
| 1397 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1398 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1399 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1400 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1401 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1402 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1403 | status_t status = outputDesc->start(); |
| 1404 | if (status != NO_ERROR) { |
| 1405 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1406 | } |
| 1407 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1408 | uint32_t delayMs; |
| 1409 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1410 | |
| 1411 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1412 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1413 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1414 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1415 | if (delayMs != 0) { |
| 1416 | usleep(delayMs * 1000); |
| 1417 | } |
| 1418 | |
| 1419 | return status; |
| 1420 | } |
| 1421 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1422 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1423 | const sp<TrackClientDescriptor>& client, |
| 1424 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1425 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1426 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1427 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1428 | |
| 1429 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1430 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1431 | if (stream == AUDIO_STREAM_TTS) { |
| 1432 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1433 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1434 | return INVALID_OPERATION; |
| 1435 | } else { |
| 1436 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1437 | } |
| 1438 | } else { |
| 1439 | // some playback other than beacon starts |
| 1440 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1441 | } |
| 1442 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1443 | // 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] | 1444 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1445 | bool force = !outputDesc->isActive() && |
| 1446 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1447 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1448 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 1449 | AudioMix *policyMix = NULL; |
| 1450 | const char *address = NULL; |
| 1451 | if (outputDesc->mPolicyMix != NULL) { |
| 1452 | policyMix = outputDesc->mPolicyMix; |
| 1453 | address = policyMix->mDeviceAddress.string(); |
| 1454 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1455 | device = policyMix->mDeviceType; |
| 1456 | } else { |
| 1457 | device = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1458 | } |
| 1459 | } |
| 1460 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1461 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1462 | // It covers a common case when there is no materially active audio |
| 1463 | // and muting would result in unnecessary delay and dropped audio. |
| 1464 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1465 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1466 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | // increment usage count for this stream on the requested output: |
| 1468 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1469 | // 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] | 1470 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1471 | |
| 1472 | if (client->hasPreferredDevice(true)) { |
| 1473 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 1474 | if (device != outputDesc->device()) { |
| 1475 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1476 | } |
| 1477 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1478 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1479 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1480 | selectOutputForMusicEffects(); |
| 1481 | } |
| 1482 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1483 | if (outputDesc->streamActiveCount(stream) == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1484 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1485 | if (device == AUDIO_DEVICE_NONE) { |
| 1486 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1487 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1488 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | routing_strategy strategy = getStrategy(stream); |
| 1490 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1491 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1492 | (beaconMuteLatency > 0); |
| 1493 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1494 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1495 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1496 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1497 | // An output has a shared device if |
| 1498 | // - managed by the same hw module |
| 1499 | // - supports the currently selected device |
| 1500 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1501 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1502 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1503 | // force a device change if any other output is: |
| 1504 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1505 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1506 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1507 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1508 | // 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] | 1509 | // change the device currently selected by the other output. |
| 1510 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1511 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1512 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1513 | force = true; |
| 1514 | } |
| 1515 | // 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] | 1516 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1517 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1518 | const uint32_t latencyMs = desc->latency(); |
| 1519 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1520 | |
| 1521 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1522 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1523 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1524 | |
| 1525 | // Require mute check if another output is on a shared device |
| 1526 | // and currently active to have proper drain and avoid pops. |
| 1527 | // Note restoring AudioTracks onto this output needs to invoke |
| 1528 | // a volume ramp if there is no mute. |
| 1529 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1530 | } |
| 1531 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1532 | |
| 1533 | const uint32_t muteWaitMs = |
| 1534 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1535 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1536 | // apply volume rules for current stream and device if necessary |
| 1537 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1538 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1539 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1540 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1541 | |
| 1542 | // update the outputs if starting an output with a stream that can affect notification |
| 1543 | // routing |
| 1544 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1545 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1546 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1547 | if (strategy == STRATEGY_SONIFICATION) { |
| 1548 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1549 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1550 | |
| 1551 | if (waitMs > muteWaitMs) { |
| 1552 | *delayMs = waitMs - muteWaitMs; |
| 1553 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1554 | |
| 1555 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1556 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1557 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1558 | // change occurs after the MixerThread starts and causes a stream volume |
| 1559 | // glitch. |
| 1560 | // |
| 1561 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1562 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1563 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1564 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1565 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1566 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1567 | } |
| 1568 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1569 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1570 | // of type MIX_TYPE_RECORDERS |
| 1571 | if (audio_is_remote_submix_device(device) && policyMix != NULL && |
| 1572 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1573 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1574 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1575 | address, |
| 1576 | "remote-submix"); |
| 1577 | } |
| 1578 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1579 | return NO_ERROR; |
| 1580 | } |
| 1581 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1582 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1583 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1584 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1585 | |
| 1586 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1587 | if (outputDesc == 0) { |
| 1588 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1589 | return BAD_VALUE; |
| 1590 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1591 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1592 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1593 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1594 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1595 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1596 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1597 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1598 | if (status == NO_ERROR ) { |
| 1599 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1600 | } |
| 1601 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1604 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1605 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1606 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1607 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1608 | audio_stream_type_t stream = client->stream(); |
| 1609 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1610 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1611 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1612 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1613 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1614 | // Automatically disable the remote submix input when output is stopped on a |
| 1615 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1616 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1617 | outputDesc->mPolicyMix != NULL && |
| 1618 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1619 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1620 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1621 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1622 | "remote-submix"); |
| 1623 | } |
| 1624 | } |
| 1625 | bool forceDeviceUpdate = false; |
| 1626 | if (client->hasPreferredDevice(true)) { |
| 1627 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1628 | forceDeviceUpdate = true; |
| 1629 | } |
| 1630 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1631 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1632 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1633 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1634 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1635 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1636 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1637 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1638 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1639 | // the track stop() command is received and at that time the audio track buffer can |
| 1640 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1641 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1642 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1643 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1644 | |
| 1645 | // force restoring the device selection on other active outputs if it differs from the |
| 1646 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1647 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1648 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1649 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1650 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1651 | desc->isActive() && |
| 1652 | outputDesc->sharesHwModuleWith(desc) && |
| 1653 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1654 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1655 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1656 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1657 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1658 | newDevice2, |
| 1659 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1660 | delayMs); |
| 1661 | // re-apply device specific volume if not done by setOutputDevice() |
| 1662 | if (!force) { |
| 1663 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1664 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1668 | handleNotificationRoutingForStream(stream); |
| 1669 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1670 | |
| 1671 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1672 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1673 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1674 | } |
| 1675 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1676 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1677 | selectOutputForMusicEffects(); |
| 1678 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1679 | return NO_ERROR; |
| 1680 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1681 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1682 | return INVALID_OPERATION; |
| 1683 | } |
| 1684 | } |
| 1685 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1686 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1687 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1688 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1689 | |
| 1690 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1691 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1692 | // If an output descriptor is closed due to a device routing change, |
| 1693 | // then there are race conditions with releaseOutput from tracks |
| 1694 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1695 | // destroyed shortly thereafter. |
| 1696 | // |
| 1697 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1698 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1699 | return; |
| 1700 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1701 | |
| 1702 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1703 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1704 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1705 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1706 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1707 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1708 | return; |
| 1709 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1710 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1711 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1712 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1713 | } |
| 1714 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1715 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1716 | // otherwise there may be inaccurate stream reference counts. |
| 1717 | // This is checked in outputDesc->removeClient below. |
| 1718 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1721 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1722 | audio_io_handle_t *input, |
| 1723 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1724 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1725 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1726 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1727 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1728 | input_type_t *inputType, |
| 1729 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1730 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1731 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1732 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1733 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1734 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1735 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1736 | // handle legacy remote submix case where the address was not always specified |
| 1737 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1738 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1739 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1740 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1741 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1742 | sp<AudioInputDescriptor> inputDesc; |
| 1743 | sp<RecordClientDescriptor> clientDesc; |
| 1744 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1745 | bool isSoundTrigger; |
| 1746 | audio_devices_t device; |
| 1747 | |
| 1748 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1749 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1750 | return INVALID_OPERATION; |
| 1751 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1752 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1753 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1754 | inputSource = AUDIO_SOURCE_MIC; |
| 1755 | } |
| 1756 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1757 | // Explicit routing? |
| 1758 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1759 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1760 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1761 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1762 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1763 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1764 | // possible |
| 1765 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1766 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1767 | ssize_t index = mInputs.indexOfKey(*input); |
| 1768 | if (index < 0) { |
| 1769 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1770 | status = BAD_VALUE; |
| 1771 | goto error; |
| 1772 | } |
| 1773 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1774 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1775 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1776 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1777 | status = BAD_VALUE; |
| 1778 | goto error; |
| 1779 | } |
| 1780 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1781 | // 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] | 1782 | // corresponds to a new client and is only permitted from the same UID. |
| 1783 | // 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] | 1784 | if (clients.size() > 1) { |
| 1785 | for (const auto& client : clients) { |
| 1786 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1787 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1788 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1789 | // to an actual client |
| 1790 | if (client == *clients.cbegin()) { |
| 1791 | continue; |
| 1792 | } |
| 1793 | if (uid != client->uid() && !client->isSilenced()) { |
| 1794 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1795 | uid, client->portId(), client->uid()); |
| 1796 | status = INVALID_OPERATION; |
| 1797 | goto error; |
| 1798 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1799 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1800 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1801 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1802 | device = inputDesc->mDevice; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1803 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1804 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1805 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1806 | } |
| 1807 | |
| 1808 | *input = AUDIO_IO_HANDLE_NONE; |
| 1809 | *inputType = API_INPUT_INVALID; |
| 1810 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1811 | halInputSource = inputSource; |
| 1812 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1813 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1814 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1815 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1816 | if (status != NO_ERROR) { |
| 1817 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1818 | } |
| 1819 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1820 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1821 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1822 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1823 | if (deviceDesc != 0) { |
| 1824 | device = deviceDesc->type(); |
| 1825 | } else { |
| 1826 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
| 1827 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1828 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1829 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1830 | status = BAD_VALUE; |
| 1831 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1832 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1833 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1834 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1835 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1836 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1837 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1838 | // know about it and is therefore considered "legacy" |
| 1839 | *inputType = API_INPUT_LEGACY; |
| 1840 | } else { |
| 1841 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1842 | // an external policy |
| 1843 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1844 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1845 | } else if (audio_is_remote_submix_device(device)) { |
| 1846 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1847 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1848 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1849 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1850 | } else { |
| 1851 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1852 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1853 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1854 | } |
| 1855 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1856 | *input = getInputForDevice(device, address, session, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1857 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1858 | policyMix); |
| 1859 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1860 | status = INVALID_OPERATION; |
| 1861 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1862 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1863 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1864 | exit: |
| 1865 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1866 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1867 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1868 | : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1869 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1870 | isSoundTrigger = inputSource == AUDIO_SOURCE_HOTWORD && |
| 1871 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1872 | *portId = AudioPort::getNextUniqueId(); |
| 1873 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1874 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1875 | *attr, *config, requestedDeviceId, |
| 1876 | inputSource,flags, isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1877 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1878 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1879 | |
| 1880 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1881 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1882 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1883 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1884 | |
| 1885 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1886 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
| 1889 | |
| 1890 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1891 | String8 address, |
| 1892 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1893 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1894 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1895 | audio_input_flags_t flags, |
| 1896 | AudioMix *policyMix) |
| 1897 | { |
| 1898 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1899 | audio_source_t halInputSource = inputSource; |
| 1900 | bool isSoundTrigger = false; |
| 1901 | |
| 1902 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1903 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1904 | if (index >= 0) { |
| 1905 | input = mSoundTriggerSessions.valueFor(session); |
| 1906 | isSoundTrigger = true; |
| 1907 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1908 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1909 | } else { |
| 1910 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1911 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1912 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1913 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1914 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1917 | // find a compatible input profile (not necessarily identical in parameters) |
| 1918 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1919 | // sampling rate and flags may be updated by getInputProfile |
| 1920 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1921 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1922 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1923 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1924 | audio_input_flags_t profileFlags = flags; |
| 1925 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1926 | 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] | 1927 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1928 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1929 | profileFlags); |
| 1930 | if (profile != 0) { |
| 1931 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1932 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1933 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1934 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1935 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1936 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1937 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1938 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1939 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1940 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1941 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1942 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1943 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1944 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1945 | if (samplingRate == 0) { |
| 1946 | samplingRate = profileSamplingRate; |
| 1947 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1948 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1949 | if (profile->getModuleHandle() == 0) { |
| 1950 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1951 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1954 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1955 | for (size_t i = 0; i < mInputs.size(); ) { |
| 1956 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 1957 | if (desc->mProfile != profile) { |
| 1958 | continue; |
| 1959 | } |
| 1960 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 1961 | // else |
| 1962 | // reuse input if active client app is not in IDLE state |
| 1963 | // |
| 1964 | RecordClientVector clients = desc->clientsList(); |
| 1965 | bool doClose = false; |
| 1966 | for (const auto& client : clients) { |
| 1967 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 1968 | continue; |
| 1969 | } |
| 1970 | if (client->isSoundTrigger()) { |
| 1971 | if (session == client->session()) { |
| 1972 | return desc->mIoHandle; |
| 1973 | } |
| 1974 | continue; |
| 1975 | } |
| 1976 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 1977 | return desc->mIoHandle; |
| 1978 | } |
| 1979 | doClose = true; |
| 1980 | } |
| 1981 | if (doClose) { |
| 1982 | closeInput(desc->mIoHandle); |
| 1983 | } else { |
| 1984 | i++; |
| 1985 | } |
| 1986 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1987 | } |
| 1988 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1989 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1990 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1991 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1992 | lConfig.sample_rate = profileSamplingRate; |
| 1993 | lConfig.channel_mask = profileChannelMask; |
| 1994 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1995 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1996 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1997 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1998 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1999 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 2000 | } |
| 2001 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2002 | status_t status = inputDesc->open(&lConfig, device, address, |
| 2003 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2004 | |
| 2005 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2006 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2007 | (profileSamplingRate != lConfig.sample_rate) || |
| 2008 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2009 | (profileChannelMask != lConfig.channel_mask)) { |
| 2010 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2011 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2012 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2013 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2014 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2015 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2016 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2017 | } |
| 2018 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2019 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2020 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2021 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2022 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2023 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2024 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2027 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2028 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2029 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2030 | |
| 2031 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2032 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2033 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2034 | return BAD_VALUE; |
| 2035 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2036 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2037 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2038 | if (client->active()) { |
| 2039 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2040 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2043 | audio_session_t session = client->session(); |
| 2044 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2045 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2046 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2047 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2048 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2049 | status_t status = inputDesc->start(); |
| 2050 | if (status != NO_ERROR) { |
| 2051 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2052 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2053 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2054 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2055 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2056 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2057 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2058 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2059 | // primary HW module |
| 2060 | audio_devices_t device = getNewInputDevice(inputDesc); |
| 2061 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2062 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2063 | if (inputDesc->activeCount() == 1) { |
| 2064 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2065 | if ((inputDesc->mPolicyMix != NULL) |
| 2066 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2067 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2068 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2069 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2070 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2071 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2072 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2073 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2074 | SoundTrigger::setCaptureState(true); |
| 2075 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2076 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2077 | // automatically enable the remote submix output when input is started if not |
| 2078 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2079 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2080 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2081 | String8 address = String8(""); |
| 2082 | if (inputDesc->mPolicyMix == NULL) { |
| 2083 | address = String8("0"); |
| 2084 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2085 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2086 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2087 | if (address != "") { |
| 2088 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2089 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2090 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2091 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2092 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2095 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2096 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2097 | return NO_ERROR; |
| 2098 | } |
| 2099 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2100 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2101 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2102 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2103 | |
| 2104 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2105 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2106 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2107 | return BAD_VALUE; |
| 2108 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2109 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2110 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2111 | if (!client->active()) { |
| 2112 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2113 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2114 | } |
| 2115 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2116 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2117 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2118 | inputDesc->stop(); |
| 2119 | if (inputDesc->isActive()) { |
| 2120 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2121 | } else { |
| 2122 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2123 | if ((inputDesc->mPolicyMix != NULL) |
| 2124 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2125 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2126 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2127 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2128 | |
| 2129 | // automatically disable the remote submix output when input is stopped if not |
| 2130 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2131 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2132 | String8 address = String8(""); |
| 2133 | if (inputDesc->mPolicyMix == NULL) { |
| 2134 | address = String8("0"); |
| 2135 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2136 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2137 | } |
| 2138 | if (address != "") { |
| 2139 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2140 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2141 | address, "remote-submix"); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | audio_devices_t device = inputDesc->mDevice; |
| 2146 | resetInputDevice(input); |
| 2147 | |
| 2148 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2149 | // primary HW module |
| 2150 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2151 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2152 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2153 | SoundTrigger::setCaptureState(false); |
| 2154 | } |
| 2155 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2156 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2157 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2160 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2161 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2162 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2163 | |
| 2164 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2165 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2166 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2167 | return; |
| 2168 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2169 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2170 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2171 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2172 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2173 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2174 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2175 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2176 | if (inputDesc->getClientCount() > 0) { |
| 2177 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2178 | return; |
| 2179 | } |
| 2180 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2181 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2182 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2183 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2186 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2187 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2188 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2189 | |
| 2190 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2191 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2192 | } |
| 2193 | } |
| 2194 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2195 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2196 | { |
| 2197 | stopInput(portId); |
| 2198 | releaseInput(portId); |
| 2199 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2200 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2201 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2202 | bool patchRemoved = false; |
| 2203 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2204 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2205 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2206 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2207 | if (patch_index >= 0) { |
| 2208 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2209 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2210 | mAudioPatches.removeItemsAt(patch_index); |
| 2211 | patchRemoved = true; |
| 2212 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2213 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2214 | } |
| 2215 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2216 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2217 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2218 | |
| 2219 | if (patchRemoved) { |
| 2220 | mpClientInterface->onAudioPatchListUpdate(); |
| 2221 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2224 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2225 | int indexMin, |
| 2226 | int indexMax) |
| 2227 | { |
| 2228 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2229 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2230 | |
| 2231 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2232 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2233 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2234 | continue; |
| 2235 | } |
| 2236 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2237 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2240 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2241 | int index, |
| 2242 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2243 | { |
| 2244 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2245 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2246 | // app that has MODIFY_PHONE_STATE permission. |
| 2247 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2248 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2249 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2250 | return BAD_VALUE; |
| 2251 | } |
| 2252 | if (!audio_is_output_device(device)) { |
| 2253 | return BAD_VALUE; |
| 2254 | } |
| 2255 | |
| 2256 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2257 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2258 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2259 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2260 | stream, device, index); |
| 2261 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2262 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2263 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2264 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2265 | continue; |
| 2266 | } |
| 2267 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2268 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2269 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2270 | // update volume on all outputs and streams matching the following: |
| 2271 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2272 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2273 | // the requested device |
| 2274 | // - For non default requested device, currently selected device on the output is either the |
| 2275 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2276 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2277 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2278 | status_t status = NO_ERROR; |
| 2279 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2280 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2281 | audio_devices_t curDevice = desc->device(); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2282 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2283 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2284 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2285 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2286 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2287 | continue; |
| 2288 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2289 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2290 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2291 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2292 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2293 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2294 | continue; |
| 2295 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2296 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2297 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2298 | curStreamDevice |= device; |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2299 | applyVolume = (Volume::getDeviceForVolume(curDevice) & curStreamDevice) != 0; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2300 | } else { |
| 2301 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2302 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2303 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2304 | // rescale index before applying to curStream as ranges may be different for |
| 2305 | // stream and curStream |
| 2306 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2307 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2308 | //FIXME: workaround for truncated touch sounds |
| 2309 | // delayed volume change for system stream to be removed when the problem is |
| 2310 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2311 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2312 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2313 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2314 | if (volStatus != NO_ERROR) { |
| 2315 | status = volStatus; |
| 2316 | } |
| 2317 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2318 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2319 | } |
| 2320 | return status; |
| 2321 | } |
| 2322 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2323 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2324 | int *index, |
| 2325 | audio_devices_t device) |
| 2326 | { |
| 2327 | if (index == NULL) { |
| 2328 | return BAD_VALUE; |
| 2329 | } |
| 2330 | if (!audio_is_output_device(device)) { |
| 2331 | return BAD_VALUE; |
| 2332 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2333 | // 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] | 2334 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2335 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2336 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2337 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2338 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2339 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2340 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2341 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2342 | return NO_ERROR; |
| 2343 | } |
| 2344 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2345 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2346 | { |
| 2347 | // select one output among several suitable for global effects. |
| 2348 | // The priority is as follows: |
| 2349 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2350 | // AudioFlinger will invalidate the track and the offloaded output |
| 2351 | // will be closed causing the effect to be moved to a PCM output. |
| 2352 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2353 | // 3: The primary output |
| 2354 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2355 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2356 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2357 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2358 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2359 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2360 | if (outputs.size() == 0) { |
| 2361 | return AUDIO_IO_HANDLE_NONE; |
| 2362 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2363 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2364 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2365 | bool activeOnly = true; |
| 2366 | |
| 2367 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2368 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2369 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2370 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2371 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2372 | for (audio_io_handle_t output : outputs) { |
| 2373 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2374 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2375 | continue; |
| 2376 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2377 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2378 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2379 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2380 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2381 | } |
| 2382 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2383 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2384 | } |
| 2385 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2386 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2387 | } |
| 2388 | } |
| 2389 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2390 | output = outputOffloaded; |
| 2391 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2392 | output = outputDeepBuffer; |
| 2393 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2394 | output = outputPrimary; |
| 2395 | } else { |
| 2396 | output = outputs[0]; |
| 2397 | } |
| 2398 | activeOnly = false; |
| 2399 | } |
| 2400 | |
| 2401 | if (output != mMusicEffectOutput) { |
| 2402 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2403 | mMusicEffectOutput = output; |
| 2404 | } |
| 2405 | |
| 2406 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2407 | return output; |
| 2408 | } |
| 2409 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2410 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2411 | { |
| 2412 | return selectOutputForMusicEffects(); |
| 2413 | } |
| 2414 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2415 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2416 | audio_io_handle_t io, |
| 2417 | uint32_t strategy, |
| 2418 | int session, |
| 2419 | int id) |
| 2420 | { |
| 2421 | ssize_t index = mOutputs.indexOfKey(io); |
| 2422 | if (index < 0) { |
| 2423 | index = mInputs.indexOfKey(io); |
| 2424 | if (index < 0) { |
| 2425 | ALOGW("registerEffect() unknown io %d", io); |
| 2426 | return INVALID_OPERATION; |
| 2427 | } |
| 2428 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2429 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2430 | } |
| 2431 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2432 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2433 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2434 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2435 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2436 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2437 | continue; |
| 2438 | } |
| 2439 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2440 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2441 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
| 2444 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2445 | { |
| 2446 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2447 | } |
| 2448 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2449 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2450 | { |
| 2451 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2452 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2453 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2454 | return true; |
| 2455 | } |
| 2456 | } |
| 2457 | return false; |
| 2458 | } |
| 2459 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2460 | // Register a list of custom mixes with their attributes and format. |
| 2461 | // When a mix is registered, corresponding input and output profiles are |
| 2462 | // added to the remote submix hw module. The profile contains only the |
| 2463 | // parameters (sampling rate, format...) specified by the mix. |
| 2464 | // The corresponding input remote submix device is also connected. |
| 2465 | // |
| 2466 | // When a remote submix device is connected, the address is checked to select the |
| 2467 | // appropriate profile and the corresponding input or output stream is opened. |
| 2468 | // |
| 2469 | // When capture starts, getInputForAttr() will: |
| 2470 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2471 | // - 2 if none found, getDeviceForInputSource() will: |
| 2472 | // - 2.1 look for a mix matching the attributes source |
| 2473 | // - 2.2 if none found, default to device selection by policy rules |
| 2474 | // At this time, the corresponding output remote submix device is also connected |
| 2475 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2476 | // after AudioTracks are invalidated |
| 2477 | // |
| 2478 | // When playback starts, getOutputForAttr() will: |
| 2479 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2480 | // - 2 if none found, look for a mix matching the attributes usage |
| 2481 | // - 3 if none found, default to device and output selection by policy rules. |
| 2482 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2483 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2484 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2485 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2486 | status_t res = NO_ERROR; |
| 2487 | |
| 2488 | sp<HwModule> rSubmixModule; |
| 2489 | // examine each mix's route type |
| 2490 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2491 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2492 | // 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] | 2493 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2494 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2495 | break; |
| 2496 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2497 | 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] | 2498 | 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] | 2499 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2500 | rSubmixModule = mHwModules.getModuleFromName( |
| 2501 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2502 | if (rSubmixModule == 0) { |
| 2503 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2504 | i); |
| 2505 | res = INVALID_OPERATION; |
| 2506 | break; |
| 2507 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2508 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2509 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2510 | String8 address = mix.mDeviceAddress; |
| 2511 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2512 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2513 | } else { |
| 2514 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2515 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2516 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2517 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2518 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2519 | res = INVALID_OPERATION; |
| 2520 | break; |
| 2521 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2522 | audio_config_t outputConfig = mix.mFormat; |
| 2523 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2524 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2525 | // stereo and let audio flinger do the channel conversion if needed. |
| 2526 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2527 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2528 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2529 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2530 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2531 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2532 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2533 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2534 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2535 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2536 | address.string(), "remote-submix"); |
| 2537 | } else { |
| 2538 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2539 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2540 | address.string(), "remote-submix"); |
| 2541 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2542 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2543 | String8 address = mix.mDeviceAddress; |
| 2544 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2545 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2546 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2547 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2548 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2549 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2550 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2551 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2552 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2553 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2554 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2555 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2556 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2557 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2558 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2559 | } else { |
| 2560 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2561 | } |
| 2562 | break; |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | if (res != NO_ERROR) { |
| 2567 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2568 | i, device, address.string()); |
| 2569 | res = INVALID_OPERATION; |
| 2570 | break; |
| 2571 | } else if (!foundOutput) { |
| 2572 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2573 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2574 | res = INVALID_OPERATION; |
| 2575 | break; |
| 2576 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2577 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2578 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2579 | if (res != NO_ERROR) { |
| 2580 | unregisterPolicyMixes(mixes); |
| 2581 | } |
| 2582 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2583 | } |
| 2584 | |
| 2585 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2586 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2587 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2588 | status_t res = NO_ERROR; |
| 2589 | sp<HwModule> rSubmixModule; |
| 2590 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2591 | for (const auto& mix : mixes) { |
| 2592 | 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] | 2593 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2594 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2595 | rSubmixModule = mHwModules.getModuleFromName( |
| 2596 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2597 | if (rSubmixModule == 0) { |
| 2598 | res = INVALID_OPERATION; |
| 2599 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2600 | } |
| 2601 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2602 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2603 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2604 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2605 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2606 | res = INVALID_OPERATION; |
| 2607 | continue; |
| 2608 | } |
| 2609 | |
| 2610 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2611 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2612 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2613 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2614 | address.string(), "remote-submix"); |
| 2615 | } |
| 2616 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2617 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2618 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2619 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2620 | address.string(), "remote-submix"); |
| 2621 | } |
| 2622 | rSubmixModule->removeOutputProfile(address); |
| 2623 | rSubmixModule->removeInputProfile(address); |
| 2624 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2625 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2626 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2627 | res = INVALID_OPERATION; |
| 2628 | continue; |
| 2629 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2630 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2631 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2632 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2633 | } |
| 2634 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 2635 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 2636 | { |
| 2637 | size_t i = 0; |
| 2638 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 2639 | for (const auto& fmt : mManualSurroundFormats) { |
| 2640 | if (i++ != 0) dst->append(", "); |
| 2641 | std::string sfmt; |
| 2642 | FormatConverter::toString(fmt, sfmt); |
| 2643 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 2644 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 2645 | } |
| 2646 | } |
| 2647 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2648 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2649 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2650 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2651 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2652 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2653 | std::string stateLiteral; |
| 2654 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2655 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2656 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2657 | "communications", "media", "record", "dock", "system", |
| 2658 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2659 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2660 | 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] | 2661 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 2662 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 2663 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 2664 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 2665 | dst->append(" (MANUAL: "); |
| 2666 | dumpManualSurroundFormats(dst); |
| 2667 | dst->append(")"); |
| 2668 | } |
| 2669 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2670 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2671 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2672 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2673 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2674 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2675 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2676 | mHwModulesAll.dump(dst); |
| 2677 | mOutputs.dump(dst); |
| 2678 | mInputs.dump(dst); |
| 2679 | mVolumeCurves->dump(dst); |
| 2680 | mEffects.dump(dst); |
| 2681 | mAudioPatches.dump(dst); |
| 2682 | mPolicyMixes.dump(dst); |
| 2683 | mAudioSources.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | status_t AudioPolicyManager::dump(int fd) |
| 2687 | { |
| 2688 | String8 result; |
| 2689 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2690 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2691 | return NO_ERROR; |
| 2692 | } |
| 2693 | |
| 2694 | // This function checks for the parameters which can be offloaded. |
| 2695 | // This can be enhanced depending on the capability of the DSP and policy |
| 2696 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2697 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2698 | { |
| 2699 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2700 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2701 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2702 | offloadInfo.format, |
| 2703 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2704 | offloadInfo.has_video); |
| 2705 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2706 | if (mMasterMono) { |
| 2707 | return false; // no offloading if mono is set. |
| 2708 | } |
| 2709 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2710 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2711 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2712 | ALOGV("offload disabled by audio.offload.disable"); |
| 2713 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2714 | } |
| 2715 | |
| 2716 | // Check if stream type is music, then only allow offload as of now. |
| 2717 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2718 | { |
| 2719 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2720 | return false; |
| 2721 | } |
| 2722 | |
| 2723 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2724 | const bool allowOffloadWithVideo = |
| 2725 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2726 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2727 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2728 | return false; |
| 2729 | } |
| 2730 | |
| 2731 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2732 | const int min_duration_secs = property_get_int32( |
| 2733 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2734 | if (min_duration_secs >= 0) { |
| 2735 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2736 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2737 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2738 | return false; |
| 2739 | } |
| 2740 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2741 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2742 | return false; |
| 2743 | } |
| 2744 | |
| 2745 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2746 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2747 | // detects there is an active non offloadable effect. |
| 2748 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2749 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2750 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2751 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2752 | return false; |
| 2753 | } |
| 2754 | |
| 2755 | // See if there is a profile to support this. |
| 2756 | // AUDIO_DEVICE_NONE |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2757 | sp<IOProfile> profile = getProfileForOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2758 | offloadInfo.sample_rate, |
| 2759 | offloadInfo.format, |
| 2760 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2761 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 2762 | true /* directOnly */); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2763 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2764 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2767 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 2768 | const audio_attributes_t& attributes) { |
| 2769 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
| 2770 | audio_attributes_flags_to_audio_output_flags(attributes.flags, output_flags); |
| 2771 | sp<IOProfile> profile = getProfileForOutput(AUDIO_DEVICE_NONE /*ignore device */, |
| 2772 | config.sample_rate, |
| 2773 | config.format, |
| 2774 | config.channel_mask, |
| 2775 | output_flags, |
| 2776 | true /* directOnly */); |
| 2777 | ALOGV("%s() profile %sfound with name: %s, " |
| 2778 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 2779 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 2780 | (profile != 0 ? profile->getTagName().string() : "null"), |
| 2781 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 2782 | return (profile != 0); |
| 2783 | } |
| 2784 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2785 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2786 | audio_port_type_t type, |
| 2787 | unsigned int *num_ports, |
| 2788 | struct audio_port *ports, |
| 2789 | unsigned int *generation) |
| 2790 | { |
| 2791 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2792 | generation == NULL) { |
| 2793 | return BAD_VALUE; |
| 2794 | } |
| 2795 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2796 | if (ports == NULL) { |
| 2797 | *num_ports = 0; |
| 2798 | } |
| 2799 | |
| 2800 | size_t portsWritten = 0; |
| 2801 | size_t portsMax = *num_ports; |
| 2802 | *num_ports = 0; |
| 2803 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2804 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2805 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2806 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2807 | for (const auto& dev : mAvailableOutputDevices) { |
| 2808 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2809 | continue; |
| 2810 | } |
| 2811 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2812 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2813 | } |
| 2814 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2815 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2816 | } |
| 2817 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2818 | for (const auto& dev : mAvailableInputDevices) { |
| 2819 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2820 | continue; |
| 2821 | } |
| 2822 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2823 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2824 | } |
| 2825 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2826 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2830 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2831 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2832 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2833 | } |
| 2834 | *num_ports += mInputs.size(); |
| 2835 | } |
| 2836 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2837 | size_t numOutputs = 0; |
| 2838 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2839 | if (!mOutputs[i]->isDuplicated()) { |
| 2840 | numOutputs++; |
| 2841 | if (portsWritten < portsMax) { |
| 2842 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2843 | } |
| 2844 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2845 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2846 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2847 | } |
| 2848 | } |
| 2849 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2850 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2851 | return NO_ERROR; |
| 2852 | } |
| 2853 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2854 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2855 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2856 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2857 | return BAD_VALUE; |
| 2858 | } |
| 2859 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2860 | if (dev != 0) { |
| 2861 | dev->toAudioPort(port); |
| 2862 | return NO_ERROR; |
| 2863 | } |
| 2864 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2865 | if (dev != 0) { |
| 2866 | dev->toAudioPort(port); |
| 2867 | return NO_ERROR; |
| 2868 | } |
| 2869 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2870 | if (out != 0) { |
| 2871 | out->toAudioPort(port); |
| 2872 | return NO_ERROR; |
| 2873 | } |
| 2874 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2875 | if (in != 0) { |
| 2876 | in->toAudioPort(port); |
| 2877 | return NO_ERROR; |
| 2878 | } |
| 2879 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2880 | } |
| 2881 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2882 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2883 | audio_patch_handle_t *handle, |
| 2884 | uid_t uid) |
| 2885 | { |
| 2886 | ALOGV("createAudioPatch()"); |
| 2887 | |
| 2888 | if (handle == NULL || patch == NULL) { |
| 2889 | return BAD_VALUE; |
| 2890 | } |
| 2891 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2892 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2893 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2894 | return BAD_VALUE; |
| 2895 | } |
| 2896 | // only one source per audio patch supported for now |
| 2897 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2898 | return INVALID_OPERATION; |
| 2899 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2900 | |
| 2901 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2902 | return INVALID_OPERATION; |
| 2903 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2904 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2905 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2906 | return INVALID_OPERATION; |
| 2907 | } |
| 2908 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2909 | |
| 2910 | sp<AudioPatch> patchDesc; |
| 2911 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2912 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2913 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2914 | patch->sources[0].role, |
| 2915 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2916 | #if LOG_NDEBUG == 0 |
| 2917 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2918 | 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] | 2919 | patch->sinks[i].role, |
| 2920 | patch->sinks[i].type); |
| 2921 | } |
| 2922 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2923 | |
| 2924 | if (index >= 0) { |
| 2925 | patchDesc = mAudioPatches.valueAt(index); |
| 2926 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2927 | mUidCached, patchDesc->mUid, uid); |
| 2928 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2929 | return INVALID_OPERATION; |
| 2930 | } |
| 2931 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2932 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2933 | } |
| 2934 | |
| 2935 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2936 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2937 | if (outputDesc == NULL) { |
| 2938 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2939 | return BAD_VALUE; |
| 2940 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2941 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2942 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2943 | if (patchDesc != 0) { |
| 2944 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2945 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2946 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2947 | return BAD_VALUE; |
| 2948 | } |
| 2949 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2950 | DeviceVector devices; |
| 2951 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2952 | // Only support mix to devices connection |
| 2953 | // TODO add support for mix to mix connection |
| 2954 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2955 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2956 | return INVALID_OPERATION; |
| 2957 | } |
| 2958 | sp<DeviceDescriptor> devDesc = |
| 2959 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2960 | if (devDesc == 0) { |
| 2961 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2962 | return BAD_VALUE; |
| 2963 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2964 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2965 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2966 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2967 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2968 | NULL, // updatedSamplingRate |
| 2969 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2970 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2971 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2972 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2973 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2974 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2975 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2976 | return INVALID_OPERATION; |
| 2977 | } |
| 2978 | devices.add(devDesc); |
| 2979 | } |
| 2980 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2981 | return INVALID_OPERATION; |
| 2982 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2983 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2984 | // TODO: reconfigure output format and channels here |
| 2985 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2986 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2987 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2988 | index = mAudioPatches.indexOfKey(*handle); |
| 2989 | if (index >= 0) { |
| 2990 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2991 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2992 | } |
| 2993 | patchDesc = mAudioPatches.valueAt(index); |
| 2994 | patchDesc->mUid = uid; |
| 2995 | ALOGV("createAudioPatch() success"); |
| 2996 | } else { |
| 2997 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2998 | return INVALID_OPERATION; |
| 2999 | } |
| 3000 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3001 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3002 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3003 | // only one sink supported when connecting an input device to a mix |
| 3004 | if (patch->num_sinks > 1) { |
| 3005 | return INVALID_OPERATION; |
| 3006 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3007 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3008 | if (inputDesc == NULL) { |
| 3009 | return BAD_VALUE; |
| 3010 | } |
| 3011 | if (patchDesc != 0) { |
| 3012 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3013 | return BAD_VALUE; |
| 3014 | } |
| 3015 | } |
| 3016 | sp<DeviceDescriptor> devDesc = |
| 3017 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3018 | if (devDesc == 0) { |
| 3019 | return BAD_VALUE; |
| 3020 | } |
| 3021 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3022 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3023 | devDesc->mAddress, |
| 3024 | patch->sinks[0].sample_rate, |
| 3025 | NULL, /*updatedSampleRate*/ |
| 3026 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3027 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3028 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3029 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3030 | // FIXME for the parameter type, |
| 3031 | // and the NONE |
| 3032 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3033 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3034 | return INVALID_OPERATION; |
| 3035 | } |
| 3036 | // TODO: reconfigure output format and channels here |
| 3037 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3038 | devDesc->type(), inputDesc->mIoHandle); |
| 3039 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3040 | index = mAudioPatches.indexOfKey(*handle); |
| 3041 | if (index >= 0) { |
| 3042 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3043 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3044 | } |
| 3045 | patchDesc = mAudioPatches.valueAt(index); |
| 3046 | patchDesc->mUid = uid; |
| 3047 | ALOGV("createAudioPatch() success"); |
| 3048 | } else { |
| 3049 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3050 | return INVALID_OPERATION; |
| 3051 | } |
| 3052 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3053 | // device to device connection |
| 3054 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3055 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3056 | return BAD_VALUE; |
| 3057 | } |
| 3058 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3059 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3060 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3061 | if (srcDeviceDesc == 0) { |
| 3062 | return BAD_VALUE; |
| 3063 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3064 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3065 | //update source and sink with our own data as the data passed in the patch may |
| 3066 | // be incomplete. |
| 3067 | struct audio_patch newPatch = *patch; |
| 3068 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3069 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3070 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3071 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3072 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3073 | return INVALID_OPERATION; |
| 3074 | } |
| 3075 | |
| 3076 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3077 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3078 | if (sinkDeviceDesc == 0) { |
| 3079 | return BAD_VALUE; |
| 3080 | } |
| 3081 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3082 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3083 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3084 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3085 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3086 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3087 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3088 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3089 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3090 | return INVALID_OPERATION; |
| 3091 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3092 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3093 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3094 | // if the sink device is reachable via an opened output stream, request to go via |
| 3095 | // this output stream by adding a second source to the patch description |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 3096 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3097 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3098 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3099 | if (outputDesc->isDuplicated()) { |
| 3100 | return INVALID_OPERATION; |
| 3101 | } |
| 3102 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3103 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3104 | newPatch.num_sources = 2; |
| 3105 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3106 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3107 | } |
| 3108 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3109 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3110 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3111 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3112 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3113 | status); |
| 3114 | return INVALID_OPERATION; |
| 3115 | } |
| 3116 | } else { |
| 3117 | return BAD_VALUE; |
| 3118 | } |
| 3119 | } else { |
| 3120 | return BAD_VALUE; |
| 3121 | } |
| 3122 | return NO_ERROR; |
| 3123 | } |
| 3124 | |
| 3125 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3126 | uid_t uid) |
| 3127 | { |
| 3128 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3129 | |
| 3130 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3131 | |
| 3132 | if (index < 0) { |
| 3133 | return BAD_VALUE; |
| 3134 | } |
| 3135 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3136 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3137 | mUidCached, patchDesc->mUid, uid); |
| 3138 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3139 | return INVALID_OPERATION; |
| 3140 | } |
| 3141 | |
| 3142 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3143 | patchDesc->mUid = mUidCached; |
| 3144 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3145 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3146 | if (outputDesc == NULL) { |
| 3147 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3148 | return BAD_VALUE; |
| 3149 | } |
| 3150 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3151 | setOutputDevice(outputDesc, |
| 3152 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3153 | true, |
| 3154 | 0, |
| 3155 | NULL); |
| 3156 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3157 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3158 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3159 | if (inputDesc == NULL) { |
| 3160 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3161 | return BAD_VALUE; |
| 3162 | } |
| 3163 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3164 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3165 | true, |
| 3166 | NULL); |
| 3167 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3168 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3169 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3170 | status, patchDesc->mAfPatchHandle); |
| 3171 | removeAudioPatch(patchDesc->mHandle); |
| 3172 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3173 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3174 | } else { |
| 3175 | return BAD_VALUE; |
| 3176 | } |
| 3177 | } else { |
| 3178 | return BAD_VALUE; |
| 3179 | } |
| 3180 | return NO_ERROR; |
| 3181 | } |
| 3182 | |
| 3183 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3184 | struct audio_patch *patches, |
| 3185 | unsigned int *generation) |
| 3186 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3187 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3188 | return BAD_VALUE; |
| 3189 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3190 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3191 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3192 | } |
| 3193 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3194 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3195 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3196 | ALOGV("setAudioPortConfig()"); |
| 3197 | |
| 3198 | if (config == NULL) { |
| 3199 | return BAD_VALUE; |
| 3200 | } |
| 3201 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3202 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3203 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3204 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3205 | } |
| 3206 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3207 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3208 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3209 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3210 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3211 | if (outputDesc == NULL) { |
| 3212 | return BAD_VALUE; |
| 3213 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3214 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3215 | "setAudioPortConfig() called on duplicated output %d", |
| 3216 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3217 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3218 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3219 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3220 | if (inputDesc == NULL) { |
| 3221 | return BAD_VALUE; |
| 3222 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3223 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3224 | } else { |
| 3225 | return BAD_VALUE; |
| 3226 | } |
| 3227 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3228 | sp<DeviceDescriptor> deviceDesc; |
| 3229 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3230 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3231 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3232 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3233 | } else { |
| 3234 | return BAD_VALUE; |
| 3235 | } |
| 3236 | if (deviceDesc == NULL) { |
| 3237 | return BAD_VALUE; |
| 3238 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3239 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3240 | } else { |
| 3241 | return BAD_VALUE; |
| 3242 | } |
| 3243 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3244 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3245 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3246 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3247 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3248 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3249 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3250 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3251 | if (status != NO_ERROR) { |
| 3252 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3253 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3254 | |
| 3255 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3256 | } |
| 3257 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3258 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3259 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3260 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3261 | clearAudioPatches(uid); |
| 3262 | clearSessionRoutes(uid); |
| 3263 | } |
| 3264 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3265 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3266 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3267 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3268 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3269 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3270 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3271 | } |
| 3272 | } |
| 3273 | } |
| 3274 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3275 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3276 | audio_io_handle_t ouptutToSkip) |
| 3277 | { |
| 3278 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3279 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3280 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3281 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3282 | continue; |
| 3283 | } |
| 3284 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3285 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3286 | continue; |
| 3287 | } |
| 3288 | // If the default device for this strategy is on another output mix, |
| 3289 | // invalidate all tracks in this strategy to force re connection. |
| 3290 | // Otherwise select new device on the output mix. |
| 3291 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3292 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3293 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3294 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3295 | } |
| 3296 | } |
| 3297 | } else { |
| 3298 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3299 | setOutputDevice(outputDesc, newDevice, false); |
| 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | |
| 3304 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3305 | { |
| 3306 | // remove output routes associated with this uid |
| 3307 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3308 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3309 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3310 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3311 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3312 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3313 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3314 | } |
| 3315 | } |
| 3316 | } |
| 3317 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3318 | for (const auto& strategy : affectedStrategies) { |
| 3319 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | // remove input routes associated with this uid |
| 3323 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3324 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3325 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3326 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3327 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3328 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3329 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3330 | } |
| 3331 | } |
| 3332 | } |
| 3333 | // reroute inputs if necessary |
| 3334 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3335 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3336 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3337 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3338 | inputsToClose.add(inputDesc->mIoHandle); |
| 3339 | } |
| 3340 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3341 | for (const auto& input : inputsToClose) { |
| 3342 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3343 | } |
| 3344 | } |
| 3345 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3346 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3347 | { |
| 3348 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3349 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3350 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3351 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3352 | } |
| 3353 | } |
| 3354 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3355 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3356 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3357 | audio_io_handle_t *ioHandle, |
| 3358 | audio_devices_t *device) |
| 3359 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3360 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3361 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3362 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3363 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3364 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3365 | } |
| 3366 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3367 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3368 | const audio_attributes_t *attributes, |
| 3369 | audio_port_handle_t *portId, |
| 3370 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3371 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3372 | ALOGV("%s", __FUNCTION__); |
| 3373 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3374 | |
| 3375 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3376 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3377 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3378 | return BAD_VALUE; |
| 3379 | } |
| 3380 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3381 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3382 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3383 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3384 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3385 | return INVALID_OPERATION; |
| 3386 | } |
| 3387 | |
| 3388 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3389 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3390 | String8(source->ext.device.address)); |
| 3391 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3392 | 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] | 3393 | return BAD_VALUE; |
| 3394 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3395 | |
| 3396 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3397 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3398 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3399 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3400 | |
| 3401 | sp<SourceClientDescriptor> sourceDesc = |
| 3402 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3403 | streamTypefromAttributesInt(attributes), |
| 3404 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3405 | |
| 3406 | status_t status = connectAudioSource(sourceDesc); |
| 3407 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3408 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3409 | } |
| 3410 | return status; |
| 3411 | } |
| 3412 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3413 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3414 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3415 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3416 | |
| 3417 | // make sure we only have one patch per source. |
| 3418 | disconnectAudioSource(sourceDesc); |
| 3419 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3420 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3421 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3422 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3423 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3424 | |
| 3425 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3426 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3427 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3428 | |
| 3429 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3430 | |
| 3431 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3432 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3433 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3434 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3435 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 3436 | // TODO: may explicitly specify whether we should use HW or SW patch |
| 3437 | // create patch between src device and output device |
| 3438 | // create Hwoutput and add to mHwOutputs |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3439 | } else { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 3440 | audio_attributes_t resultAttr; |
| 3441 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3442 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3443 | config.sample_rate = sourceDesc->config().sample_rate; |
| 3444 | config.channel_mask = sourceDesc->config().channel_mask; |
| 3445 | config.format = sourceDesc->config().format; |
| 3446 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 3447 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3448 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, |
| 3449 | &attributes, &stream, sourceDesc->uid(), &config, &flags, &selectedDeviceId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3450 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3451 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3452 | return INVALID_OPERATION; |
| 3453 | } |
| 3454 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3455 | if (outputDesc->isDuplicated()) { |
| 3456 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3457 | return INVALID_OPERATION; |
| 3458 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3459 | status_t status = outputDesc->start(); |
| 3460 | if (status != NO_ERROR) { |
| 3461 | return status; |
| 3462 | } |
| 3463 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3464 | // create a special patch with no sink and two sources: |
| 3465 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3466 | // be connected as well as the stream type for volume control |
| 3467 | // - the sink is defined by whatever output device is currently selected for the output |
| 3468 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3469 | PatchBuilder patchBuilder; |
| 3470 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3471 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3472 | &afPatchHandle, |
| 3473 | 0); |
| 3474 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3475 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3476 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3477 | if (status != NO_ERROR) { |
| 3478 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3479 | __FUNCTION__, status); |
| 3480 | return INVALID_OPERATION; |
| 3481 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame^] | 3482 | |
| 3483 | if (outputDesc->getClient(sourceDesc->portId()) != nullptr) { |
| 3484 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 3485 | return INVALID_OPERATION; |
| 3486 | } |
| 3487 | outputDesc->addClient(sourceDesc); |
| 3488 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3489 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3490 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3491 | |
| 3492 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3493 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3494 | return status; |
| 3495 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3496 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3497 | if (delayMs != 0) { |
| 3498 | usleep(delayMs * 1000); |
| 3499 | } |
| 3500 | } |
| 3501 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3502 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3503 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3504 | |
| 3505 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3508 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3509 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3510 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3511 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3512 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3513 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3514 | return BAD_VALUE; |
| 3515 | } |
| 3516 | status_t status = disconnectAudioSource(sourceDesc); |
| 3517 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3518 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3519 | return status; |
| 3520 | } |
| 3521 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3522 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3523 | { |
| 3524 | if (mMasterMono == mono) { |
| 3525 | return NO_ERROR; |
| 3526 | } |
| 3527 | mMasterMono = mono; |
| 3528 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3529 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3530 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3531 | // |
| 3532 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3533 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3534 | // play back offloaded. |
| 3535 | if (mMasterMono) { |
| 3536 | Vector<audio_io_handle_t> offloaded; |
| 3537 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3538 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3539 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3540 | offloaded.push(desc->mIoHandle); |
| 3541 | } |
| 3542 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3543 | for (const auto& handle : offloaded) { |
| 3544 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3545 | } |
| 3546 | } |
| 3547 | // update master mono for all remaining outputs |
| 3548 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3549 | updateMono(mOutputs.keyAt(i)); |
| 3550 | } |
| 3551 | return NO_ERROR; |
| 3552 | } |
| 3553 | |
| 3554 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3555 | { |
| 3556 | *mono = mMasterMono; |
| 3557 | return NO_ERROR; |
| 3558 | } |
| 3559 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3560 | float AudioPolicyManager::getStreamVolumeDB( |
| 3561 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3562 | { |
| 3563 | return computeVolume(stream, index, device); |
| 3564 | } |
| 3565 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3566 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3567 | audio_format_t *surroundFormats, |
| 3568 | bool *surroundFormatsEnabled, |
| 3569 | bool reported) |
| 3570 | { |
| 3571 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3572 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3573 | return BAD_VALUE; |
| 3574 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3575 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p reported %d", |
| 3576 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3577 | |
| 3578 | size_t formatsWritten = 0; |
| 3579 | size_t formatsMax = *numSurroundFormats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3580 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3581 | if (reported) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3582 | // Return formats from all device profiles that have already been resolved by |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3583 | // checkOutputsForDevice(). |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3584 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 3585 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 3586 | FormatVector supportedFormats = |
| 3587 | device->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3588 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
| 3589 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
| 3590 | formats.insert(supportedFormats[j]); |
| 3591 | } else { |
| 3592 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3593 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3594 | formats.insert(pair.first); |
| 3595 | break; |
| 3596 | } |
| 3597 | } |
| 3598 | } |
| 3599 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3602 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3603 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3604 | } |
| 3605 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3606 | *numSurroundFormats = formats.size(); |
| 3607 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3608 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3609 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3610 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3611 | surroundFormats[formatsWritten] = format; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3612 | bool formatEnabled = true; |
| 3613 | switch (forceUse) { |
| 3614 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
| 3615 | formatEnabled = mManualSurroundFormats.count(format) != 0; |
| 3616 | break; |
| 3617 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 3618 | formatEnabled = false; |
| 3619 | break; |
| 3620 | default: // AUTO or ALWAYS => true |
| 3621 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3622 | } |
| 3623 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3624 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3625 | } |
| 3626 | return NO_ERROR; |
| 3627 | } |
| 3628 | |
| 3629 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3630 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3631 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3632 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3633 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3634 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3635 | return BAD_VALUE; |
| 3636 | } |
| 3637 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3638 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 3639 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3640 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3641 | return INVALID_OPERATION; |
| 3642 | } |
| 3643 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3644 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3645 | return NO_ERROR; |
| 3646 | } |
| 3647 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3648 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3649 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3650 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3651 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3652 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3653 | } |
| 3654 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3655 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3656 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3657 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3662 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3663 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3664 | AUDIO_DEVICE_OUT_HDMI); |
| 3665 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3666 | // Simulate reconnection to update enabled surround sound formats. |
| 3667 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3668 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3669 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3670 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3671 | address.c_str(), |
| 3672 | name.c_str()); |
| 3673 | if (status != NO_ERROR) { |
| 3674 | continue; |
| 3675 | } |
| 3676 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3677 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3678 | address.c_str(), |
| 3679 | name.c_str()); |
| 3680 | profileUpdated |= (status == NO_ERROR); |
| 3681 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3682 | // 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] | 3683 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3684 | AUDIO_DEVICE_IN_HDMI); |
| 3685 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3686 | // Simulate reconnection to update enabled surround sound formats. |
| 3687 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3688 | String8 name = hdmiInputDevices[i]->getName(); |
| 3689 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3690 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3691 | address.c_str(), |
| 3692 | name.c_str()); |
| 3693 | if (status != NO_ERROR) { |
| 3694 | continue; |
| 3695 | } |
| 3696 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3697 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3698 | address.c_str(), |
| 3699 | name.c_str()); |
| 3700 | profileUpdated |= (status == NO_ERROR); |
| 3701 | } |
| 3702 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3703 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3704 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3705 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3709 | } |
| 3710 | |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3711 | void AudioPolicyManager::setAppState(uid_t uid, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3712 | { |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3713 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3714 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3715 | ALOGV("%s(uid:%d, state:%d)", __func__, uid, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3716 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3717 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3718 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3719 | RecordClientVector clients = activeDesc->clientsList(true /*activeOnly*/); |
| 3720 | for (const auto& client : clients) { |
| 3721 | if (uid == client->uid()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3722 | client->setAppState(state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3723 | } |
| 3724 | } |
| 3725 | } |
| 3726 | } |
| 3727 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 3728 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 3729 | { |
| 3730 | for (const auto& hwModule : mHwModules) { |
| 3731 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 3732 | for (const auto &outProfile : outputProfiles) { |
| 3733 | struct audio_port audioPort; |
| 3734 | outProfile->toAudioPort(&audioPort); |
| 3735 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 3736 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 3737 | return true; |
| 3738 | } |
| 3739 | } |
| 3740 | } |
| 3741 | } |
| 3742 | return false; |
| 3743 | } |
| 3744 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3745 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3746 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3747 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3748 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3749 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3750 | if (patchDesc == 0) { |
| 3751 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3752 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3753 | return BAD_VALUE; |
| 3754 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3755 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3756 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3757 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3758 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3759 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3760 | if (status == NO_ERROR) { |
| 3761 | swOutputDesc->stop(); |
| 3762 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3763 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3764 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3765 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3766 | if (hwOutputDesc != 0) { |
| 3767 | // release patch between src device and output device |
| 3768 | // close Hwoutput and remove from mHwOutputs |
| 3769 | } else { |
| 3770 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3771 | } |
| 3772 | } |
| 3773 | return NO_ERROR; |
| 3774 | } |
| 3775 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3776 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3777 | audio_io_handle_t output, routing_strategy strategy) |
| 3778 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3779 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3780 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3781 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3782 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3783 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3784 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3785 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3786 | source = sourceDesc; |
| 3787 | break; |
| 3788 | } |
| 3789 | } |
| 3790 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3791 | } |
| 3792 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3793 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3794 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3795 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3796 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3797 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3798 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3801 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3802 | static const char *kConfigLocationList[] = |
| 3803 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3804 | static const int kConfigLocationListSize = |
| 3805 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3806 | |
| 3807 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3808 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3809 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3810 | status_t ret; |
| 3811 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3812 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3813 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3814 | // A2DP offload supported but disabled: try to use special XML file |
| 3815 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3816 | } |
| 3817 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3818 | |
| 3819 | for (const char* fileName : fileNames) { |
| 3820 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3821 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3822 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3823 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3824 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3825 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3826 | return ret; |
| 3827 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | return ret; |
| 3831 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3832 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3833 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3834 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3835 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3836 | 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] | 3837 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3838 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3839 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3840 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3841 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3842 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3843 | mAudioPortGeneration(1), |
| 3844 | mBeaconMuteRefCount(0), |
| 3845 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3846 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3847 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3848 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3849 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3850 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3851 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3852 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3853 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3854 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3855 | { |
| 3856 | loadConfig(); |
| 3857 | initialize(); |
| 3858 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3859 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3860 | // This check is to catch any legacy platform updating to Q without having |
| 3861 | // switched to XML since its deprecation on O. |
| 3862 | // TODO: after Q release, remove this check and flag as XML is now the only |
| 3863 | // option and all legacy platform should have transitioned to XML. |
| 3864 | #ifndef USE_XML_AUDIO_POLICY_CONF |
| 3865 | #error Audio policy no longer supports legacy .conf configuration format |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3866 | #endif |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3867 | |
| 3868 | void AudioPolicyManager::loadConfig() { |
| 3869 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3870 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3871 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3872 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3873 | } |
| 3874 | |
| 3875 | status_t AudioPolicyManager::initialize() { |
| 3876 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3877 | |
| 3878 | // 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] | 3879 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3880 | if (!engineInstance) { |
| 3881 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3882 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3883 | } |
| 3884 | // Retrieve the Policy Manager Interface |
| 3885 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3886 | if (mEngine == NULL) { |
| 3887 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3888 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3889 | } |
| 3890 | mEngine->setObserver(this); |
| 3891 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3892 | if (status != NO_ERROR) { |
| 3893 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3894 | return status; |
| 3895 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3896 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3897 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3898 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3899 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3900 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3901 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3902 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3903 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3904 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3905 | continue; |
| 3906 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3907 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3908 | // open all output streams needed to access attached devices |
| 3909 | // except for direct output streams that are only opened when they are actually |
| 3910 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3911 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3912 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3913 | if (!outProfile->canOpenNewIo()) { |
| 3914 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3915 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3916 | continue; |
| 3917 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3918 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3919 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3920 | continue; |
| 3921 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3922 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3923 | mTtsOutputAvailable = true; |
| 3924 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3925 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3926 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3927 | continue; |
| 3928 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3929 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3930 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3931 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3932 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3933 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3934 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3935 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3936 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3937 | if ((profileType & outputDeviceTypes) == 0) { |
| 3938 | continue; |
| 3939 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3940 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3941 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3942 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3943 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 3944 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3945 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3946 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3947 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3948 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3949 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3950 | |
| 3951 | if (status != NO_ERROR) { |
| 3952 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3953 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3954 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3955 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3956 | for (const auto& dev : supportedDevices) { |
| 3957 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3958 | // 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] | 3959 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3960 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3961 | } |
| 3962 | } |
| 3963 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3964 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3965 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3966 | } |
| 3967 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3968 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3969 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3970 | true, |
| 3971 | 0, |
| 3972 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3973 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3974 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3975 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3976 | // open input streams needed to access attached devices to validate |
| 3977 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3978 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3979 | if (!inProfile->canOpenNewIo()) { |
| 3980 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 3981 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 3982 | continue; |
| 3983 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3984 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3985 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3986 | continue; |
| 3987 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3988 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3989 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3990 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3991 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3992 | if ((profileType & inputDeviceTypes) == 0) { |
| 3993 | continue; |
| 3994 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3995 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3996 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3997 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3998 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 3999 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4000 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4001 | : String8(""); |
| 4002 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4003 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4004 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4005 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4006 | status_t status = inputDesc->open(nullptr, |
| 4007 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4008 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4009 | AUDIO_SOURCE_MIC, |
| 4010 | AUDIO_INPUT_FLAG_NONE, |
| 4011 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4012 | |
| 4013 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4014 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4015 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4016 | // 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] | 4017 | if (index >= 0) { |
| 4018 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4019 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4020 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4021 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4022 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4023 | } |
| 4024 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4025 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4026 | } else { |
| 4027 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4028 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4029 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4030 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4031 | } |
| 4032 | } |
| 4033 | // make sure all attached devices have been allocated a unique ID |
| 4034 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4035 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4036 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4037 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4038 | continue; |
| 4039 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4040 | // The device is now validated and can be appended to the available devices of the engine |
| 4041 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4042 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4043 | i++; |
| 4044 | } |
| 4045 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4046 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4047 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4048 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4049 | continue; |
| 4050 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4051 | // The device is now validated and can be appended to the available devices of the engine |
| 4052 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4053 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4054 | i++; |
| 4055 | } |
| 4056 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4057 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4058 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4059 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4060 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4061 | // If microphones address is empty, set it according to device type |
| 4062 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4063 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4064 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4065 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4066 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4067 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4068 | } |
| 4069 | } |
| 4070 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4071 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4072 | if (mPrimaryOutput == 0) { |
| 4073 | ALOGE("Failed to open primary output"); |
| 4074 | status = NO_INIT; |
| 4075 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4076 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4077 | // Silence ALOGV statements |
| 4078 | property_set("log.tag." LOG_TAG, "D"); |
| 4079 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4080 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4081 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4082 | } |
| 4083 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4084 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4085 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4087 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4088 | } |
| 4089 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4090 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4091 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4092 | mAvailableOutputDevices.clear(); |
| 4093 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4094 | mOutputs.clear(); |
| 4095 | mInputs.clear(); |
| 4096 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4097 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4098 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4099 | } |
| 4100 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4101 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4102 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4103 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4104 | } |
| 4105 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4106 | // --- |
| 4107 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4108 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4109 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4110 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4111 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4112 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4113 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4114 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4115 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4116 | } |
| 4117 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4118 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4119 | { |
| 4120 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4121 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4122 | } |
| 4123 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4124 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4125 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4126 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4127 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4128 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4129 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4130 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4131 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4132 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4133 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4134 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4135 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4136 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4137 | if (devDesc != 0) { |
| 4138 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4139 | desc->mIoHandle, address.string()); |
| 4140 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4141 | } |
| 4142 | } |
| 4143 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4144 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4145 | audio_policy_dev_state_t state, |
| 4146 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4147 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4148 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4149 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4150 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4151 | |
| 4152 | if (audio_device_is_digital(device)) { |
| 4153 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4154 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4155 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4157 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4158 | // first list already open outputs that can be routed to this device |
| 4159 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4160 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4161 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4162 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4163 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4164 | outputs.add(mOutputs.keyAt(i)); |
| 4165 | } else { |
| 4166 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4167 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4168 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4169 | } |
| 4170 | } |
| 4171 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4172 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4173 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4174 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4175 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4176 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4177 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4178 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4179 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4180 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4181 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4182 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4183 | } |
| 4184 | } |
| 4185 | } |
| 4186 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4187 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4188 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4189 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4190 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4191 | return BAD_VALUE; |
| 4192 | } |
| 4193 | |
| 4194 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4195 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4196 | 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] | 4197 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4198 | |
| 4199 | // nothing to do if one output is already opened for this profile |
| 4200 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4201 | for (j = 0; j < outputs.size(); j++) { |
| 4202 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4203 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4204 | // matching profile: save the sample rates, format and channel masks supported |
| 4205 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4206 | if (audio_device_is_digital(device)) { |
| 4207 | devDesc->importAudioPort(profile); |
| 4208 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4209 | break; |
| 4210 | } |
| 4211 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4212 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4213 | continue; |
| 4214 | } |
| 4215 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4216 | if (!profile->canOpenNewIo()) { |
| 4217 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4218 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4219 | continue; |
| 4220 | } |
| 4221 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4222 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4223 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4224 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4225 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4226 | status_t status = desc->open(nullptr, device, address, |
| 4227 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4228 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4229 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4230 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4231 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4232 | char *param = audio_device_address_to_parameter(device, address); |
| 4233 | mpClientInterface->setParameters(output, String8(param)); |
| 4234 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4235 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 4236 | updateAudioProfiles(devDesc, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4237 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4238 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4239 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4240 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4241 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4242 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4243 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4244 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4245 | profile->pickAudioProfile( |
| 4246 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4247 | config.offload_info.sample_rate = config.sample_rate; |
| 4248 | config.offload_info.channel_mask = config.channel_mask; |
| 4249 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4250 | |
| 4251 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4252 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4253 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4254 | output = AUDIO_IO_HANDLE_NONE; |
| 4255 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4256 | } |
| 4257 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4258 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4259 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4260 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4261 | sp<AudioPolicyMix> policyMix; |
| 4262 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4263 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4264 | address.string()); |
| 4265 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4266 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4267 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4268 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4269 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4270 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4271 | // no duplicated output for direct outputs and |
| 4272 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4273 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4274 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4275 | //TODO: configure audio effect output stage here |
| 4276 | |
| 4277 | // 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] | 4278 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4279 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4280 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4281 | &duplicatedOutput); |
| 4282 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4283 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4284 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4285 | } else { |
| 4286 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4287 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4288 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4289 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4290 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4291 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4292 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | } |
| 4294 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4295 | } else { |
| 4296 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4297 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4298 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4300 | profiles.removeAt(profile_index); |
| 4301 | profile_index--; |
| 4302 | } else { |
| 4303 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4304 | // Load digital format info only for digital devices |
| 4305 | if (audio_device_is_digital(device)) { |
| 4306 | devDesc->importAudioPort(profile); |
| 4307 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4308 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4309 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4310 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4311 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4312 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4313 | NULL/*patch handle*/, address.string()); |
| 4314 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4315 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4316 | } |
| 4317 | } |
| 4318 | |
| 4319 | if (profiles.isEmpty()) { |
| 4320 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4321 | return BAD_VALUE; |
| 4322 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4323 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4324 | // check if one opened output is not needed any more after disconnecting one device |
| 4325 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4326 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4327 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4328 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4329 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4330 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4331 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4332 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4333 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4334 | mOutputs.keyAt(i)); |
| 4335 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4336 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4337 | } |
| 4338 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4339 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4340 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4341 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4342 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4343 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4344 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4345 | "clearing direct output profile %zu on module %s", |
| 4346 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4347 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | } |
| 4352 | return NO_ERROR; |
| 4353 | } |
| 4354 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4355 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4356 | audio_policy_dev_state_t state, |
| 4357 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4358 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4359 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4360 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4361 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4362 | |
| 4363 | if (audio_device_is_digital(device)) { |
| 4364 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4365 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4366 | } |
| 4367 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4368 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4369 | // first list already open inputs that can be routed to this device |
| 4370 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4371 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4372 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4373 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4374 | inputs.add(mInputs.keyAt(input_index)); |
| 4375 | } |
| 4376 | } |
| 4377 | |
| 4378 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4379 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4380 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4381 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4382 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4383 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4384 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4385 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4386 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4387 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4388 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4389 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4390 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4391 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4392 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4393 | } |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4398 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4399 | return BAD_VALUE; |
| 4400 | } |
| 4401 | |
| 4402 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4403 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4404 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4405 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4406 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4407 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4408 | // nothing to do if one input is already opened for this profile |
| 4409 | size_t input_index; |
| 4410 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4411 | desc = mInputs.valueAt(input_index); |
| 4412 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4413 | if (audio_device_is_digital(device)) { |
| 4414 | devDesc->importAudioPort(profile); |
| 4415 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4416 | break; |
| 4417 | } |
| 4418 | } |
| 4419 | if (input_index != mInputs.size()) { |
| 4420 | continue; |
| 4421 | } |
| 4422 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4423 | if (!profile->canOpenNewIo()) { |
| 4424 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4425 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4426 | continue; |
| 4427 | } |
| 4428 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4429 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4430 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4431 | status_t status = desc->open(nullptr, |
| 4432 | device, |
| 4433 | address, |
| 4434 | AUDIO_SOURCE_MIC, |
| 4435 | AUDIO_INPUT_FLAG_NONE, |
| 4436 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4437 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4438 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4439 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4440 | char *param = audio_device_address_to_parameter(device, address); |
| 4441 | mpClientInterface->setParameters(input, String8(param)); |
| 4442 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4443 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 4444 | updateAudioProfiles(devDesc, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4445 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4446 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4447 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4448 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4449 | } |
| 4450 | |
| 4451 | if (input != 0) { |
| 4452 | addInput(input, desc); |
| 4453 | } |
| 4454 | } // endif input != 0 |
| 4455 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4456 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4457 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4458 | profiles.removeAt(profile_index); |
| 4459 | profile_index--; |
| 4460 | } else { |
| 4461 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4462 | if (audio_device_is_digital(device)) { |
| 4463 | devDesc->importAudioPort(profile); |
| 4464 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4465 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4466 | } |
| 4467 | } // end scan profiles |
| 4468 | |
| 4469 | if (profiles.isEmpty()) { |
| 4470 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4471 | return BAD_VALUE; |
| 4472 | } |
| 4473 | } else { |
| 4474 | // Disconnect |
| 4475 | // check if one opened input is not needed any more after disconnecting one device |
| 4476 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4477 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4478 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4479 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4480 | mInputs.keyAt(input_index)); |
| 4481 | inputs.add(mInputs.keyAt(input_index)); |
| 4482 | } |
| 4483 | } |
| 4484 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4485 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4486 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4487 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4488 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4489 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4490 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4491 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4492 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4493 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4494 | } |
| 4495 | } |
| 4496 | } |
| 4497 | } // end disconnect |
| 4498 | |
| 4499 | return NO_ERROR; |
| 4500 | } |
| 4501 | |
| 4502 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4503 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4504 | { |
| 4505 | ALOGV("closeOutput(%d)", output); |
| 4506 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4507 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4508 | if (outputDesc == NULL) { |
| 4509 | ALOGW("closeOutput() unknown output %d", output); |
| 4510 | return; |
| 4511 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4512 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4513 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4514 | // look for duplicated outputs connected to the output being removed. |
| 4515 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4516 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4517 | if (dupOutputDesc->isDuplicated() && |
| 4518 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4519 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4520 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4521 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4522 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4523 | } else { |
| 4524 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4525 | } |
| 4526 | // As all active tracks on duplicated output will be deleted, |
| 4527 | // and as they were also referenced on the other output, the reference |
| 4528 | // count for their stream type must be adjusted accordingly on |
| 4529 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4530 | const bool wasActive = outputDesc2->isActive(); |
| 4531 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4532 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4533 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4534 | // stop() will be a no op if the output is still active but is needed in case all |
| 4535 | // active streams refcounts where cleared above |
| 4536 | if (wasActive) { |
| 4537 | outputDesc2->stop(); |
| 4538 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4539 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4540 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4541 | |
| 4542 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4543 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4544 | } |
| 4545 | } |
| 4546 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4547 | nextAudioPortGeneration(); |
| 4548 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4549 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4550 | if (index >= 0) { |
| 4551 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4552 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4553 | mAudioPatches.removeItemsAt(index); |
| 4554 | mpClientInterface->onAudioPatchListUpdate(); |
| 4555 | } |
| 4556 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4557 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4558 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4559 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4560 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4561 | |
| 4562 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4563 | // no direct outputs are open. |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4564 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4565 | bool directOutputOpen = false; |
| 4566 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4567 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4568 | directOutputOpen = true; |
| 4569 | break; |
| 4570 | } |
| 4571 | } |
| 4572 | if (!directOutputOpen) { |
| 4573 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4574 | setMsdPatch(); |
| 4575 | } |
| 4576 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4577 | } |
| 4578 | |
| 4579 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4580 | { |
| 4581 | ALOGV("closeInput(%d)", input); |
| 4582 | |
| 4583 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4584 | if (inputDesc == NULL) { |
| 4585 | ALOGW("closeInput() unknown input %d", input); |
| 4586 | return; |
| 4587 | } |
| 4588 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4589 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4590 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4591 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4592 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4593 | if (index >= 0) { |
| 4594 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4595 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4596 | mAudioPatches.removeItemsAt(index); |
| 4597 | mpClientInterface->onAudioPatchListUpdate(); |
| 4598 | } |
| 4599 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4600 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4601 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4602 | |
| 4603 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4604 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4605 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4606 | SoundTrigger::setCaptureState(false); |
| 4607 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4608 | } |
| 4609 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4610 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4611 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4612 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4613 | { |
| 4614 | SortedVector<audio_io_handle_t> outputs; |
| 4615 | |
| 4616 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4617 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4618 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4619 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4620 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4621 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4622 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4623 | outputs.add(openOutputs.keyAt(i)); |
| 4624 | } |
| 4625 | } |
| 4626 | return outputs; |
| 4627 | } |
| 4628 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4629 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4630 | { |
| 4631 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4632 | // output is suspended before any tracks are moved to it |
| 4633 | checkA2dpSuspend(); |
| 4634 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4635 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4636 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 4637 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4638 | setMsdPatch(); |
| 4639 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4640 | } |
| 4641 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4642 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4643 | { |
| 4644 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4645 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4646 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4647 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4648 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4649 | // also take into account external policy-related changes: add all outputs which are |
| 4650 | // associated with policies in the "before" and "after" output vectors |
| 4651 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4652 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4653 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4654 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4655 | srcOutputs.add(desc->mIoHandle); |
| 4656 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4657 | } |
| 4658 | } |
| 4659 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4660 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4661 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4662 | dstOutputs.add(desc->mIoHandle); |
| 4663 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4664 | } |
| 4665 | } |
| 4666 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4667 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4668 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4669 | // audio from invalidated tracks will be rendered when unmuting |
| 4670 | uint32_t maxLatency = 0; |
| 4671 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4672 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4673 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4674 | maxLatency = desc->latency(); |
| 4675 | } |
| 4676 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4677 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4678 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4679 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4680 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4681 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4682 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4683 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4684 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4685 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4686 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4687 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4688 | if (source != 0){ |
| 4689 | connectAudioSource(source); |
| 4690 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | // Move effects associated to this strategy from previous output to new output |
| 4694 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4695 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4696 | } |
| 4697 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4698 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4699 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4700 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4701 | } |
| 4702 | } |
| 4703 | } |
| 4704 | } |
| 4705 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4706 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4707 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4708 | 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] | 4709 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4711 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4712 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4713 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4714 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4715 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4716 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4717 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4718 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4719 | } |
| 4720 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4721 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4722 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4723 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4724 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4725 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | return; |
| 4727 | } |
| 4728 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4729 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4730 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4731 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4732 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4733 | |
| 4734 | // if suspended, restore A2DP output if: |
| 4735 | // ((SCO device is NOT connected) || |
| 4736 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4737 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4738 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4739 | // if not suspended, suspend A2DP output if: |
| 4740 | // (SCO device is connected) && |
| 4741 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4742 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4743 | // |
| 4744 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4745 | if (!isScoConnected || |
| 4746 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4747 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4748 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4749 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4750 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4751 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4752 | |
| 4753 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4754 | mA2dpSuspended = false; |
| 4755 | } |
| 4756 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4757 | if (isScoConnected && |
| 4758 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4759 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4760 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4761 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4762 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4763 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4764 | |
| 4765 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4766 | mA2dpSuspended = true; |
| 4767 | } |
| 4768 | } |
| 4769 | } |
| 4770 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4771 | template <class IoDescriptor, class Filter> |
| 4772 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4773 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4774 | { |
| 4775 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4776 | auto activeClientsWithRoute = |
| 4777 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4778 | active = activeClients.size() > 0; |
| 4779 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4780 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4781 | } |
| 4782 | return nullptr; |
| 4783 | } |
| 4784 | |
| 4785 | template <class IoCollection, class Filter> |
| 4786 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4787 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4788 | { |
| 4789 | sp<DeviceDescriptor> device; |
| 4790 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4791 | auto desc = ioCollection.valueAt(i); |
| 4792 | bool active; |
| 4793 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4794 | if (active && curDevice == nullptr) { |
| 4795 | return nullptr; |
| 4796 | } else if (curDevice != nullptr) { |
| 4797 | device = curDevice; |
| 4798 | } |
| 4799 | } |
| 4800 | return device; |
| 4801 | } |
| 4802 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4803 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4804 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4805 | { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4806 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4807 | if (index >= 0) { |
| 4808 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4809 | if (patchDesc->mUid != mUidCached) { |
| 4810 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4811 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4812 | return outputDesc->device(); |
| 4813 | } |
| 4814 | } |
| 4815 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4816 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4817 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4818 | bool active; // unused |
| 4819 | sp<DeviceDescriptor> deviceDesc = |
| 4820 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
| 4821 | if (deviceDesc != nullptr) { |
| 4822 | return deviceDesc->type(); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4823 | } |
| 4824 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4825 | // 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] | 4826 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4827 | // use device for strategy enforced audible |
| 4828 | // 2: we are in call or the strategy phone is active on the output: |
| 4829 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4830 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4831 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4832 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4833 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4834 | // 5: the strategy accessibility is active on the output: |
| 4835 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4836 | // 6: the strategy "respectful" sonification is active on the output: |
| 4837 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4838 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4839 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4840 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4842 | // 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] | 4843 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4844 | |
| 4845 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4846 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4847 | // as opposed to all streams on the same audio HAL module. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4848 | audio_devices_t device = AUDIO_DEVICE_NONE; |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4849 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4850 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4851 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4852 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4853 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4854 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4855 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4856 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4857 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4858 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4859 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4860 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4861 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4863 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4865 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4866 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4867 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4868 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4869 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4870 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4871 | } |
| 4872 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4873 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4874 | return device; |
| 4875 | } |
| 4876 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4877 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4878 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4879 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4880 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4881 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4882 | if (index >= 0) { |
| 4883 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4884 | if (patchDesc->mUid != mUidCached) { |
| 4885 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4886 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4887 | return inputDesc->mDevice; |
| 4888 | } |
| 4889 | } |
| 4890 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4891 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4892 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4893 | bool active; |
| 4894 | sp<DeviceDescriptor> deviceDesc = |
| 4895 | findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4896 | if (deviceDesc != nullptr) { |
| 4897 | return deviceDesc->type(); |
| 4898 | } |
| 4899 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4900 | // If we are not in call and no client is active on this input, this methods returns |
| 4901 | // 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] | 4902 | audio_source_t source = inputDesc->source(); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4903 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4904 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4905 | } |
| 4906 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4907 | device = getDeviceAndMixForInputSource(source); |
| 4908 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4909 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4910 | return device; |
| 4911 | } |
| 4912 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4913 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4914 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4915 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4916 | } |
| 4917 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4918 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4919 | return (uint32_t)getStrategy(stream); |
| 4920 | } |
| 4921 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4922 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4923 | // By checking the range of stream before calling getStrategy, we avoid |
| 4924 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4925 | // 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] | 4926 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4927 | return AUDIO_DEVICE_NONE; |
| 4928 | } |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4929 | audio_devices_t activeDevices = AUDIO_DEVICE_NONE; |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4930 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4931 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4932 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4933 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4934 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4935 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4936 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4937 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4938 | devices |= curDevices; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4939 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4940 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4941 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4942 | activeDevices |= outputDesc->device(); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4943 | } |
| 4944 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4945 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4946 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4947 | // Favor devices selected on active streams if any to report correct device in case of |
| 4948 | // explicit device selection |
| 4949 | if (activeDevices != AUDIO_DEVICE_NONE) { |
| 4950 | devices = activeDevices; |
| 4951 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4952 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4953 | and doesn't really need to.*/ |
| 4954 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4955 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4956 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4957 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4958 | return devices; |
| 4959 | } |
| 4960 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4961 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4962 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4963 | 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] | 4964 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4965 | } |
| 4966 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4967 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4968 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4969 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4970 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4971 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4972 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4973 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4974 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4975 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4976 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4977 | } |
| 4978 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4979 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4980 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4981 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4982 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4983 | updateDevicesAndOutputs(); |
| 4984 | break; |
| 4985 | default: |
| 4986 | break; |
| 4987 | } |
| 4988 | } |
| 4989 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4990 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4991 | |
| 4992 | // skip beacon mute management if a dedicated TTS output is available |
| 4993 | if (mTtsOutputAvailable) { |
| 4994 | return 0; |
| 4995 | } |
| 4996 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4997 | switch(event) { |
| 4998 | case STARTING_OUTPUT: |
| 4999 | mBeaconMuteRefCount++; |
| 5000 | break; |
| 5001 | case STOPPING_OUTPUT: |
| 5002 | if (mBeaconMuteRefCount > 0) { |
| 5003 | mBeaconMuteRefCount--; |
| 5004 | } |
| 5005 | break; |
| 5006 | case STARTING_BEACON: |
| 5007 | mBeaconPlayingRefCount++; |
| 5008 | break; |
| 5009 | case STOPPING_BEACON: |
| 5010 | if (mBeaconPlayingRefCount > 0) { |
| 5011 | mBeaconPlayingRefCount--; |
| 5012 | } |
| 5013 | break; |
| 5014 | } |
| 5015 | |
| 5016 | if (mBeaconMuteRefCount > 0) { |
| 5017 | // any playback causes beacon to be muted |
| 5018 | return setBeaconMute(true); |
| 5019 | } else { |
| 5020 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5021 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5022 | } |
| 5023 | } |
| 5024 | |
| 5025 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5026 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5027 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5028 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5029 | if (mBeaconMuted != mute) { |
| 5030 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5031 | ALOGV("\t muting %d", mute); |
| 5032 | uint32_t maxLatency = 0; |
| 5033 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5034 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5035 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5036 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5037 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5038 | const uint32_t latency = desc->latency() * 2; |
| 5039 | if (latency > maxLatency) { |
| 5040 | maxLatency = latency; |
| 5041 | } |
| 5042 | } |
| 5043 | mBeaconMuted = mute; |
| 5044 | return maxLatency; |
| 5045 | } |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5049 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5050 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5051 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5052 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5053 | // case the last active client route is used |
| 5054 | sp<DeviceDescriptor> deviceDesc = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5055 | if (deviceDesc != nullptr) { |
| 5056 | return deviceDesc->type(); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5057 | } |
| 5058 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5059 | if (fromCache) { |
| 5060 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5061 | strategy, mDeviceForStrategy[strategy]); |
| 5062 | return mDeviceForStrategy[strategy]; |
| 5063 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5064 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5065 | } |
| 5066 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5067 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5068 | { |
| 5069 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5070 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5071 | } |
| 5072 | mPreviousOutputs = mOutputs; |
| 5073 | } |
| 5074 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5075 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5076 | audio_devices_t prevDevice, |
| 5077 | uint32_t delayMs) |
| 5078 | { |
| 5079 | // mute/unmute strategies using an incompatible device combination |
| 5080 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5081 | // if unmuting, unmute only after the specified delay |
| 5082 | if (outputDesc->isDuplicated()) { |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | uint32_t muteWaitMs = 0; |
| 5087 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5088 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5089 | |
| 5090 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5091 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5092 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5093 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5094 | bool doMute = false; |
| 5095 | |
| 5096 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5097 | doMute = true; |
| 5098 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5099 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5100 | doMute = true; |
| 5101 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5102 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5103 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5104 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5105 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5106 | // skip output if it does not share any device with current output |
| 5107 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5108 | == AUDIO_DEVICE_NONE) { |
| 5109 | continue; |
| 5110 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5111 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5112 | mute ? "muting" : "unmuting", i, curDevice); |
| 5113 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5114 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5115 | if (mute) { |
| 5116 | // FIXME: should not need to double latency if volume could be applied |
| 5117 | // immediately by the audioflinger mixer. We must account for the delay |
| 5118 | // between now and the next time the audioflinger thread for this output |
| 5119 | // will process a buffer (which corresponds to one buffer size, |
| 5120 | // usually 1/2 or 1/4 of the latency). |
| 5121 | if (muteWaitMs < desc->latency() * 2) { |
| 5122 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5123 | } |
| 5124 | } |
| 5125 | } |
| 5126 | } |
| 5127 | } |
| 5128 | } |
| 5129 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5130 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5131 | // different per device volumes |
| 5132 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5133 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5134 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5135 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5136 | if (muteWaitMs < tempMuteWaitMs) { |
| 5137 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5138 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5139 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5140 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5141 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5142 | // make sure that we do not start the temporary mute period too early in case of |
| 5143 | // delayed device change |
| 5144 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5145 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5146 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5147 | } |
| 5148 | } |
| 5149 | } |
| 5150 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5151 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5152 | if (muteWaitMs > delayMs) { |
| 5153 | muteWaitMs -= delayMs; |
| 5154 | usleep(muteWaitMs * 1000); |
| 5155 | return muteWaitMs; |
| 5156 | } |
| 5157 | return 0; |
| 5158 | } |
| 5159 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5160 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5161 | audio_devices_t device, |
| 5162 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5163 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5164 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5165 | const char *address, |
| 5166 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5167 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5168 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5169 | AudioParameter param; |
| 5170 | uint32_t muteWaitMs; |
| 5171 | |
| 5172 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5173 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5174 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5175 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5176 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5177 | return muteWaitMs; |
| 5178 | } |
| 5179 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5180 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5181 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5182 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5183 | return 0; |
| 5184 | } |
| 5185 | |
| 5186 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5187 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5188 | |
| 5189 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5190 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5191 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5192 | |
| 5193 | if (device != AUDIO_DEVICE_NONE) { |
| 5194 | outputDesc->mDevice = device; |
| 5195 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5196 | |
| 5197 | // if the outputs are not materially active, there is no need to mute. |
| 5198 | if (requiresMuteCheck) { |
| 5199 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5200 | } else { |
| 5201 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5202 | muteWaitMs = 0; |
| 5203 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | |
| 5205 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5206 | // the requested device is AUDIO_DEVICE_NONE |
| 5207 | // OR the requested device is the same as current device |
| 5208 | // AND force is not specified |
| 5209 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5210 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5211 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5212 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5213 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5214 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5215 | return muteWaitMs; |
| 5216 | } |
| 5217 | |
| 5218 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5219 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5220 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5221 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5222 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5223 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5224 | DeviceVector deviceList; |
| 5225 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5226 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5227 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5228 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5229 | device, String8(address)); |
| 5230 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5231 | } |
| 5232 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5233 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5234 | PatchBuilder patchBuilder; |
| 5235 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5236 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5237 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5238 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5239 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5240 | } |
| 5241 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5242 | |
| 5243 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5244 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5245 | |
| 5246 | return muteWaitMs; |
| 5247 | } |
| 5248 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5249 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5250 | int delayMs, |
| 5251 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5252 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5253 | ssize_t index; |
| 5254 | if (patchHandle) { |
| 5255 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5256 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5257 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5258 | } |
| 5259 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5260 | return INVALID_OPERATION; |
| 5261 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5262 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5263 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5264 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5265 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5266 | removeAudioPatch(patchDesc->mHandle); |
| 5267 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5268 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5269 | return status; |
| 5270 | } |
| 5271 | |
| 5272 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5273 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5274 | bool force, |
| 5275 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5276 | { |
| 5277 | status_t status = NO_ERROR; |
| 5278 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5279 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5280 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5281 | inputDesc->mDevice = device; |
| 5282 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5283 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5284 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5285 | PatchBuilder patchBuilder; |
| 5286 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5287 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5288 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5289 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5290 | auto result = usecase; |
| 5291 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5292 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5293 | } |
| 5294 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5295 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5296 | addSource(deviceList.itemAt(0)); |
| 5297 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | return status; |
| 5301 | } |
| 5302 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5303 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5304 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5305 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5306 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5307 | ssize_t index; |
| 5308 | if (patchHandle) { |
| 5309 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5310 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5311 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5312 | } |
| 5313 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5314 | return INVALID_OPERATION; |
| 5315 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5316 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5317 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5318 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5319 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5320 | removeAudioPatch(patchDesc->mHandle); |
| 5321 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5322 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5323 | return status; |
| 5324 | } |
| 5325 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5326 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5327 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5328 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5329 | audio_format_t& format, |
| 5330 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5331 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5332 | { |
| 5333 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5334 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5335 | // |
| 5336 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5337 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5338 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5339 | sp<IOProfile> firstInexact; |
| 5340 | uint32_t updatedSamplingRate = 0; |
| 5341 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5342 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5343 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5344 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5345 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5346 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5347 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5348 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5349 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5350 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5351 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5352 | &channelMask /*updatedChannelMask*/, |
| 5353 | // FIXME ugly cast |
| 5354 | (audio_output_flags_t) flags, |
| 5355 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5356 | return profile; |
| 5357 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5358 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5359 | samplingRate, |
| 5360 | &updatedSamplingRate, |
| 5361 | format, |
| 5362 | &updatedFormat, |
| 5363 | channelMask, |
| 5364 | &updatedChannelMask, |
| 5365 | // FIXME ugly cast |
| 5366 | (audio_output_flags_t) flags, |
| 5367 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5368 | firstInexact = profile; |
| 5369 | } |
| 5370 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5371 | } |
| 5372 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5373 | if (firstInexact != nullptr) { |
| 5374 | samplingRate = updatedSamplingRate; |
| 5375 | format = updatedFormat; |
| 5376 | channelMask = updatedChannelMask; |
| 5377 | return firstInexact; |
| 5378 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5379 | return NULL; |
| 5380 | } |
| 5381 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5382 | |
| 5383 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5384 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5385 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5386 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5387 | // case the last active client route is used |
| 5388 | sp<DeviceDescriptor> deviceDesc = |
| 5389 | findPreferredDevice(mInputs, inputSource, mAvailableInputDevices); |
| 5390 | if (deviceDesc != nullptr) { |
| 5391 | return deviceDesc->type(); |
| 5392 | } |
| 5393 | |
| 5394 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5395 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5396 | audio_devices_t selectedDeviceFromMix = |
| 5397 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5398 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5399 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5400 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5401 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5402 | return getDeviceForInputSource(inputSource); |
| 5403 | } |
| 5404 | |
| 5405 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5406 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5407 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5408 | } |
| 5409 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5410 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5411 | int index, |
| 5412 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5413 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5414 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5415 | |
| 5416 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5417 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5418 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5419 | // the ringtone volume |
| 5420 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5421 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5422 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5423 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5424 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5425 | } |
| 5426 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5427 | // in-call: always cap volume by voice volume + some low headroom |
| 5428 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5429 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5430 | switch (stream) { |
| 5431 | case AUDIO_STREAM_SYSTEM: |
| 5432 | case AUDIO_STREAM_RING: |
| 5433 | case AUDIO_STREAM_MUSIC: |
| 5434 | case AUDIO_STREAM_ALARM: |
| 5435 | case AUDIO_STREAM_NOTIFICATION: |
| 5436 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5437 | case AUDIO_STREAM_DTMF: |
| 5438 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5439 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5440 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5441 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5442 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5443 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5444 | if (volumeDB > maxVoiceVolDb) { |
| 5445 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5446 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5447 | volumeDB = maxVoiceVolDb; |
| 5448 | } |
| 5449 | } break; |
| 5450 | default: |
| 5451 | break; |
| 5452 | } |
| 5453 | } |
| 5454 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5455 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5456 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5457 | // - always attenuate notifications volume by 6dB |
| 5458 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5459 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5460 | // - if music is playing, always limit the volume to current music volume, |
| 5461 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5462 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5463 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5464 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5465 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5466 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5467 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5468 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5469 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5470 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5471 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5472 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5473 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5474 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5475 | // when the phone is ringing we must consider that music could have been paused just before |
| 5476 | // by the music application and behave as if music was active if the last music track was |
| 5477 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5478 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5479 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5480 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5481 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5482 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5483 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5484 | musicDevice), |
| 5485 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5486 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5487 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5488 | if (volumeDB > minVolDB) { |
| 5489 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5490 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5491 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5492 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5493 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5494 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5495 | // intended to be played |
| 5496 | if ((volumeDB > -96.0f) && |
| 5497 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5498 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5499 | stream, device, |
| 5500 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5501 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5502 | } |
| 5503 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5504 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5505 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5506 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5507 | } |
| 5508 | } |
| 5509 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5510 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5511 | } |
| 5512 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5513 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5514 | audio_stream_type_t srcStream, |
| 5515 | audio_stream_type_t dstStream) |
| 5516 | { |
| 5517 | if (srcStream == dstStream) { |
| 5518 | return srcIndex; |
| 5519 | } |
| 5520 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5521 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5522 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5523 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5524 | |
| 5525 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5526 | } |
| 5527 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5528 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5529 | int index, |
| 5530 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5531 | audio_devices_t device, |
| 5532 | int delayMs, |
| 5533 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5534 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5535 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5536 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5537 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5538 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5539 | return NO_ERROR; |
| 5540 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5541 | audio_policy_forced_cfg_t forceUseForComm = |
| 5542 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5543 | // 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] | 5544 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5545 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5546 | 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] | 5547 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5548 | return INVALID_OPERATION; |
| 5549 | } |
| 5550 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5551 | if (device == AUDIO_DEVICE_NONE) { |
| 5552 | device = outputDesc->device(); |
| 5553 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5554 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5555 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5556 | if (outputDesc->isFixedVolume(device) || |
| 5557 | // Force VoIP volume to max for bluetooth SCO |
| 5558 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5559 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5560 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5561 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5562 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5563 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5564 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5565 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5566 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5567 | float voiceVolume; |
| 5568 | // 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] | 5569 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5570 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5571 | } else { |
| 5572 | voiceVolume = 1.0; |
| 5573 | } |
| 5574 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5575 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5576 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5577 | mLastVoiceVolume = voiceVolume; |
| 5578 | } |
| 5579 | } |
| 5580 | |
| 5581 | return NO_ERROR; |
| 5582 | } |
| 5583 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5584 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5585 | audio_devices_t device, |
| 5586 | int delayMs, |
| 5587 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5588 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5589 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5590 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5591 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5592 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5593 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5594 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5595 | device, |
| 5596 | delayMs, |
| 5597 | force); |
| 5598 | } |
| 5599 | } |
| 5600 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5601 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5602 | bool on, |
| 5603 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5604 | int delayMs, |
| 5605 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5606 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5607 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5608 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5609 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5610 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5611 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5612 | } |
| 5613 | } |
| 5614 | } |
| 5615 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5616 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5617 | bool on, |
| 5618 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5619 | int delayMs, |
| 5620 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5621 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5622 | if (device == AUDIO_DEVICE_NONE) { |
| 5623 | device = outputDesc->device(); |
| 5624 | } |
| 5625 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5626 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5627 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5628 | |
| 5629 | if (on) { |
| 5630 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5631 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5632 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5633 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5634 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5635 | } |
| 5636 | } |
| 5637 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5638 | outputDesc->mMuteCount[stream]++; |
| 5639 | } else { |
| 5640 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5641 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5642 | return; |
| 5643 | } |
| 5644 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5645 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5646 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5647 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5648 | device, |
| 5649 | delayMs); |
| 5650 | } |
| 5651 | } |
| 5652 | } |
| 5653 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5654 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5655 | { |
| 5656 | // flags to stream type mapping |
| 5657 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5658 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5659 | } |
| 5660 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5661 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5662 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5663 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5664 | return AUDIO_STREAM_TTS; |
| 5665 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5666 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5667 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5668 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5669 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5670 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5671 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5672 | // has flags that map to a strategy? |
| 5673 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5674 | return true; |
| 5675 | } |
| 5676 | |
| 5677 | // has known usage? |
| 5678 | switch (paa->usage) { |
| 5679 | case AUDIO_USAGE_UNKNOWN: |
| 5680 | case AUDIO_USAGE_MEDIA: |
| 5681 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5682 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5683 | case AUDIO_USAGE_ALARM: |
| 5684 | case AUDIO_USAGE_NOTIFICATION: |
| 5685 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5686 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5687 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5688 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5689 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5690 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5691 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5692 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5693 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5694 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5695 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5696 | break; |
| 5697 | default: |
| 5698 | return false; |
| 5699 | } |
| 5700 | return true; |
| 5701 | } |
| 5702 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5703 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5704 | routing_strategy strategy, uint32_t inPastMs, |
| 5705 | nsecs_t sysTime) const |
| 5706 | { |
| 5707 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5708 | sysTime = systemTime(); |
| 5709 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5710 | 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] | 5711 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5712 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5713 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5714 | return true; |
| 5715 | } |
| 5716 | } |
| 5717 | return false; |
| 5718 | } |
| 5719 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5720 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5721 | routing_strategy strategy, uint32_t inPastMs, |
| 5722 | nsecs_t sysTime) const |
| 5723 | { |
| 5724 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5725 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5726 | if (outputDesc->sharesHwModuleWith(desc) |
| 5727 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5728 | return true; |
| 5729 | } |
| 5730 | } |
| 5731 | return false; |
| 5732 | } |
| 5733 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5734 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5735 | { |
| 5736 | return mEngine->getForceUse(usage); |
| 5737 | } |
| 5738 | |
| 5739 | bool AudioPolicyManager::isInCall() |
| 5740 | { |
| 5741 | return isStateInCall(mEngine->getPhoneState()); |
| 5742 | } |
| 5743 | |
| 5744 | bool AudioPolicyManager::isStateInCall(int state) |
| 5745 | { |
| 5746 | return is_state_in_call(state); |
| 5747 | } |
| 5748 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5749 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5750 | { |
| 5751 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5752 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5753 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5754 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5755 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5756 | } |
| 5757 | } |
| 5758 | |
| 5759 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5760 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5761 | bool release = false; |
| 5762 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5763 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5764 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5765 | source->ext.device.type == deviceDesc->type()) { |
| 5766 | release = true; |
| 5767 | } |
| 5768 | } |
| 5769 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5770 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5771 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5772 | sink->ext.device.type == deviceDesc->type()) { |
| 5773 | release = true; |
| 5774 | } |
| 5775 | } |
| 5776 | if (release) { |
| 5777 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5778 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5779 | } |
| 5780 | } |
| 5781 | } |
| 5782 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5783 | void AudioPolicyManager::modifySurroundFormats( |
| 5784 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5785 | std::unordered_set<audio_format_t> enforcedSurround( |
| 5786 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5787 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 5788 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5789 | allSurround.insert(pair.first); |
| 5790 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 5791 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5792 | |
| 5793 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5794 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5795 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5796 | // This is the resulting set of formats depending on the surround mode: |
| 5797 | // 'all surround' = allSurround |
| 5798 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 5799 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 5800 | // 'manual surround' = mManualSurroundFormats |
| 5801 | // AUTO: formats v 'enforced surround' |
| 5802 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 5803 | // NEVER: formats ^ 'non-surround' |
| 5804 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5805 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5806 | std::unordered_set<audio_format_t> formatSet; |
| 5807 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 5808 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5809 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5810 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5811 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5812 | formatSet.insert(*formatIter); |
| 5813 | } |
| 5814 | } |
| 5815 | } else { |
| 5816 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 5817 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5818 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5819 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5820 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5821 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5822 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 5823 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 5824 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5825 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5826 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 5827 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5828 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5829 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5830 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5831 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5832 | for (const auto& format : formatSet) { |
| 5833 | formatsPtr->push(format); |
| 5834 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5835 | } |
| 5836 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5837 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5838 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5839 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5840 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5841 | |
| 5842 | // If NEVER, then remove support for channelMasks > stereo. |
| 5843 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5844 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5845 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5846 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5847 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5848 | channelMasks.removeAt(maskIndex); |
| 5849 | } else { |
| 5850 | maskIndex++; |
| 5851 | } |
| 5852 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5853 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5854 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5855 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5856 | bool supports5dot1 = false; |
| 5857 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5858 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5859 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5860 | supports5dot1 = true; |
| 5861 | break; |
| 5862 | } |
| 5863 | } |
| 5864 | // If not then add 5.1 support. |
| 5865 | if (!supports5dot1) { |
| 5866 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5867 | 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] | 5868 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5869 | } |
| 5870 | } |
| 5871 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5872 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5873 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5874 | AudioProfileVector &profiles) |
| 5875 | { |
| 5876 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5877 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5878 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5879 | // Format MUST be checked first to update the list of AudioProfile |
| 5880 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5881 | reply = mpClientInterface->getParameters( |
| 5882 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5883 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5884 | AudioParameter repliedParameters(reply); |
| 5885 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5886 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5887 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5888 | return; |
| 5889 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5890 | FormatVector formats = formatsFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5891 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5892 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5893 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5894 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5895 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5896 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5897 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5898 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5899 | ChannelsVector channelMasks; |
| 5900 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5901 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5902 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5903 | |
| 5904 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5905 | reply = mpClientInterface->getParameters( |
| 5906 | ioHandle, |
| 5907 | requestedParameters.toString() + ";" + |
| 5908 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5909 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5910 | AudioParameter repliedParameters(reply); |
| 5911 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5912 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5913 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5914 | } |
| 5915 | } |
| 5916 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5917 | reply = mpClientInterface->getParameters(ioHandle, |
| 5918 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5919 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5920 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5921 | AudioParameter repliedParameters(reply); |
| 5922 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5923 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5924 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5925 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5926 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5927 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5928 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5929 | } |
| 5930 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5931 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5932 | } |
| 5933 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5934 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5935 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5936 | audio_patch_handle_t *patchHandle, |
| 5937 | AudioIODescriptorInterface *ioDescriptor, |
| 5938 | const struct audio_patch *patch, |
| 5939 | int delayMs) |
| 5940 | { |
| 5941 | ssize_t index = mAudioPatches.indexOfKey( |
| 5942 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 5943 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 5944 | sp<AudioPatch> patchDesc; |
| 5945 | status_t status = installPatch( |
| 5946 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 5947 | if (status == NO_ERROR) { |
| 5948 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 5949 | } |
| 5950 | return status; |
| 5951 | } |
| 5952 | |
| 5953 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5954 | ssize_t index, |
| 5955 | audio_patch_handle_t *patchHandle, |
| 5956 | const struct audio_patch *patch, |
| 5957 | int delayMs, |
| 5958 | uid_t uid, |
| 5959 | sp<AudioPatch> *patchDescPtr) |
| 5960 | { |
| 5961 | sp<AudioPatch> patchDesc; |
| 5962 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5963 | if (index >= 0) { |
| 5964 | patchDesc = mAudioPatches.valueAt(index); |
| 5965 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5966 | } |
| 5967 | |
| 5968 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 5969 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 5970 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 5971 | if (status == NO_ERROR) { |
| 5972 | if (index < 0) { |
| 5973 | patchDesc = new AudioPatch(patch, uid); |
| 5974 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5975 | } else { |
| 5976 | patchDesc->mPatch = *patch; |
| 5977 | } |
| 5978 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 5979 | if (patchHandle) { |
| 5980 | *patchHandle = patchDesc->mHandle; |
| 5981 | } |
| 5982 | nextAudioPortGeneration(); |
| 5983 | mpClientInterface->onAudioPatchListUpdate(); |
| 5984 | } |
| 5985 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 5986 | return status; |
| 5987 | } |
| 5988 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 5989 | } // namespace android |