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 | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 39 | #include <vector> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 40 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 41 | #include <AudioPolicyManagerInterface.h> |
| 42 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 43 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 44 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 45 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 46 | #include <media/AudioPolicyHelper.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 47 | #include <private/android_filesystem_config.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 48 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 49 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 50 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 51 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 52 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 53 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 54 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 56 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 57 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 58 | //FIXME: workaround for truncated touch sounds |
| 59 | // to be removed when the problem is handled by system UI |
| 60 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 61 | |
| 62 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 63 | // media / notification / system volume. |
| 64 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 65 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 66 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 67 | static const std::vector<audio_format_t> compressedFormatsOrder = {{ |
| 68 | AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 69 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 70 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
| 71 | static const std::vector<audio_channel_mask_t> surroundChannelMasksOrder = {{ |
| 72 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 73 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 74 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 75 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 76 | // ---------------------------------------------------------------------------- |
| 77 | // AudioPolicyInterface implementation |
| 78 | // ---------------------------------------------------------------------------- |
| 79 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 80 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 81 | audio_policy_dev_state_t state, |
| 82 | const char *device_address, |
| 83 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 84 | { |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 85 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); |
| 86 | nextAudioPortGeneration(); |
| 87 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 88 | } |
| 89 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 90 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 91 | audio_policy_dev_state_t state, |
| 92 | const String8 &device_address) |
| 93 | { |
| 94 | AudioParameter param(device_address); |
| 95 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 96 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 97 | param.addInt(key, device); |
| 98 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 99 | } |
| 100 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 101 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 102 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 103 | const char *device_address, |
| 104 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 105 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 106 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 107 | device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 108 | |
| 109 | // connect/disconnect only 1 device at a time |
| 110 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 111 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 112 | sp<DeviceDescriptor> devDesc = |
| 113 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 114 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 115 | // handle output devices |
| 116 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 117 | SortedVector <audio_io_handle_t> outputs; |
| 118 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 119 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 120 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 121 | // save a copy of the opened output descriptors before any output is opened or closed |
| 122 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 123 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 124 | switch (state) |
| 125 | { |
| 126 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 127 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 128 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 129 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 130 | return INVALID_OPERATION; |
| 131 | } |
| 132 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 133 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 135 | index = mAvailableOutputDevices.add(devDesc); |
| 136 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 137 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 138 | if (module == 0) { |
| 139 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 140 | device); |
| 141 | mAvailableOutputDevices.remove(devDesc); |
| 142 | return INVALID_OPERATION; |
| 143 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 144 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 145 | } else { |
| 146 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 147 | } |
| 148 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 149 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 150 | // parameters on newly connected devices (instead of opening the outputs...) |
| 151 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 152 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 153 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 154 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 155 | |
| 156 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 157 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 158 | return INVALID_OPERATION; |
| 159 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 160 | // Propagate device availability to Engine |
| 161 | mEngine->setDeviceConnectionState(devDesc, state); |
| 162 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 163 | // outputs should never be empty here |
| 164 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 165 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 166 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 167 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 168 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 169 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 170 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 171 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 172 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 173 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 174 | return INVALID_OPERATION; |
| 175 | } |
| 176 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 177 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 178 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 179 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 180 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 181 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 182 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 183 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 185 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 186 | |
| 187 | // Propagate device availability to Engine |
| 188 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 189 | } break; |
| 190 | |
| 191 | default: |
| 192 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 193 | return BAD_VALUE; |
| 194 | } |
| 195 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 196 | checkForDeviceAndOutputChanges([&]() { |
| 197 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 198 | if (!outputs.isEmpty()) { |
| 199 | for (audio_io_handle_t output : outputs) { |
| 200 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
| 201 | // close unused outputs after device disconnection or direct outputs that have been |
| 202 | // opened by checkOutputsForDevice() to query dynamic parameters |
| 203 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 204 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 205 | (desc->mDirectOpenCount == 0))) { |
| 206 | closeOutput(output); |
| 207 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 208 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 209 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 210 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 211 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 212 | return false; |
| 213 | }); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 214 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 215 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 216 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 217 | updateCallRouting(newDevice); |
| 218 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 219 | const audio_devices_t msdOutDevice = getMsdAudioOutDeviceTypes(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 220 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 221 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 222 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 223 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 224 | // do not force device change on duplicated output because if device is 0, it will |
| 225 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 226 | // a valid device selection on those outputs. |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 227 | bool force = (msdOutDevice == AUDIO_DEVICE_NONE || msdOutDevice != desc->device()) |
| 228 | && !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 229 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 230 | // always force when disconnecting (a non-duplicated device) |
| 231 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 232 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 233 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 236 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 237 | cleanUpForDevice(devDesc); |
| 238 | } |
| 239 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 240 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 241 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 242 | } // end if is output device |
| 243 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 244 | // handle input devices |
| 245 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 246 | SortedVector <audio_io_handle_t> inputs; |
| 247 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 248 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | switch (state) |
| 250 | { |
| 251 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 252 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 253 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 254 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 255 | return INVALID_OPERATION; |
| 256 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 257 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 258 | if (module == NULL) { |
| 259 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 260 | device); |
| 261 | return INVALID_OPERATION; |
| 262 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 263 | |
| 264 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 265 | // parameters on newly connected devices (instead of opening the inputs...) |
| 266 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 267 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 268 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 269 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 270 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 271 | return INVALID_OPERATION; |
| 272 | } |
| 273 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 274 | index = mAvailableInputDevices.add(devDesc); |
| 275 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 276 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 277 | } else { |
| 278 | return NO_MEMORY; |
| 279 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 280 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 281 | // Propagate device availability to Engine |
| 282 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 283 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | |
| 285 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 286 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 287 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 288 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 289 | return INVALID_OPERATION; |
| 290 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 291 | |
| 292 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 293 | |
| 294 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 295 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 296 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 297 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 298 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 299 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 300 | // Propagate device availability to Engine |
| 301 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 302 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | |
| 304 | default: |
| 305 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 306 | return BAD_VALUE; |
| 307 | } |
| 308 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 309 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 310 | // As the input device list can impact the output device selection, update |
| 311 | // getDeviceForStrategy() cache |
| 312 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 313 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 314 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 315 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 316 | updateCallRouting(newDevice); |
| 317 | } |
| 318 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 319 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 320 | cleanUpForDevice(devDesc); |
| 321 | } |
| 322 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 323 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 324 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 325 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 326 | |
| 327 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 328 | return BAD_VALUE; |
| 329 | } |
| 330 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 331 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 332 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 333 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 334 | sp<DeviceDescriptor> devDesc = |
| 335 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 336 | (strlen(device_address) != 0)/*matchAddress*/); |
| 337 | |
| 338 | if (devDesc == 0) { |
| 339 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 340 | device, device_address); |
| 341 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 342 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 343 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 344 | DeviceVector *deviceVector; |
| 345 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 346 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 347 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 348 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 349 | deviceVector = &mAvailableInputDevices; |
| 350 | } else { |
| 351 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 352 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 353 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 354 | |
| 355 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 356 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 359 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 360 | const char *device_address, |
| 361 | const char *device_name) |
| 362 | { |
| 363 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 364 | String8 reply; |
| 365 | AudioParameter param; |
| 366 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 367 | |
| 368 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 369 | device, device_address, device_name); |
| 370 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 371 | // connect/disconnect only 1 device at a time |
| 372 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 373 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 374 | // Check if the device is currently connected |
| 375 | sp<DeviceDescriptor> devDesc = |
| 376 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 377 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 378 | if (index < 0) { |
| 379 | // Nothing to do: device is not connected |
| 380 | return NO_ERROR; |
| 381 | } |
| 382 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 383 | // For offloaded A2DP, Hw modules may have the capability to |
| 384 | // configure codecs. Check if any of the loaded hw modules |
| 385 | // supports this. |
| 386 | // If supported, send a set parameter to configure A2DP codecs |
| 387 | // and return. No need to toggle device state. |
| 388 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 389 | reply = mpClientInterface->getParameters( |
| 390 | AUDIO_IO_HANDLE_NONE, |
| 391 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 392 | AudioParameter repliedParameters(reply); |
| 393 | repliedParameters.getInt( |
| 394 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 395 | if (isReconfigA2dpSupported) { |
| 396 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 397 | param.add(key, String8("true")); |
| 398 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 399 | return NO_ERROR; |
| 400 | } |
| 401 | } |
| 402 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 403 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 404 | // This will force reading again the device configuration |
| 405 | status = setDeviceConnectionState(device, |
| 406 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 407 | device_address, device_name); |
| 408 | if (status != NO_ERROR) { |
| 409 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 410 | status); |
| 411 | return status; |
| 412 | } |
| 413 | |
| 414 | status = setDeviceConnectionState(device, |
| 415 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 416 | device_address, device_name); |
| 417 | if (status != NO_ERROR) { |
| 418 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 419 | status); |
| 420 | return status; |
| 421 | } |
| 422 | |
| 423 | return NO_ERROR; |
| 424 | } |
| 425 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 426 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 427 | { |
| 428 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 429 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 430 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 431 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 432 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 433 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 434 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 435 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 436 | |
| 437 | // release existing RX patch if any |
| 438 | if (mCallRxPatch != 0) { |
| 439 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 440 | mCallRxPatch.clear(); |
| 441 | } |
| 442 | // release TX patch if any |
| 443 | if (mCallTxPatch != 0) { |
| 444 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 445 | mCallTxPatch.clear(); |
| 446 | } |
| 447 | |
| 448 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 449 | // via setOutputDevice() on primary output. |
| 450 | // Otherwise, create two audio patches for TX and RX path. |
| 451 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 452 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 453 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 454 | // of it due to legacy implementation. If not, create a patch. |
| 455 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 456 | == AUDIO_DEVICE_NONE) { |
| 457 | createTxPatch = true; |
| 458 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 459 | } else { // create RX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 460 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 461 | createTxPatch = true; |
| 462 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 463 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 464 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 465 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 466 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 467 | return muteWaitMs; |
| 468 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 469 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 470 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
| 471 | bool isRx, audio_devices_t device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 472 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 473 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 474 | sp<DeviceDescriptor> txSourceDeviceDesc; |
| 475 | if (isRx) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 476 | patchBuilder.addSink(findDevice(mAvailableOutputDevices, device)). |
| 477 | addSource(findDevice(mAvailableInputDevices, AUDIO_DEVICE_IN_TELEPHONY_RX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 478 | } else { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 479 | patchBuilder.addSource(txSourceDeviceDesc = findDevice(mAvailableInputDevices, device)). |
| 480 | addSink(findDevice(mAvailableOutputDevices, AUDIO_DEVICE_OUT_TELEPHONY_TX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | audio_devices_t outputDevice = isRx ? device : AUDIO_DEVICE_OUT_TELEPHONY_TX; |
| 484 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(outputDevice, mOutputs); |
| 485 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 486 | // request to reuse existing output stream if one is already opened to reach the target device |
| 487 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 488 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 489 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 490 | "%s() %#x device output %d is duplicated", __func__, outputDevice, output); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 491 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 495 | // terminate active capture if on the same HW module as the call TX source device |
| 496 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 497 | // call TX device but this information is not in the audio patch and logic here must be |
| 498 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 499 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 500 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 501 | closeActiveClients(activeDesc); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 504 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 505 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 506 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 507 | status_t status = mpClientInterface->createAudioPatch( |
| 508 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 509 | ALOGW_IF(status != NO_ERROR, |
| 510 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 511 | sp<AudioPatch> audioPatch; |
| 512 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 513 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 514 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 515 | audioPatch->mUid = mUidCached; |
| 516 | } |
| 517 | return audioPatch; |
| 518 | } |
| 519 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 520 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 521 | const DeviceVector& devices, audio_devices_t device) const { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 522 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 523 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 524 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 525 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 528 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 529 | { |
| 530 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 531 | // store previous phone state for management of sonification strategy below |
| 532 | int oldState = mEngine->getPhoneState(); |
| 533 | |
| 534 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 535 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | return; |
| 537 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 538 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 539 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 541 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 542 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 543 | } |
| 544 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 545 | /** |
| 546 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 547 | * routing command. |
| 548 | */ |
| 549 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 550 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 551 | |
| 552 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 553 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 555 | int delayMs = 0; |
| 556 | if (isStateInCall(state)) { |
| 557 | nsecs_t sysTime = systemTime(); |
| 558 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 559 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | // mute media and sonification strategies and delay device switch by the largest |
| 561 | // latency of any output where either strategy is active. |
| 562 | // 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] | 563 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 564 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 565 | sysTime) || |
| 566 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 567 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 568 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 569 | (delayMs < (int)desc->latency()*2)) { |
| 570 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 572 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 573 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 574 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 575 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 576 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 577 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 578 | } |
| 579 | } |
| 580 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 581 | if (hasPrimaryOutput()) { |
| 582 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 583 | // the device returned is not necessarily reachable via this output |
| 584 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 585 | // force routing command to audio hardware when ending call |
| 586 | // even if no device change is needed |
| 587 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 588 | rxDevice = mPrimaryOutput->device(); |
| 589 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 590 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 591 | if (state == AUDIO_MODE_IN_CALL) { |
| 592 | updateCallRouting(rxDevice, delayMs); |
| 593 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 594 | if (mCallRxPatch != 0) { |
| 595 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 596 | mCallRxPatch.clear(); |
| 597 | } |
| 598 | if (mCallTxPatch != 0) { |
| 599 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 600 | mCallTxPatch.clear(); |
| 601 | } |
| 602 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 603 | } else { |
| 604 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 605 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 606 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 607 | |
| 608 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 609 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 610 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 611 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
| 612 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
Yung Ti Su | f60c824 | 2018-05-10 18:07:26 +0800 | [diff] [blame] | 613 | setOutputDevice(desc, newDevice, (newDevice != AUDIO_DEVICE_NONE), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 617 | if (isStateInCall(state)) { |
| 618 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 619 | // force reevaluating accessibility routing when call starts |
| 620 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // 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] | 624 | if (state == AUDIO_MODE_RINGTONE && |
| 625 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 626 | mLimitRingtoneVolume = true; |
| 627 | } else { |
| 628 | mLimitRingtoneVolume = false; |
| 629 | } |
| 630 | } |
| 631 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 632 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 633 | return mEngine->getPhoneState(); |
| 634 | } |
| 635 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 636 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 637 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 638 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 639 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 640 | if (config == mEngine->getForceUse(usage)) { |
| 641 | return; |
| 642 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 643 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 644 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 645 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 646 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 647 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 648 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 649 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 650 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 651 | |
| 652 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 653 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 654 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 655 | //FIXME: workaround for truncated touch sounds |
| 656 | // to be removed when the problem is handled by system UI |
| 657 | uint32_t delayMs = 0; |
| 658 | uint32_t waitMs = 0; |
| 659 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 660 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 661 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 662 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 663 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 664 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 665 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 667 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 668 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 669 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 670 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 671 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 672 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 673 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 674 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 678 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 679 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 680 | // 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] | 681 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 682 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 683 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 684 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 685 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 686 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 687 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 690 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 691 | { |
| 692 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 693 | } |
| 694 | |
| 695 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 696 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 697 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 698 | audio_devices_t device, |
| 699 | uint32_t samplingRate, |
| 700 | audio_format_t format, |
| 701 | audio_channel_mask_t channelMask, |
| 702 | audio_output_flags_t flags) |
| 703 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 704 | // only retain flags that will drive the direct output profile selection |
| 705 | // if explicitly requested |
| 706 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 707 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 708 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 709 | flags = |
| 710 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 711 | |
| 712 | sp<IOProfile> profile; |
| 713 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 714 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 715 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 716 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 717 | samplingRate, NULL /*updatedSamplingRate*/, |
| 718 | format, NULL /*updatedFormat*/, |
| 719 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 720 | flags)) { |
| 721 | continue; |
| 722 | } |
| 723 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 724 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 725 | continue; |
| 726 | } |
| 727 | // if several profiles are compatible, give priority to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 728 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 729 | continue; |
| 730 | } |
| 731 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 732 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 733 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 734 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 737 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 740 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 741 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 742 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 743 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 744 | |
| 745 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 746 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 747 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 748 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 749 | // and AudioSystem::getOutputSamplingRate(). |
| 750 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 751 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 752 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 753 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 754 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 755 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 758 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 759 | audio_io_handle_t *output, |
| 760 | audio_session_t session, |
| 761 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 762 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 763 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 764 | audio_output_flags_t *flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 765 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 766 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 767 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 768 | audio_attributes_t attributes; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 769 | DeviceVector outputDevices; |
| 770 | routing_strategy strategy; |
| 771 | audio_devices_t device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 772 | const audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 773 | audio_devices_t msdDevice = getMsdAudioOutDeviceTypes(); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 774 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 775 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 776 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 777 | return INVALID_OPERATION; |
| 778 | } |
| 779 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 780 | if (attr != NULL) { |
| 781 | if (!isValidAttributes(attr)) { |
| 782 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 783 | attr->usage, attr->content_type, attr->flags, |
| 784 | attr->tags); |
| 785 | return BAD_VALUE; |
| 786 | } |
| 787 | attributes = *attr; |
| 788 | } else { |
| 789 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 790 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 791 | return BAD_VALUE; |
| 792 | } |
| 793 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 794 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 795 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 796 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 797 | " session %d selectedDeviceId %d", |
| 798 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 799 | session, requestedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 800 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 801 | *stream = streamTypefromAttributesInt(&attributes); |
| 802 | |
| 803 | strategy = getStrategyForAttr(&attributes); |
| 804 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 805 | // First check for explicit routing (eg. setPreferredDevice) |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 806 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 807 | sp<DeviceDescriptor> deviceDesc = |
| 808 | mAvailableOutputDevices.getDeviceFromId(requestedDeviceId); |
| 809 | device = deviceDesc->type(); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 810 | } else { |
| 811 | // If no explict route, is there a matching dynamic policy that applies? |
| 812 | sp<SwAudioOutputDescriptor> desc; |
| 813 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
| 814 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 815 | if (!audio_has_proportional_frames(config->format)) { |
| 816 | return BAD_VALUE; |
| 817 | } |
| 818 | *stream = streamTypefromAttributesInt(&attributes); |
| 819 | *output = desc->mIoHandle; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 820 | AudioMix *mix = desc->mPolicyMix; |
| 821 | sp<DeviceDescriptor> deviceDesc = |
| 822 | mAvailableOutputDevices.getDevice(mix->mDeviceType, mix->mDeviceAddress); |
| 823 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 824 | ALOGV("getOutputForAttr() returns output %d", *output); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 825 | goto exit; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | // Virtual sources must always be dynamicaly or explicitly routed |
| 829 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 830 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 831 | return BAD_VALUE; |
| 832 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 833 | device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 834 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 835 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 836 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 837 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 840 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 841 | // chosen by the engine if not. |
| 842 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 843 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 844 | // 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] | 845 | if (device == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 846 | (*stream == AUDIO_STREAM_MUSIC || attributes.usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 847 | audio_is_linear_pcm(config->format) && |
| 848 | isInCall()) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 849 | if (requestedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 850 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 851 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 852 | // 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] | 853 | device = mEngine->getDeviceForStrategy(strategy); |
| 854 | } |
| 855 | } |
| 856 | |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 857 | ALOGV("getOutputForAttr() device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
| 858 | "flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 859 | device, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 860 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 861 | *output = AUDIO_IO_HANDLE_NONE; |
| 862 | if (msdDevice != AUDIO_DEVICE_NONE) { |
| 863 | *output = getOutputForDevice(msdDevice, session, *stream, config, flags); |
| 864 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(device) == NO_ERROR) { |
| 865 | ALOGV("%s() Using MSD device 0x%x instead of device 0x%x", |
| 866 | __func__, msdDevice, device); |
| 867 | device = msdDevice; |
| 868 | } else { |
| 869 | *output = AUDIO_IO_HANDLE_NONE; |
| 870 | } |
| 871 | } |
| 872 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 873 | *output = getOutputForDevice(device, session, *stream, config, flags); |
| 874 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 875 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 876 | return INVALID_OPERATION; |
| 877 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 878 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 879 | outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 880 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 881 | : AUDIO_PORT_HANDLE_NONE; |
| 882 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 883 | exit: |
| 884 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 885 | .format = config->format, |
| 886 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 887 | *portId = AudioPort::getNextUniqueId(); |
| 888 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 889 | sp<TrackClientDescriptor> clientDesc = |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 890 | new TrackClientDescriptor(*portId, uid, session, attributes, clientConfig, |
| 891 | requestedDeviceId, *stream, |
| 892 | getStrategyForAttr(&attributes), |
| 893 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 894 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 895 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 896 | |
| 897 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d for port ID %d", |
| 898 | *output, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 899 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 900 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 904 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 905 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 906 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 907 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 908 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 909 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 910 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 911 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 912 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 913 | // open a direct output if required by specified parameters |
| 914 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 915 | // and all common behaviors are driven by checking only the direct flag |
| 916 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 917 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 918 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 919 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 920 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 921 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 922 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 923 | // only allow deep buffering for music stream type |
| 924 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 925 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 926 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 927 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 928 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 929 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 930 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 931 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 932 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 933 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 934 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 935 | audio_is_linear_pcm(config->format) && |
| 936 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 937 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 938 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 939 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 940 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 942 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 943 | sp<IOProfile> profile; |
| 944 | |
| 945 | // 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] | 946 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 947 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 948 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 949 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 950 | goto non_direct_output; |
| 951 | } |
| 952 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 953 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 954 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 955 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 957 | // This may prevent offloading in rare situations where effects are left active by apps |
| 958 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 959 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 960 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 961 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | profile = getProfileForDirectOutput(device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 963 | config->sample_rate, |
| 964 | config->format, |
| 965 | config->channel_mask, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 966 | (audio_output_flags_t)*flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 969 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 970 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 971 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 972 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 973 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 974 | // reuse direct output if currently open by the same client |
| 975 | // and configured with same parameters |
| 976 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 977 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 978 | (config->channel_mask == desc->mChannelMask) && |
| 979 | (session == desc->mDirectClientSession)) { |
| 980 | desc->mDirectOpenCount++; |
| 981 | ALOGI("getOutputForDevice() reusing direct output %d for session %d", |
| 982 | mOutputs.keyAt(i), session); |
| 983 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 987 | |
| 988 | if (!profile->canOpenNewIo()) { |
| 989 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 990 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 991 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 992 | sp<SwAudioOutputDescriptor> outputDesc = |
| 993 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 994 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 995 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 996 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 997 | : String8(""); |
| 998 | |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 999 | // MSD patch may be using the only output stream that can service this request. Release |
| 1000 | // MSD patch to prioritize this request over any active output on MSD. |
| 1001 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 1002 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1003 | const auto& patch = msdPatches[i]; |
| 1004 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1005 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1006 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 1007 | (sink->ext.device.type & device) != AUDIO_DEVICE_NONE && |
| 1008 | (address.isEmpty() || strncmp(sink->ext.device.address, address.string(), |
| 1009 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 1010 | releaseAudioPatch(patch->mHandle, mUidCached); |
| 1011 | break; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1016 | status = outputDesc->open(config, device, address, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | |
| 1018 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1019 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1020 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1021 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1022 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
| 1023 | ALOGV("getOutputForDevice() failed opening direct output: output %d sample rate %d %d," |
| 1024 | "format %d %d, channel mask %04x %04x", output, config->sample_rate, |
| 1025 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1026 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1027 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1028 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1029 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1030 | // fall back to mixer output if possible when the direct output could not be open |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1031 | if (audio_is_linear_pcm(config->format) && |
| 1032 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1033 | goto non_direct_output; |
| 1034 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1035 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1036 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1037 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1038 | outputDesc->mDirectClientSession = session; |
| 1039 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1040 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1042 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1043 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | return output; |
| 1045 | } |
| 1046 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1047 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1048 | |
| 1049 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1050 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1051 | if ((*flags & (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ)) != 0) { |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1052 | return AUDIO_IO_HANDLE_NONE; |
| 1053 | } |
| 1054 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1055 | // ignoring channel mask due to downmix capability in mixer |
| 1056 | |
| 1057 | // open a non direct output |
| 1058 | |
| 1059 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1060 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1061 | // get which output is suitable for the specified stream. The actual |
| 1062 | // routing change will happen when startOutput() will be called |
| 1063 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1064 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1065 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1066 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1067 | output = selectOutput(outputs, *flags, config->format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1069 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1070 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1071 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1072 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | return output; |
| 1074 | } |
| 1075 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1076 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1077 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1078 | if (msdModule != 0) { |
| 1079 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1080 | msdModule->getHandle()); |
| 1081 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1082 | } |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | audio_devices_t AudioPolicyManager::getMsdAudioOutDeviceTypes() const { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1087 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1088 | if (msdModule != 0) { |
| 1089 | return mAvailableOutputDevices.getDeviceTypesFromHwModule(msdModule->getHandle()); |
| 1090 | } |
| 1091 | return AUDIO_DEVICE_NONE; |
| 1092 | } |
| 1093 | |
| 1094 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1095 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1096 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1097 | if (msdModule != 0) { |
| 1098 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1099 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1100 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1101 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1102 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1103 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1104 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1105 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | return msdPatches; |
| 1110 | } |
| 1111 | |
| 1112 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1113 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1114 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1115 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1116 | if (msdModule == nullptr) { |
| 1117 | ALOGE("%s() unable to get MSD module", __func__); |
| 1118 | return NO_INIT; |
| 1119 | } |
| 1120 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1121 | if (deviceModule == nullptr) { |
| 1122 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1123 | return NO_INIT; |
| 1124 | } |
| 1125 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1126 | if (inputProfiles.isEmpty()) { |
| 1127 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1128 | return NO_INIT; |
| 1129 | } |
| 1130 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1131 | if (outputProfiles.isEmpty()) { |
| 1132 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1133 | return NO_INIT; |
| 1134 | } |
| 1135 | AudioProfileVector msdProfiles; |
| 1136 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1137 | for (const auto &inProfile : inputProfiles) { |
| 1138 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1139 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1140 | } |
| 1141 | } |
| 1142 | AudioProfileVector deviceProfiles; |
| 1143 | for (const auto &outProfile : outputProfiles) { |
| 1144 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1145 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1146 | } |
| 1147 | } |
| 1148 | struct audio_config_base bestSinkConfig; |
| 1149 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1150 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1151 | &bestSinkConfig); |
| 1152 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1153 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1154 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1155 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1156 | } |
| 1157 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1158 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1159 | sinkConfig->format = bestSinkConfig.format; |
| 1160 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1161 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1162 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1163 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1164 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1165 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1166 | sourceConfig->format = bestSinkConfig.format; |
| 1167 | // Copy input stream directly without any processing (e.g. resampling). |
| 1168 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1169 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1170 | if (hwAvSync) { |
| 1171 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1172 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1173 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1174 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1175 | } |
| 1176 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1177 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1178 | sinkConfig->config_mask |= config_mask; |
| 1179 | sourceConfig->config_mask |= config_mask; |
| 1180 | return NO_ERROR; |
| 1181 | } |
| 1182 | |
| 1183 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1184 | { |
| 1185 | PatchBuilder patchBuilder; |
| 1186 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1187 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1188 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1189 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1190 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1191 | // For now, we just forcefully try with HwAvSync first. |
| 1192 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1193 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1194 | getBestMsdAudioProfileFor( |
| 1195 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1196 | if (res == NO_ERROR) { |
| 1197 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1198 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1199 | } |
| 1200 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1201 | " supporting PCM format conversion.", __func__); |
| 1202 | return patchBuilder; |
| 1203 | } |
| 1204 | |
| 1205 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1206 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1207 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1208 | // Use media strategy for unspecified output device. This should only |
| 1209 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1210 | // therefore invalidate explicit routing requests. |
| 1211 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1212 | } |
| 1213 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1214 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1215 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1216 | if (!msdPatches.isEmpty()) { |
| 1217 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1218 | "The current MSD prototype only supports one output patch"); |
| 1219 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1220 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1221 | return NO_ERROR; |
| 1222 | } |
| 1223 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1224 | } |
| 1225 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1226 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1227 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1228 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1229 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1230 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1231 | return status; |
| 1232 | } |
| 1233 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1234 | audio_io_handle_t AudioPolicyManager::selectOutput(const SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1235 | audio_output_flags_t flags, |
| 1236 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1237 | { |
| 1238 | // select one output among several that provide a path to a particular device or set of |
| 1239 | // devices (the list was previously build by getOutputsForDevice()). |
| 1240 | // The priority is as follows: |
| 1241 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1242 | // 2: the output with the bit depth the closest to the requested one |
| 1243 | // 3: the primary output |
| 1244 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1245 | |
| 1246 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1247 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1248 | } |
| 1249 | if (outputs.size() == 1) { |
| 1250 | return outputs[0]; |
| 1251 | } |
| 1252 | |
| 1253 | int maxCommonFlags = 0; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1254 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1255 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1256 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1257 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1258 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1259 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1260 | for (audio_io_handle_t output : outputs) { |
| 1261 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1262 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1263 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1264 | continue; |
| 1265 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1266 | // if a valid format is specified, skip output if not compatible |
| 1267 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1268 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1269 | continue; |
| 1270 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1271 | if (AudioPort::isBetterFormatMatch( |
| 1272 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1273 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1274 | bestFormat = outputDesc->mFormat; |
| 1275 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1278 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1279 | if (commonFlags >= maxCommonFlags) { |
| 1280 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1281 | if (format != AUDIO_FORMAT_INVALID |
| 1282 | && AudioPort::isBetterFormatMatch( |
| 1283 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1284 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1285 | bestFormatForFlags = outputDesc->mFormat; |
| 1286 | } |
| 1287 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1288 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1289 | maxCommonFlags = commonFlags; |
| 1290 | bestFormatForFlags = outputDesc->mFormat; |
| 1291 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1292 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1293 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1294 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1295 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1300 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1301 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1302 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1303 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1304 | return outputForFormat; |
| 1305 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1306 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1307 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | return outputs[0]; |
| 1311 | } |
| 1312 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1313 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1314 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1315 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1316 | |
| 1317 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1318 | if (outputDesc == 0) { |
| 1319 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1320 | return BAD_VALUE; |
| 1321 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1322 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1323 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1324 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1325 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1326 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1327 | status_t status = outputDesc->start(); |
| 1328 | if (status != NO_ERROR) { |
| 1329 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1332 | uint32_t delayMs; |
| 1333 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1334 | |
| 1335 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1336 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1337 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1338 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1339 | if (delayMs != 0) { |
| 1340 | usleep(delayMs * 1000); |
| 1341 | } |
| 1342 | |
| 1343 | return status; |
| 1344 | } |
| 1345 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1346 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1347 | const sp<TrackClientDescriptor>& client, |
| 1348 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1349 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1350 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1351 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1352 | |
| 1353 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1354 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1355 | if (stream == AUDIO_STREAM_TTS) { |
| 1356 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1357 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1358 | return INVALID_OPERATION; |
| 1359 | } else { |
| 1360 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1361 | } |
| 1362 | } else { |
| 1363 | // some playback other than beacon starts |
| 1364 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1365 | } |
| 1366 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1367 | // 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] | 1368 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1369 | bool force = !outputDesc->isActive() && |
| 1370 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1371 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1372 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 1373 | AudioMix *policyMix = NULL; |
| 1374 | const char *address = NULL; |
| 1375 | if (outputDesc->mPolicyMix != NULL) { |
| 1376 | policyMix = outputDesc->mPolicyMix; |
| 1377 | address = policyMix->mDeviceAddress.string(); |
| 1378 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1379 | device = policyMix->mDeviceType; |
| 1380 | } else { |
| 1381 | device = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1382 | } |
| 1383 | } |
| 1384 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1385 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1386 | // It covers a common case when there is no materially active audio |
| 1387 | // and muting would result in unnecessary delay and dropped audio. |
| 1388 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1389 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1390 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1391 | // increment usage count for this stream on the requested output: |
| 1392 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1393 | // 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] | 1394 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1395 | |
| 1396 | if (client->hasPreferredDevice(true)) { |
| 1397 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 1398 | if (device != outputDesc->device()) { |
| 1399 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1400 | } |
| 1401 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1402 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1403 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1404 | selectOutputForMusicEffects(); |
| 1405 | } |
| 1406 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1407 | if (outputDesc->streamActiveCount(stream) == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1408 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1409 | if (device == AUDIO_DEVICE_NONE) { |
| 1410 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1411 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1412 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1413 | routing_strategy strategy = getStrategy(stream); |
| 1414 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1415 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1416 | (beaconMuteLatency > 0); |
| 1417 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1418 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1419 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1420 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1421 | // An output has a shared device if |
| 1422 | // - managed by the same hw module |
| 1423 | // - supports the currently selected device |
| 1424 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1425 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1426 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1427 | // force a device change if any other output is: |
| 1428 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1429 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1430 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1431 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1432 | // 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] | 1433 | // change the device currently selected by the other output. |
| 1434 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1435 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1436 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1437 | force = true; |
| 1438 | } |
| 1439 | // 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] | 1440 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1441 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1442 | const uint32_t latencyMs = desc->latency(); |
| 1443 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1444 | |
| 1445 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1446 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1447 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1448 | |
| 1449 | // Require mute check if another output is on a shared device |
| 1450 | // and currently active to have proper drain and avoid pops. |
| 1451 | // Note restoring AudioTracks onto this output needs to invoke |
| 1452 | // a volume ramp if there is no mute. |
| 1453 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1454 | } |
| 1455 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1456 | |
| 1457 | const uint32_t muteWaitMs = |
| 1458 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1459 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1460 | // apply volume rules for current stream and device if necessary |
| 1461 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1462 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1463 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1464 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1465 | |
| 1466 | // update the outputs if starting an output with a stream that can affect notification |
| 1467 | // routing |
| 1468 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1469 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1470 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1471 | if (strategy == STRATEGY_SONIFICATION) { |
| 1472 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1473 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1474 | |
| 1475 | if (waitMs > muteWaitMs) { |
| 1476 | *delayMs = waitMs - muteWaitMs; |
| 1477 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1478 | |
| 1479 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1480 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1481 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1482 | // change occurs after the MixerThread starts and causes a stream volume |
| 1483 | // glitch. |
| 1484 | // |
| 1485 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1486 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1487 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1488 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1489 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1490 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1491 | } |
| 1492 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1493 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1494 | // of type MIX_TYPE_RECORDERS |
| 1495 | if (audio_is_remote_submix_device(device) && policyMix != NULL && |
| 1496 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1497 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1498 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1499 | address, |
| 1500 | "remote-submix"); |
| 1501 | } |
| 1502 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1503 | return NO_ERROR; |
| 1504 | } |
| 1505 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1506 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1507 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1508 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1509 | |
| 1510 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1511 | if (outputDesc == 0) { |
| 1512 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1513 | return BAD_VALUE; |
| 1514 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1515 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1516 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1517 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1518 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1519 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1520 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1521 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1522 | if (status == NO_ERROR ) { |
| 1523 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1524 | } |
| 1525 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1528 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1529 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1530 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1531 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1532 | audio_stream_type_t stream = client->stream(); |
| 1533 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1534 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1535 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1536 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1537 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1538 | // Automatically disable the remote submix input when output is stopped on a |
| 1539 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1540 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1541 | outputDesc->mPolicyMix != NULL && |
| 1542 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1543 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1544 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1545 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1546 | "remote-submix"); |
| 1547 | } |
| 1548 | } |
| 1549 | bool forceDeviceUpdate = false; |
| 1550 | if (client->hasPreferredDevice(true)) { |
| 1551 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1552 | forceDeviceUpdate = true; |
| 1553 | } |
| 1554 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1555 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1556 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1557 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1558 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1559 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1560 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1561 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1562 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1563 | // the track stop() command is received and at that time the audio track buffer can |
| 1564 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1565 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1566 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1567 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1568 | |
| 1569 | // force restoring the device selection on other active outputs if it differs from the |
| 1570 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1571 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1572 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1573 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1574 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1575 | desc->isActive() && |
| 1576 | outputDesc->sharesHwModuleWith(desc) && |
| 1577 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1578 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1579 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1580 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1581 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1582 | newDevice2, |
| 1583 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1584 | delayMs); |
| 1585 | // re-apply device specific volume if not done by setOutputDevice() |
| 1586 | if (!force) { |
| 1587 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1588 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1589 | } |
| 1590 | } |
| 1591 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1592 | handleNotificationRoutingForStream(stream); |
| 1593 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1594 | |
| 1595 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1596 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1597 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1598 | } |
| 1599 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1600 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1601 | selectOutputForMusicEffects(); |
| 1602 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1603 | return NO_ERROR; |
| 1604 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1605 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1606 | return INVALID_OPERATION; |
| 1607 | } |
| 1608 | } |
| 1609 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1610 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1611 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1612 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1613 | |
| 1614 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1615 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1616 | // If an output descriptor is closed due to a device routing change, |
| 1617 | // then there are race conditions with releaseOutput from tracks |
| 1618 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1619 | // destroyed shortly thereafter. |
| 1620 | // |
| 1621 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1622 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1623 | return; |
| 1624 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1625 | |
| 1626 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1627 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1628 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1629 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1630 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1631 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1632 | return; |
| 1633 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1634 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1635 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1636 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1637 | } |
| 1638 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1639 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1640 | // otherwise there may be inaccurate stream reference counts. |
| 1641 | // This is checked in outputDesc->removeClient below. |
| 1642 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1645 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1646 | audio_io_handle_t *input, |
| 1647 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1648 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1649 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1650 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1651 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1652 | input_type_t *inputType, |
| 1653 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1654 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1655 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1656 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1657 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1658 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1659 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1660 | // handle legacy remote submix case where the address was not always specified |
| 1661 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1662 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1663 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1664 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1665 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1666 | sp<AudioInputDescriptor> inputDesc; |
| 1667 | sp<RecordClientDescriptor> clientDesc; |
| 1668 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1669 | bool isSoundTrigger; |
| 1670 | audio_devices_t device; |
| 1671 | |
| 1672 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1673 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1674 | return INVALID_OPERATION; |
| 1675 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1676 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1677 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1678 | inputSource = AUDIO_SOURCE_MIC; |
| 1679 | } |
| 1680 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1681 | // Explicit routing? |
| 1682 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1683 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1684 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1685 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1686 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1687 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1688 | // possible |
| 1689 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1690 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1691 | ssize_t index = mInputs.indexOfKey(*input); |
| 1692 | if (index < 0) { |
| 1693 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1694 | status = BAD_VALUE; |
| 1695 | goto error; |
| 1696 | } |
| 1697 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1698 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1699 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1700 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1701 | status = BAD_VALUE; |
| 1702 | goto error; |
| 1703 | } |
| 1704 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1705 | // 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] | 1706 | // corresponds to a new client and is only permitted from the same UID. |
| 1707 | // 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] | 1708 | if (clients.size() > 1) { |
| 1709 | for (const auto& client : clients) { |
| 1710 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1711 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1712 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1713 | // to an actual client |
| 1714 | if (client == *clients.cbegin()) { |
| 1715 | continue; |
| 1716 | } |
| 1717 | if (uid != client->uid() && !client->isSilenced()) { |
| 1718 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1719 | uid, client->portId(), client->uid()); |
| 1720 | status = INVALID_OPERATION; |
| 1721 | goto error; |
| 1722 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1723 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1724 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1725 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1726 | device = inputDesc->mDevice; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1727 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1728 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1729 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | *input = AUDIO_IO_HANDLE_NONE; |
| 1733 | *inputType = API_INPUT_INVALID; |
| 1734 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1735 | halInputSource = inputSource; |
| 1736 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1737 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1738 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1739 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1740 | if (status != NO_ERROR) { |
| 1741 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1742 | } |
| 1743 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1744 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1745 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1746 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1747 | if (deviceDesc != 0) { |
| 1748 | device = deviceDesc->type(); |
| 1749 | } else { |
| 1750 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
| 1751 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1752 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1753 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1754 | status = BAD_VALUE; |
| 1755 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1756 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1757 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1758 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1759 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1760 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1761 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1762 | // know about it and is therefore considered "legacy" |
| 1763 | *inputType = API_INPUT_LEGACY; |
| 1764 | } else { |
| 1765 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1766 | // an external policy |
| 1767 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1768 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1769 | } else if (audio_is_remote_submix_device(device)) { |
| 1770 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1771 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1772 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1773 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1774 | } else { |
| 1775 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1776 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1777 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1778 | } |
| 1779 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1780 | *input = getInputForDevice(device, address, session, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1781 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1782 | policyMix); |
| 1783 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1784 | status = INVALID_OPERATION; |
| 1785 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1786 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1787 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1788 | exit: |
| 1789 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1790 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1791 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1792 | : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1793 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1794 | isSoundTrigger = inputSource == AUDIO_SOURCE_HOTWORD && |
| 1795 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1796 | *portId = AudioPort::getNextUniqueId(); |
| 1797 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1798 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1799 | *attr, *config, requestedDeviceId, |
| 1800 | inputSource,flags, isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1801 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1802 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1803 | |
| 1804 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1805 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1806 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1807 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1808 | |
| 1809 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1810 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | |
| 1814 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1815 | String8 address, |
| 1816 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1817 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1818 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1819 | audio_input_flags_t flags, |
| 1820 | AudioMix *policyMix) |
| 1821 | { |
| 1822 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1823 | audio_source_t halInputSource = inputSource; |
| 1824 | bool isSoundTrigger = false; |
| 1825 | |
| 1826 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1827 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1828 | if (index >= 0) { |
| 1829 | input = mSoundTriggerSessions.valueFor(session); |
| 1830 | isSoundTrigger = true; |
| 1831 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1832 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1833 | } else { |
| 1834 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1835 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1836 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1837 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1838 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1839 | } |
| 1840 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1841 | // find a compatible input profile (not necessarily identical in parameters) |
| 1842 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1843 | // sampling rate and flags may be updated by getInputProfile |
| 1844 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1845 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1846 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1847 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1848 | audio_input_flags_t profileFlags = flags; |
| 1849 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1850 | 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] | 1851 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1852 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1853 | profileFlags); |
| 1854 | if (profile != 0) { |
| 1855 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1856 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1857 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1858 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1859 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1860 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1861 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1862 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1863 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1864 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1865 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1866 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1867 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1868 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1869 | if (samplingRate == 0) { |
| 1870 | samplingRate = profileSamplingRate; |
| 1871 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1872 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1873 | if (profile->getModuleHandle() == 0) { |
| 1874 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1875 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1878 | if (!profile->canOpenNewIo()) { |
| 1879 | return AUDIO_IO_HANDLE_NONE; |
| 1880 | } |
| 1881 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1882 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1883 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1884 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1885 | lConfig.sample_rate = profileSamplingRate; |
| 1886 | lConfig.channel_mask = profileChannelMask; |
| 1887 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1888 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1889 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1890 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1891 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1892 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1893 | } |
| 1894 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1895 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1896 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1897 | |
| 1898 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1899 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1900 | (profileSamplingRate != lConfig.sample_rate) || |
| 1901 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1902 | (profileChannelMask != lConfig.channel_mask)) { |
| 1903 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1904 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1905 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1906 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1907 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1908 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1909 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1910 | } |
| 1911 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1912 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1913 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1914 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1915 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1916 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1917 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1920 | //static |
| 1921 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1922 | { |
| 1923 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1924 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1925 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1926 | } |
| 1927 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1928 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1929 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1930 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1931 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1932 | unsigned int numModules = 0; |
| 1933 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1934 | |
| 1935 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1936 | if (status == NO_ERROR && numModules != 0) { |
| 1937 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1938 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1939 | if (nModules == NULL) { |
| 1940 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1941 | // ram the next time this function is called. |
| 1942 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1943 | return false; |
| 1944 | } |
| 1945 | |
| 1946 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1947 | if (status == NO_ERROR) { |
| 1948 | soundTriggerSupportsConcurrentCapture = true; |
| 1949 | for (size_t i = 0; i < numModules; ++i) { |
| 1950 | soundTriggerSupportsConcurrentCapture &= |
| 1951 | nModules[i].properties.concurrent_capture; |
| 1952 | } |
| 1953 | } |
| 1954 | free(nModules); |
| 1955 | } |
| 1956 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1957 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1958 | } |
| 1959 | return mSoundTriggerSupportsConcurrentCapture; |
| 1960 | } |
| 1961 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1962 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1963 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1964 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1965 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1966 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1967 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
| 1968 | |
| 1969 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1970 | |
| 1971 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 1972 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1973 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1974 | return BAD_VALUE; |
| 1975 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1976 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1977 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1978 | if (client->active()) { |
| 1979 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 1980 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1983 | audio_session_t session = client->session(); |
| 1984 | |
| 1985 | ALOGV("%s input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 1986 | __FUNCTION__, input, session, silenced, *concurrency); |
| 1987 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1988 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1989 | if (mCallTxPatch != 0 && |
| 1990 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1991 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 1992 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1993 | return INVALID_OPERATION; |
| 1994 | } |
| 1995 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1996 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1997 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1998 | // If a UID is idle and records silence and another not silenced recording starts |
| 1999 | // from another UID (idle or active) we stop the current idle UID recording in |
| 2000 | // favor of the new one - "There can be only one" TM |
| 2001 | if (!silenced) { |
| 2002 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2003 | if ((activeDesc->getAudioPort()->getFlags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2004 | activeDesc->getId() == inputDesc->getId()) { |
| 2005 | continue; |
| 2006 | } |
| 2007 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2008 | RecordClientVector activeClients = activeDesc->clientsList(true /*activeOnly*/); |
| 2009 | for (const auto& activeClient : activeClients) { |
| 2010 | if (activeClient->isSilenced()) { |
| 2011 | closeClient(activeClient->portId()); |
| 2012 | ALOGV("%s client %d stopping silenced client %d", __FUNCTION__, |
| 2013 | portId, activeClient->portId()); |
| 2014 | activeInputs = mInputs.getActiveInputs(); |
| 2015 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2016 | } |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2021 | if ((client->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 2022 | activeDesc->getId() == inputDesc->getId()) { |
| 2023 | continue; |
| 2024 | } |
| 2025 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2026 | audio_source_t activeSource = activeDesc->inputSource(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2027 | if (client->source() == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2028 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2029 | if (activeDesc->hasPreemptedSession(session)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2030 | ALOGW("%s input %d failed for HOTWORD: " |
| 2031 | "other input %d already started for HOTWORD", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2032 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2033 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2034 | return INVALID_OPERATION; |
| 2035 | } |
| 2036 | } else { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2037 | ALOGV("%s input %d failed for HOTWORD: other input %d already started", |
| 2038 | __FUNCTION__, input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2039 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2040 | return INVALID_OPERATION; |
| 2041 | } |
| 2042 | } else { |
| 2043 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2044 | ALOGW("%s input %d failed: other input %d already started", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2045 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2046 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2047 | return INVALID_OPERATION; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2052 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 2053 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 2054 | // that's running. |
| 2055 | const bool allowConcurrentWithSoundTrigger = |
| 2056 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 2057 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2058 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2059 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2060 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 2061 | continue; |
| 2062 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2063 | RecordClientVector activeHotwordClients = |
| 2064 | activeDesc->clientsList(true, AUDIO_SOURCE_HOTWORD); |
| 2065 | if (activeHotwordClients.size() > 0) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2066 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2067 | |
| 2068 | for (const auto& activeClient : activeHotwordClients) { |
| 2069 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
| 2070 | sessions.add(activeClient->session()); |
| 2071 | closeClient(activeClient->portId()); |
| 2072 | ALOGV("%s input %d for HOTWORD preempting HOTWORD input %d", __FUNCTION__, |
| 2073 | input, activeDesc->mIoHandle); |
| 2074 | } |
| 2075 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2076 | inputDesc->setPreemptedSessions(sessions); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2077 | } |
| 2078 | } |
| 2079 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2080 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2081 | // Make sure we start with the correct silence state |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2082 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2083 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2084 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2085 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2086 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2087 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2088 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2089 | // primary HW module |
| 2090 | audio_devices_t device = getNewInputDevice(inputDesc); |
| 2091 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2092 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2093 | status_t status = inputDesc->start(); |
| 2094 | if (status != NO_ERROR) { |
| 2095 | inputDesc->setClientActive(client, false); |
| 2096 | return status; |
| 2097 | } |
| 2098 | |
| 2099 | if (inputDesc->activeCount() == 1) { |
| 2100 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2101 | if ((inputDesc->mPolicyMix != NULL) |
| 2102 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2103 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2104 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2105 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2106 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2107 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2108 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2109 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2110 | SoundTrigger::setCaptureState(true); |
| 2111 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2112 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2113 | // automatically enable the remote submix output when input is started if not |
| 2114 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2115 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2116 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2117 | String8 address = String8(""); |
| 2118 | if (inputDesc->mPolicyMix == NULL) { |
| 2119 | address = String8("0"); |
| 2120 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2121 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2122 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2123 | if (address != "") { |
| 2124 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2125 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2126 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2127 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2128 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2131 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2132 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2133 | return NO_ERROR; |
| 2134 | } |
| 2135 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2136 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2138 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2139 | |
| 2140 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2141 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2142 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2143 | return BAD_VALUE; |
| 2144 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2145 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2146 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2147 | if (!client->active()) { |
| 2148 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2149 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2150 | } |
| 2151 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2152 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2153 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2154 | inputDesc->stop(); |
| 2155 | if (inputDesc->isActive()) { |
| 2156 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2157 | } else { |
| 2158 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2159 | if ((inputDesc->mPolicyMix != NULL) |
| 2160 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2161 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2162 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2163 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2164 | |
| 2165 | // automatically disable the remote submix output when input is stopped if not |
| 2166 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2167 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2168 | String8 address = String8(""); |
| 2169 | if (inputDesc->mPolicyMix == NULL) { |
| 2170 | address = String8("0"); |
| 2171 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2172 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2173 | } |
| 2174 | if (address != "") { |
| 2175 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2176 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2177 | address, "remote-submix"); |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | audio_devices_t device = inputDesc->mDevice; |
| 2182 | resetInputDevice(input); |
| 2183 | |
| 2184 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2185 | // primary HW module |
| 2186 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2187 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2188 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2189 | SoundTrigger::setCaptureState(false); |
| 2190 | } |
| 2191 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2192 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2193 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2196 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2197 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2198 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2199 | |
| 2200 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2201 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2202 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2203 | return; |
| 2204 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2205 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2206 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2207 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2208 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2209 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2210 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2211 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2212 | if (inputDesc->getClientCount() > 0) { |
| 2213 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2214 | return; |
| 2215 | } |
| 2216 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2217 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2218 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2219 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2220 | } |
| 2221 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2222 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2223 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2224 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2225 | |
| 2226 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2227 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2228 | } |
| 2229 | } |
| 2230 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2231 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2232 | { |
| 2233 | stopInput(portId); |
| 2234 | releaseInput(portId); |
| 2235 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2236 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2237 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2238 | bool patchRemoved = false; |
| 2239 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2240 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2241 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2242 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2243 | if (patch_index >= 0) { |
| 2244 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2245 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2246 | mAudioPatches.removeItemsAt(patch_index); |
| 2247 | patchRemoved = true; |
| 2248 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2249 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2250 | } |
| 2251 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2252 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2253 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2254 | |
| 2255 | if (patchRemoved) { |
| 2256 | mpClientInterface->onAudioPatchListUpdate(); |
| 2257 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2260 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2261 | int indexMin, |
| 2262 | int indexMax) |
| 2263 | { |
| 2264 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2265 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2266 | |
| 2267 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2268 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2269 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2270 | continue; |
| 2271 | } |
| 2272 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2273 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2276 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2277 | int index, |
| 2278 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2279 | { |
| 2280 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2281 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2282 | // app that has MODIFY_PHONE_STATE permission. |
| 2283 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2284 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2285 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2286 | return BAD_VALUE; |
| 2287 | } |
| 2288 | if (!audio_is_output_device(device)) { |
| 2289 | return BAD_VALUE; |
| 2290 | } |
| 2291 | |
| 2292 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2293 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2294 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2295 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2296 | stream, device, index); |
| 2297 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2298 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2299 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2300 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2301 | continue; |
| 2302 | } |
| 2303 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2304 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2305 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2306 | // update volume on all outputs and streams matching the following: |
| 2307 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2308 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2309 | // the requested device |
| 2310 | // - For non default requested device, currently selected device on the output is either the |
| 2311 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2312 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2313 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2314 | status_t status = NO_ERROR; |
| 2315 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2316 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2317 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2318 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2319 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2320 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2321 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2322 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2323 | continue; |
| 2324 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2325 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2326 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2327 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2328 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2329 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2330 | continue; |
| 2331 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2332 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2333 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2334 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2335 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2336 | } else { |
| 2337 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2338 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2339 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2340 | // rescale index before applying to curStream as ranges may be different for |
| 2341 | // stream and curStream |
| 2342 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2343 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2344 | //FIXME: workaround for truncated touch sounds |
| 2345 | // delayed volume change for system stream to be removed when the problem is |
| 2346 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2347 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2348 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2349 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2350 | if (volStatus != NO_ERROR) { |
| 2351 | status = volStatus; |
| 2352 | } |
| 2353 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2354 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2355 | } |
| 2356 | return status; |
| 2357 | } |
| 2358 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2359 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2360 | int *index, |
| 2361 | audio_devices_t device) |
| 2362 | { |
| 2363 | if (index == NULL) { |
| 2364 | return BAD_VALUE; |
| 2365 | } |
| 2366 | if (!audio_is_output_device(device)) { |
| 2367 | return BAD_VALUE; |
| 2368 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2369 | // 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] | 2370 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2371 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2372 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2373 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2374 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2375 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2376 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2377 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2378 | return NO_ERROR; |
| 2379 | } |
| 2380 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2381 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2382 | { |
| 2383 | // select one output among several suitable for global effects. |
| 2384 | // The priority is as follows: |
| 2385 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2386 | // AudioFlinger will invalidate the track and the offloaded output |
| 2387 | // will be closed causing the effect to be moved to a PCM output. |
| 2388 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2389 | // 3: The primary output |
| 2390 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2391 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2392 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2393 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2394 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2395 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2396 | if (outputs.size() == 0) { |
| 2397 | return AUDIO_IO_HANDLE_NONE; |
| 2398 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2399 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2400 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2401 | bool activeOnly = true; |
| 2402 | |
| 2403 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2404 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2405 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2406 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2407 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2408 | for (audio_io_handle_t output : outputs) { |
| 2409 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2410 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2411 | continue; |
| 2412 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2413 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2414 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2415 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2416 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2417 | } |
| 2418 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2419 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2420 | } |
| 2421 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2422 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2426 | output = outputOffloaded; |
| 2427 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2428 | output = outputDeepBuffer; |
| 2429 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2430 | output = outputPrimary; |
| 2431 | } else { |
| 2432 | output = outputs[0]; |
| 2433 | } |
| 2434 | activeOnly = false; |
| 2435 | } |
| 2436 | |
| 2437 | if (output != mMusicEffectOutput) { |
| 2438 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2439 | mMusicEffectOutput = output; |
| 2440 | } |
| 2441 | |
| 2442 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2443 | return output; |
| 2444 | } |
| 2445 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2446 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2447 | { |
| 2448 | return selectOutputForMusicEffects(); |
| 2449 | } |
| 2450 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2451 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2452 | audio_io_handle_t io, |
| 2453 | uint32_t strategy, |
| 2454 | int session, |
| 2455 | int id) |
| 2456 | { |
| 2457 | ssize_t index = mOutputs.indexOfKey(io); |
| 2458 | if (index < 0) { |
| 2459 | index = mInputs.indexOfKey(io); |
| 2460 | if (index < 0) { |
| 2461 | ALOGW("registerEffect() unknown io %d", io); |
| 2462 | return INVALID_OPERATION; |
| 2463 | } |
| 2464 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2465 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2468 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2469 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2470 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2471 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2472 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2473 | continue; |
| 2474 | } |
| 2475 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2476 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2477 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2481 | { |
| 2482 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2483 | } |
| 2484 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2485 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2486 | { |
| 2487 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2488 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2489 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2490 | return true; |
| 2491 | } |
| 2492 | } |
| 2493 | return false; |
| 2494 | } |
| 2495 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2496 | // Register a list of custom mixes with their attributes and format. |
| 2497 | // When a mix is registered, corresponding input and output profiles are |
| 2498 | // added to the remote submix hw module. The profile contains only the |
| 2499 | // parameters (sampling rate, format...) specified by the mix. |
| 2500 | // The corresponding input remote submix device is also connected. |
| 2501 | // |
| 2502 | // When a remote submix device is connected, the address is checked to select the |
| 2503 | // appropriate profile and the corresponding input or output stream is opened. |
| 2504 | // |
| 2505 | // When capture starts, getInputForAttr() will: |
| 2506 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2507 | // - 2 if none found, getDeviceForInputSource() will: |
| 2508 | // - 2.1 look for a mix matching the attributes source |
| 2509 | // - 2.2 if none found, default to device selection by policy rules |
| 2510 | // At this time, the corresponding output remote submix device is also connected |
| 2511 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2512 | // after AudioTracks are invalidated |
| 2513 | // |
| 2514 | // When playback starts, getOutputForAttr() will: |
| 2515 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2516 | // - 2 if none found, look for a mix matching the attributes usage |
| 2517 | // - 3 if none found, default to device and output selection by policy rules. |
| 2518 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2519 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2520 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2521 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2522 | status_t res = NO_ERROR; |
| 2523 | |
| 2524 | sp<HwModule> rSubmixModule; |
| 2525 | // examine each mix's route type |
| 2526 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2527 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2528 | // 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] | 2529 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2530 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2531 | break; |
| 2532 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2533 | 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] | 2534 | 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] | 2535 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2536 | rSubmixModule = mHwModules.getModuleFromName( |
| 2537 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2538 | if (rSubmixModule == 0) { |
| 2539 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2540 | i); |
| 2541 | res = INVALID_OPERATION; |
| 2542 | break; |
| 2543 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2544 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2545 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2546 | String8 address = mix.mDeviceAddress; |
| 2547 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2548 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2549 | } else { |
| 2550 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2551 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2552 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2553 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2554 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2555 | res = INVALID_OPERATION; |
| 2556 | break; |
| 2557 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2558 | audio_config_t outputConfig = mix.mFormat; |
| 2559 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2560 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2561 | // stereo and let audio flinger do the channel conversion if needed. |
| 2562 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2563 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2564 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2565 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2566 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2567 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2568 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2569 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2570 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2571 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2572 | address.string(), "remote-submix"); |
| 2573 | } else { |
| 2574 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2575 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2576 | address.string(), "remote-submix"); |
| 2577 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2578 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2579 | String8 address = mix.mDeviceAddress; |
| 2580 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2581 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2582 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2583 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2584 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2585 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2586 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2587 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2588 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2589 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2590 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2591 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2592 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2593 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2594 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2595 | } else { |
| 2596 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2597 | } |
| 2598 | break; |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | if (res != NO_ERROR) { |
| 2603 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2604 | i, device, address.string()); |
| 2605 | res = INVALID_OPERATION; |
| 2606 | break; |
| 2607 | } else if (!foundOutput) { |
| 2608 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2609 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2610 | res = INVALID_OPERATION; |
| 2611 | break; |
| 2612 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2613 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2614 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2615 | if (res != NO_ERROR) { |
| 2616 | unregisterPolicyMixes(mixes); |
| 2617 | } |
| 2618 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2622 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2623 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2624 | status_t res = NO_ERROR; |
| 2625 | sp<HwModule> rSubmixModule; |
| 2626 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2627 | for (const auto& mix : mixes) { |
| 2628 | 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] | 2629 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2630 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2631 | rSubmixModule = mHwModules.getModuleFromName( |
| 2632 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2633 | if (rSubmixModule == 0) { |
| 2634 | res = INVALID_OPERATION; |
| 2635 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2636 | } |
| 2637 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2638 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2639 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2640 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2641 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2642 | res = INVALID_OPERATION; |
| 2643 | continue; |
| 2644 | } |
| 2645 | |
| 2646 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2647 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2648 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2649 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2650 | address.string(), "remote-submix"); |
| 2651 | } |
| 2652 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2653 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2654 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2655 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2656 | address.string(), "remote-submix"); |
| 2657 | } |
| 2658 | rSubmixModule->removeOutputProfile(address); |
| 2659 | rSubmixModule->removeInputProfile(address); |
| 2660 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2661 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2662 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2663 | res = INVALID_OPERATION; |
| 2664 | continue; |
| 2665 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2666 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2667 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2668 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2669 | } |
| 2670 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2671 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2672 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2673 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2674 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2675 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2676 | std::string stateLiteral; |
| 2677 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2678 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2679 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2680 | "communications", "media", "record", "dock", "system", |
| 2681 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2682 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2683 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2684 | dst->appendFormat(" Force use for %s: %d\n", |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2685 | forceUses[i], mEngine->getForceUse(i)); |
| 2686 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2687 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2688 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2689 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2690 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2691 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2692 | mHwModulesAll.dump(dst); |
| 2693 | mOutputs.dump(dst); |
| 2694 | mInputs.dump(dst); |
| 2695 | mVolumeCurves->dump(dst); |
| 2696 | mEffects.dump(dst); |
| 2697 | mAudioPatches.dump(dst); |
| 2698 | mPolicyMixes.dump(dst); |
| 2699 | mAudioSources.dump(dst); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2700 | if (!mSurroundFormats.empty()) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2701 | dst->append("\nEnabled Surround Formats:\n"); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2702 | size_t i = 0; |
| 2703 | for (const auto& fmt : mSurroundFormats) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2704 | dst->append(i++ == 0 ? " " : ", "); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2705 | std::string sfmt; |
| 2706 | FormatConverter::toString(fmt, sfmt); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2707 | dst->append(sfmt.c_str()); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2708 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2709 | dst->append("\n"); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2710 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | status_t AudioPolicyManager::dump(int fd) |
| 2714 | { |
| 2715 | String8 result; |
| 2716 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2717 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2718 | return NO_ERROR; |
| 2719 | } |
| 2720 | |
| 2721 | // This function checks for the parameters which can be offloaded. |
| 2722 | // This can be enhanced depending on the capability of the DSP and policy |
| 2723 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2724 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2725 | { |
| 2726 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2727 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2728 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2729 | offloadInfo.format, |
| 2730 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2731 | offloadInfo.has_video); |
| 2732 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2733 | if (mMasterMono) { |
| 2734 | return false; // no offloading if mono is set. |
| 2735 | } |
| 2736 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2737 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2738 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2739 | ALOGV("offload disabled by audio.offload.disable"); |
| 2740 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2741 | } |
| 2742 | |
| 2743 | // Check if stream type is music, then only allow offload as of now. |
| 2744 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2745 | { |
| 2746 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2747 | return false; |
| 2748 | } |
| 2749 | |
| 2750 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2751 | const bool allowOffloadWithVideo = |
| 2752 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2753 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2754 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2755 | return false; |
| 2756 | } |
| 2757 | |
| 2758 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2759 | const int min_duration_secs = property_get_int32( |
| 2760 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2761 | if (min_duration_secs >= 0) { |
| 2762 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2763 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2764 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2765 | return false; |
| 2766 | } |
| 2767 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2768 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2769 | return false; |
| 2770 | } |
| 2771 | |
| 2772 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2773 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2774 | // detects there is an active non offloadable effect. |
| 2775 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2776 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2777 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2778 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2779 | return false; |
| 2780 | } |
| 2781 | |
| 2782 | // See if there is a profile to support this. |
| 2783 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2784 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2785 | offloadInfo.sample_rate, |
| 2786 | offloadInfo.format, |
| 2787 | offloadInfo.channel_mask, |
| 2788 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2789 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2790 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2793 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2794 | audio_port_type_t type, |
| 2795 | unsigned int *num_ports, |
| 2796 | struct audio_port *ports, |
| 2797 | unsigned int *generation) |
| 2798 | { |
| 2799 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2800 | generation == NULL) { |
| 2801 | return BAD_VALUE; |
| 2802 | } |
| 2803 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2804 | if (ports == NULL) { |
| 2805 | *num_ports = 0; |
| 2806 | } |
| 2807 | |
| 2808 | size_t portsWritten = 0; |
| 2809 | size_t portsMax = *num_ports; |
| 2810 | *num_ports = 0; |
| 2811 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2812 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2813 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2814 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2815 | for (const auto& dev : mAvailableOutputDevices) { |
| 2816 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2817 | continue; |
| 2818 | } |
| 2819 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2820 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2821 | } |
| 2822 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2823 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2824 | } |
| 2825 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2826 | for (const auto& dev : mAvailableInputDevices) { |
| 2827 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2828 | continue; |
| 2829 | } |
| 2830 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2831 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2832 | } |
| 2833 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2834 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2838 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2839 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2840 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2841 | } |
| 2842 | *num_ports += mInputs.size(); |
| 2843 | } |
| 2844 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2845 | size_t numOutputs = 0; |
| 2846 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2847 | if (!mOutputs[i]->isDuplicated()) { |
| 2848 | numOutputs++; |
| 2849 | if (portsWritten < portsMax) { |
| 2850 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2851 | } |
| 2852 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2853 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2854 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2858 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2859 | return NO_ERROR; |
| 2860 | } |
| 2861 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2862 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2863 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2864 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2865 | return BAD_VALUE; |
| 2866 | } |
| 2867 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2868 | if (dev != 0) { |
| 2869 | dev->toAudioPort(port); |
| 2870 | return NO_ERROR; |
| 2871 | } |
| 2872 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2873 | if (dev != 0) { |
| 2874 | dev->toAudioPort(port); |
| 2875 | return NO_ERROR; |
| 2876 | } |
| 2877 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2878 | if (out != 0) { |
| 2879 | out->toAudioPort(port); |
| 2880 | return NO_ERROR; |
| 2881 | } |
| 2882 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2883 | if (in != 0) { |
| 2884 | in->toAudioPort(port); |
| 2885 | return NO_ERROR; |
| 2886 | } |
| 2887 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2888 | } |
| 2889 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2890 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2891 | audio_patch_handle_t *handle, |
| 2892 | uid_t uid) |
| 2893 | { |
| 2894 | ALOGV("createAudioPatch()"); |
| 2895 | |
| 2896 | if (handle == NULL || patch == NULL) { |
| 2897 | return BAD_VALUE; |
| 2898 | } |
| 2899 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2900 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2901 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2902 | return BAD_VALUE; |
| 2903 | } |
| 2904 | // only one source per audio patch supported for now |
| 2905 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2906 | return INVALID_OPERATION; |
| 2907 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2908 | |
| 2909 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2910 | return INVALID_OPERATION; |
| 2911 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2912 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2913 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2914 | return INVALID_OPERATION; |
| 2915 | } |
| 2916 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2917 | |
| 2918 | sp<AudioPatch> patchDesc; |
| 2919 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2920 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2921 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2922 | patch->sources[0].role, |
| 2923 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2924 | #if LOG_NDEBUG == 0 |
| 2925 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2926 | 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] | 2927 | patch->sinks[i].role, |
| 2928 | patch->sinks[i].type); |
| 2929 | } |
| 2930 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2931 | |
| 2932 | if (index >= 0) { |
| 2933 | patchDesc = mAudioPatches.valueAt(index); |
| 2934 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2935 | mUidCached, patchDesc->mUid, uid); |
| 2936 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2937 | return INVALID_OPERATION; |
| 2938 | } |
| 2939 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2940 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2944 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2945 | if (outputDesc == NULL) { |
| 2946 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2947 | return BAD_VALUE; |
| 2948 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2949 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2950 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2951 | if (patchDesc != 0) { |
| 2952 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2953 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2954 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2955 | return BAD_VALUE; |
| 2956 | } |
| 2957 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2958 | DeviceVector devices; |
| 2959 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2960 | // Only support mix to devices connection |
| 2961 | // TODO add support for mix to mix connection |
| 2962 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2963 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2964 | return INVALID_OPERATION; |
| 2965 | } |
| 2966 | sp<DeviceDescriptor> devDesc = |
| 2967 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2968 | if (devDesc == 0) { |
| 2969 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2970 | return BAD_VALUE; |
| 2971 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2972 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2973 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2974 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2975 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2976 | NULL, // updatedSamplingRate |
| 2977 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2978 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2979 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2980 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2981 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2982 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2983 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2984 | return INVALID_OPERATION; |
| 2985 | } |
| 2986 | devices.add(devDesc); |
| 2987 | } |
| 2988 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2989 | return INVALID_OPERATION; |
| 2990 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2991 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2992 | // TODO: reconfigure output format and channels here |
| 2993 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2994 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2995 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2996 | index = mAudioPatches.indexOfKey(*handle); |
| 2997 | if (index >= 0) { |
| 2998 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2999 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3000 | } |
| 3001 | patchDesc = mAudioPatches.valueAt(index); |
| 3002 | patchDesc->mUid = uid; |
| 3003 | ALOGV("createAudioPatch() success"); |
| 3004 | } else { |
| 3005 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3006 | return INVALID_OPERATION; |
| 3007 | } |
| 3008 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3009 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3010 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3011 | // only one sink supported when connecting an input device to a mix |
| 3012 | if (patch->num_sinks > 1) { |
| 3013 | return INVALID_OPERATION; |
| 3014 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3015 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3016 | if (inputDesc == NULL) { |
| 3017 | return BAD_VALUE; |
| 3018 | } |
| 3019 | if (patchDesc != 0) { |
| 3020 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3021 | return BAD_VALUE; |
| 3022 | } |
| 3023 | } |
| 3024 | sp<DeviceDescriptor> devDesc = |
| 3025 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3026 | if (devDesc == 0) { |
| 3027 | return BAD_VALUE; |
| 3028 | } |
| 3029 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3030 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3031 | devDesc->mAddress, |
| 3032 | patch->sinks[0].sample_rate, |
| 3033 | NULL, /*updatedSampleRate*/ |
| 3034 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3035 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3036 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3037 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3038 | // FIXME for the parameter type, |
| 3039 | // and the NONE |
| 3040 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3041 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3042 | return INVALID_OPERATION; |
| 3043 | } |
| 3044 | // TODO: reconfigure output format and channels here |
| 3045 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3046 | devDesc->type(), inputDesc->mIoHandle); |
| 3047 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3048 | index = mAudioPatches.indexOfKey(*handle); |
| 3049 | if (index >= 0) { |
| 3050 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3051 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3052 | } |
| 3053 | patchDesc = mAudioPatches.valueAt(index); |
| 3054 | patchDesc->mUid = uid; |
| 3055 | ALOGV("createAudioPatch() success"); |
| 3056 | } else { |
| 3057 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3058 | return INVALID_OPERATION; |
| 3059 | } |
| 3060 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3061 | // device to device connection |
| 3062 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3063 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3064 | return BAD_VALUE; |
| 3065 | } |
| 3066 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3067 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3068 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3069 | if (srcDeviceDesc == 0) { |
| 3070 | return BAD_VALUE; |
| 3071 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3072 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3073 | //update source and sink with our own data as the data passed in the patch may |
| 3074 | // be incomplete. |
| 3075 | struct audio_patch newPatch = *patch; |
| 3076 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3077 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3078 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3079 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3080 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3081 | return INVALID_OPERATION; |
| 3082 | } |
| 3083 | |
| 3084 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3085 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3086 | if (sinkDeviceDesc == 0) { |
| 3087 | return BAD_VALUE; |
| 3088 | } |
| 3089 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3090 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3091 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3092 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3093 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3094 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3095 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3096 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3097 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3098 | return INVALID_OPERATION; |
| 3099 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3100 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3101 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3102 | // if the sink device is reachable via an opened output stream, request to go via |
| 3103 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3104 | audio_io_handle_t output = selectOutput(outputs, |
| 3105 | AUDIO_OUTPUT_FLAG_NONE, |
| 3106 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3107 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3108 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3109 | if (outputDesc->isDuplicated()) { |
| 3110 | return INVALID_OPERATION; |
| 3111 | } |
| 3112 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3113 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3114 | newPatch.num_sources = 2; |
| 3115 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3116 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3117 | } |
| 3118 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3119 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3120 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3121 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3122 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3123 | status); |
| 3124 | return INVALID_OPERATION; |
| 3125 | } |
| 3126 | } else { |
| 3127 | return BAD_VALUE; |
| 3128 | } |
| 3129 | } else { |
| 3130 | return BAD_VALUE; |
| 3131 | } |
| 3132 | return NO_ERROR; |
| 3133 | } |
| 3134 | |
| 3135 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3136 | uid_t uid) |
| 3137 | { |
| 3138 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3139 | |
| 3140 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3141 | |
| 3142 | if (index < 0) { |
| 3143 | return BAD_VALUE; |
| 3144 | } |
| 3145 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3146 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3147 | mUidCached, patchDesc->mUid, uid); |
| 3148 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3149 | return INVALID_OPERATION; |
| 3150 | } |
| 3151 | |
| 3152 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3153 | patchDesc->mUid = mUidCached; |
| 3154 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3155 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3156 | if (outputDesc == NULL) { |
| 3157 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3158 | return BAD_VALUE; |
| 3159 | } |
| 3160 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3161 | setOutputDevice(outputDesc, |
| 3162 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3163 | true, |
| 3164 | 0, |
| 3165 | NULL); |
| 3166 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3167 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3168 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3169 | if (inputDesc == NULL) { |
| 3170 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3171 | return BAD_VALUE; |
| 3172 | } |
| 3173 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3174 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3175 | true, |
| 3176 | NULL); |
| 3177 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3178 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3179 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3180 | status, patchDesc->mAfPatchHandle); |
| 3181 | removeAudioPatch(patchDesc->mHandle); |
| 3182 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3183 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3184 | } else { |
| 3185 | return BAD_VALUE; |
| 3186 | } |
| 3187 | } else { |
| 3188 | return BAD_VALUE; |
| 3189 | } |
| 3190 | return NO_ERROR; |
| 3191 | } |
| 3192 | |
| 3193 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3194 | struct audio_patch *patches, |
| 3195 | unsigned int *generation) |
| 3196 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3197 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3198 | return BAD_VALUE; |
| 3199 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3200 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3201 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3202 | } |
| 3203 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3204 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3205 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3206 | ALOGV("setAudioPortConfig()"); |
| 3207 | |
| 3208 | if (config == NULL) { |
| 3209 | return BAD_VALUE; |
| 3210 | } |
| 3211 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3212 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3213 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3214 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3217 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3218 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3219 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3220 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3221 | if (outputDesc == NULL) { |
| 3222 | return BAD_VALUE; |
| 3223 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3224 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3225 | "setAudioPortConfig() called on duplicated output %d", |
| 3226 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3227 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3228 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3229 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3230 | if (inputDesc == NULL) { |
| 3231 | return BAD_VALUE; |
| 3232 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3233 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3234 | } else { |
| 3235 | return BAD_VALUE; |
| 3236 | } |
| 3237 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3238 | sp<DeviceDescriptor> deviceDesc; |
| 3239 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3240 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3241 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3242 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3243 | } else { |
| 3244 | return BAD_VALUE; |
| 3245 | } |
| 3246 | if (deviceDesc == NULL) { |
| 3247 | return BAD_VALUE; |
| 3248 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3249 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3250 | } else { |
| 3251 | return BAD_VALUE; |
| 3252 | } |
| 3253 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3254 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3255 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3256 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3257 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3258 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3259 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3260 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3261 | if (status != NO_ERROR) { |
| 3262 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3263 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3264 | |
| 3265 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3268 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3269 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3270 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3271 | clearAudioPatches(uid); |
| 3272 | clearSessionRoutes(uid); |
| 3273 | } |
| 3274 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3275 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3276 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3277 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3278 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3279 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3280 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3281 | } |
| 3282 | } |
| 3283 | } |
| 3284 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3285 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3286 | audio_io_handle_t ouptutToSkip) |
| 3287 | { |
| 3288 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3289 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3290 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3291 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3292 | continue; |
| 3293 | } |
| 3294 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3295 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3296 | continue; |
| 3297 | } |
| 3298 | // If the default device for this strategy is on another output mix, |
| 3299 | // invalidate all tracks in this strategy to force re connection. |
| 3300 | // Otherwise select new device on the output mix. |
| 3301 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3302 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3303 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3304 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3305 | } |
| 3306 | } |
| 3307 | } else { |
| 3308 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3309 | setOutputDevice(outputDesc, newDevice, false); |
| 3310 | } |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3315 | { |
| 3316 | // remove output routes associated with this uid |
| 3317 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3318 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3319 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3320 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3321 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3322 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3323 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3324 | } |
| 3325 | } |
| 3326 | } |
| 3327 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3328 | for (const auto& strategy : affectedStrategies) { |
| 3329 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | // remove input routes associated with this uid |
| 3333 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3334 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3335 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3336 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3337 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3338 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3339 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3340 | } |
| 3341 | } |
| 3342 | } |
| 3343 | // reroute inputs if necessary |
| 3344 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3345 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3346 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3347 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3348 | inputsToClose.add(inputDesc->mIoHandle); |
| 3349 | } |
| 3350 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3351 | for (const auto& input : inputsToClose) { |
| 3352 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3353 | } |
| 3354 | } |
| 3355 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3356 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3357 | { |
| 3358 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3359 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3360 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3361 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3362 | } |
| 3363 | } |
| 3364 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3365 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3366 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3367 | audio_io_handle_t *ioHandle, |
| 3368 | audio_devices_t *device) |
| 3369 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3370 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3371 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3372 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3373 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3374 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3375 | } |
| 3376 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3377 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3378 | const audio_attributes_t *attributes, |
| 3379 | audio_port_handle_t *portId, |
| 3380 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3381 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3382 | ALOGV("%s", __FUNCTION__); |
| 3383 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3384 | |
| 3385 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3386 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3387 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3388 | return BAD_VALUE; |
| 3389 | } |
| 3390 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3391 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3392 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3393 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3394 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3395 | return INVALID_OPERATION; |
| 3396 | } |
| 3397 | |
| 3398 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3399 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3400 | String8(source->ext.device.address)); |
| 3401 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3402 | 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] | 3403 | return BAD_VALUE; |
| 3404 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3405 | |
| 3406 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3407 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3408 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3409 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3410 | |
| 3411 | sp<SourceClientDescriptor> sourceDesc = |
| 3412 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3413 | streamTypefromAttributesInt(attributes), |
| 3414 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3415 | |
| 3416 | status_t status = connectAudioSource(sourceDesc); |
| 3417 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3418 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3419 | } |
| 3420 | return status; |
| 3421 | } |
| 3422 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3423 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3424 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3425 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3426 | |
| 3427 | // make sure we only have one patch per source. |
| 3428 | disconnectAudioSource(sourceDesc); |
| 3429 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3430 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3431 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3432 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3433 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3434 | |
| 3435 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3436 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3437 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3438 | |
| 3439 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3440 | |
| 3441 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3442 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3443 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3444 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3445 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3446 | // create patch between src device and output device |
| 3447 | // create Hwoutput and add to mHwOutputs |
| 3448 | } else { |
| 3449 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3450 | audio_io_handle_t output = |
| 3451 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3452 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3453 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3454 | return INVALID_OPERATION; |
| 3455 | } |
| 3456 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3457 | if (outputDesc->isDuplicated()) { |
| 3458 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3459 | return INVALID_OPERATION; |
| 3460 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3461 | status_t status = outputDesc->start(); |
| 3462 | if (status != NO_ERROR) { |
| 3463 | return status; |
| 3464 | } |
| 3465 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3466 | // create a special patch with no sink and two sources: |
| 3467 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3468 | // be connected as well as the stream type for volume control |
| 3469 | // - the sink is defined by whatever output device is currently selected for the output |
| 3470 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3471 | PatchBuilder patchBuilder; |
| 3472 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3473 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3474 | &afPatchHandle, |
| 3475 | 0); |
| 3476 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3477 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3478 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3479 | if (status != NO_ERROR) { |
| 3480 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3481 | __FUNCTION__, status); |
| 3482 | return INVALID_OPERATION; |
| 3483 | } |
| 3484 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3485 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3486 | |
| 3487 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3488 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3489 | return status; |
| 3490 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3491 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3492 | if (delayMs != 0) { |
| 3493 | usleep(delayMs * 1000); |
| 3494 | } |
| 3495 | } |
| 3496 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3497 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3498 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3499 | |
| 3500 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3501 | } |
| 3502 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3503 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3504 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3505 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3506 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3507 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3508 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3509 | return BAD_VALUE; |
| 3510 | } |
| 3511 | status_t status = disconnectAudioSource(sourceDesc); |
| 3512 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3513 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3514 | return status; |
| 3515 | } |
| 3516 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3517 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3518 | { |
| 3519 | if (mMasterMono == mono) { |
| 3520 | return NO_ERROR; |
| 3521 | } |
| 3522 | mMasterMono = mono; |
| 3523 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3524 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3525 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3526 | // |
| 3527 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3528 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3529 | // play back offloaded. |
| 3530 | if (mMasterMono) { |
| 3531 | Vector<audio_io_handle_t> offloaded; |
| 3532 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3533 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3534 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3535 | offloaded.push(desc->mIoHandle); |
| 3536 | } |
| 3537 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3538 | for (const auto& handle : offloaded) { |
| 3539 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3540 | } |
| 3541 | } |
| 3542 | // update master mono for all remaining outputs |
| 3543 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3544 | updateMono(mOutputs.keyAt(i)); |
| 3545 | } |
| 3546 | return NO_ERROR; |
| 3547 | } |
| 3548 | |
| 3549 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3550 | { |
| 3551 | *mono = mMasterMono; |
| 3552 | return NO_ERROR; |
| 3553 | } |
| 3554 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3555 | float AudioPolicyManager::getStreamVolumeDB( |
| 3556 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3557 | { |
| 3558 | return computeVolume(stream, index, device); |
| 3559 | } |
| 3560 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3561 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3562 | audio_format_t *surroundFormats, |
| 3563 | bool *surroundFormatsEnabled, |
| 3564 | bool reported) |
| 3565 | { |
| 3566 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3567 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3568 | return BAD_VALUE; |
| 3569 | } |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3570 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p" |
| 3571 | " reported %d", *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3572 | |
| 3573 | // Only return value if there is HDMI output. |
| 3574 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3575 | return INVALID_OPERATION; |
| 3576 | } |
| 3577 | |
| 3578 | size_t formatsWritten = 0; |
| 3579 | size_t formatsMax = *numSurroundFormats; |
| 3580 | *numSurroundFormats = 0; |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3581 | std::unordered_set<audio_format_t> formats; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3582 | if (reported) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3583 | // Return formats from HDMI profiles, that have already been resolved by |
| 3584 | // checkOutputsForDevice(). |
| 3585 | DeviceVector hdmiOutputDevs = mAvailableOutputDevices.getDevicesFromTypeMask( |
| 3586 | AUDIO_DEVICE_OUT_HDMI); |
| 3587 | for (size_t i = 0; i < hdmiOutputDevs.size(); i++) { |
| 3588 | FormatVector supportedFormats = |
| 3589 | hdmiOutputDevs[i]->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3590 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3591 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3592 | formats.insert(supportedFormats[j]); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3593 | } else { |
| 3594 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3595 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3596 | formats.insert(pair.first); |
| 3597 | break; |
| 3598 | } |
| 3599 | } |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3602 | } |
| 3603 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3604 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3605 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3606 | } |
| 3607 | } |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3608 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3609 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3610 | surroundFormats[formatsWritten] = format; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3611 | bool formatEnabled = false; |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3612 | if (mConfig.getSurroundFormats().count(format) == 0) { |
| 3613 | // Check sub-formats |
| 3614 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3615 | for (const auto& subformat : pair.second) { |
| 3616 | formatEnabled = mSurroundFormats.count(subformat) != 0; |
| 3617 | if (formatEnabled) break; |
| 3618 | } |
| 3619 | if (formatEnabled) break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3620 | } |
| 3621 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3622 | formatEnabled = mSurroundFormats.count(format) != 0; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3623 | } |
| 3624 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3625 | } |
| 3626 | (*numSurroundFormats)++; |
| 3627 | } |
| 3628 | return NO_ERROR; |
| 3629 | } |
| 3630 | |
| 3631 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3632 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3633 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3634 | // Check if audio format is a surround formats. |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3635 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3636 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3637 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3638 | return BAD_VALUE; |
| 3639 | } |
| 3640 | |
| 3641 | // Should only be called when MANUAL. |
| 3642 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3643 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3644 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3645 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3646 | return INVALID_OPERATION; |
| 3647 | } |
| 3648 | |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3649 | if ((mSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3650 | return NO_ERROR; |
| 3651 | } |
| 3652 | |
| 3653 | // The operation is valid only when there is HDMI output available. |
| 3654 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3655 | ALOGW("%s() no HDMI out devices found", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3656 | return INVALID_OPERATION; |
| 3657 | } |
| 3658 | |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3659 | std::unordered_set<audio_format_t> surroundFormatsBackup(mSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3660 | if (enabled) { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3661 | mSurroundFormats.insert(audioFormat); |
| 3662 | for (const auto& subFormat : formatIter->second) { |
| 3663 | mSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3664 | } |
| 3665 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3666 | mSurroundFormats.erase(audioFormat); |
| 3667 | for (const auto& subFormat : formatIter->second) { |
| 3668 | mSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3673 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3674 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3675 | AUDIO_DEVICE_OUT_HDMI); |
| 3676 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3677 | // Simulate reconnection to update enabled surround sound formats. |
| 3678 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3679 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3680 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3681 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3682 | address.c_str(), |
| 3683 | name.c_str()); |
| 3684 | if (status != NO_ERROR) { |
| 3685 | continue; |
| 3686 | } |
| 3687 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3688 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3689 | address.c_str(), |
| 3690 | name.c_str()); |
| 3691 | profileUpdated |= (status == NO_ERROR); |
| 3692 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3693 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3694 | AUDIO_DEVICE_IN_HDMI); |
| 3695 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3696 | // Simulate reconnection to update enabled surround sound formats. |
| 3697 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3698 | String8 name = hdmiInputDevices[i]->getName(); |
| 3699 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3700 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3701 | address.c_str(), |
| 3702 | name.c_str()); |
| 3703 | if (status != NO_ERROR) { |
| 3704 | continue; |
| 3705 | } |
| 3706 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3707 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3708 | address.c_str(), |
| 3709 | name.c_str()); |
| 3710 | profileUpdated |= (status == NO_ERROR); |
| 3711 | } |
| 3712 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3713 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3714 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3715 | mSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3716 | } |
| 3717 | |
| 3718 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3719 | } |
| 3720 | |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3721 | void AudioPolicyManager::setAppState(uid_t uid, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3722 | { |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3723 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3724 | bool silenced = state == APP_STATE_IDLE; |
| 3725 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3726 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3727 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3728 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3729 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3730 | RecordClientVector clients = activeDesc->clientsList(true /*activeOnly*/); |
| 3731 | for (const auto& client : clients) { |
| 3732 | if (uid == client->uid()) { |
| 3733 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | } |
| 3738 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame^] | 3739 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 3740 | { |
| 3741 | for (const auto& hwModule : mHwModules) { |
| 3742 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 3743 | for (const auto &outProfile : outputProfiles) { |
| 3744 | struct audio_port audioPort; |
| 3745 | outProfile->toAudioPort(&audioPort); |
| 3746 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 3747 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 3748 | return true; |
| 3749 | } |
| 3750 | } |
| 3751 | } |
| 3752 | } |
| 3753 | return false; |
| 3754 | } |
| 3755 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3756 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3757 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3758 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3759 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3760 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3761 | if (patchDesc == 0) { |
| 3762 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3763 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3764 | return BAD_VALUE; |
| 3765 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3766 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3767 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3768 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3769 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3770 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3771 | if (status == NO_ERROR) { |
| 3772 | swOutputDesc->stop(); |
| 3773 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3774 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3775 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3776 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3777 | if (hwOutputDesc != 0) { |
| 3778 | // release patch between src device and output device |
| 3779 | // close Hwoutput and remove from mHwOutputs |
| 3780 | } else { |
| 3781 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3782 | } |
| 3783 | } |
| 3784 | return NO_ERROR; |
| 3785 | } |
| 3786 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3787 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3788 | audio_io_handle_t output, routing_strategy strategy) |
| 3789 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3790 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3791 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3792 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3793 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3794 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3795 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3796 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3797 | source = sourceDesc; |
| 3798 | break; |
| 3799 | } |
| 3800 | } |
| 3801 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3802 | } |
| 3803 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3804 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3805 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3806 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3807 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3808 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3809 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3810 | } |
| 3811 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3812 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3813 | static const char *kConfigLocationList[] = |
| 3814 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3815 | static const int kConfigLocationListSize = |
| 3816 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3817 | |
| 3818 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3819 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3820 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3821 | status_t ret; |
| 3822 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3823 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3824 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3825 | // A2DP offload supported but disabled: try to use special XML file |
| 3826 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3827 | } |
| 3828 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3829 | |
| 3830 | for (const char* fileName : fileNames) { |
| 3831 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3832 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3833 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3834 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3835 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3836 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3837 | return ret; |
| 3838 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3839 | } |
| 3840 | } |
| 3841 | return ret; |
| 3842 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3843 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3844 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3845 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3846 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3847 | 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] | 3848 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3849 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3850 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3851 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3852 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3853 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3854 | mAudioPortGeneration(1), |
| 3855 | mBeaconMuteRefCount(0), |
| 3856 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3857 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3858 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3859 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3860 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3861 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3862 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3863 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3864 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3865 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3866 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3867 | { |
| 3868 | loadConfig(); |
| 3869 | initialize(); |
| 3870 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3871 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3872 | // This check is to catch any legacy platform updating to Q without having |
| 3873 | // switched to XML since its deprecation on O. |
| 3874 | // TODO: after Q release, remove this check and flag as XML is now the only |
| 3875 | // option and all legacy platform should have transitioned to XML. |
| 3876 | #ifndef USE_XML_AUDIO_POLICY_CONF |
| 3877 | #error Audio policy no longer supports legacy .conf configuration format |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3878 | #endif |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3879 | |
| 3880 | void AudioPolicyManager::loadConfig() { |
| 3881 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3882 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3883 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3884 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3885 | } |
| 3886 | |
| 3887 | status_t AudioPolicyManager::initialize() { |
| 3888 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3889 | |
| 3890 | // 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] | 3891 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3892 | if (!engineInstance) { |
| 3893 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3894 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3895 | } |
| 3896 | // Retrieve the Policy Manager Interface |
| 3897 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3898 | if (mEngine == NULL) { |
| 3899 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3900 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3901 | } |
| 3902 | mEngine->setObserver(this); |
| 3903 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3904 | if (status != NO_ERROR) { |
| 3905 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3906 | return status; |
| 3907 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3908 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3909 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3910 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3911 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3912 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3913 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3914 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3915 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3916 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3917 | continue; |
| 3918 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3919 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3920 | // open all output streams needed to access attached devices |
| 3921 | // except for direct output streams that are only opened when they are actually |
| 3922 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3923 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3924 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3925 | if (!outProfile->canOpenNewIo()) { |
| 3926 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3927 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3928 | continue; |
| 3929 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3930 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3931 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3932 | continue; |
| 3933 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3934 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3935 | mTtsOutputAvailable = true; |
| 3936 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3937 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3938 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3939 | continue; |
| 3940 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3941 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3942 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3943 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3944 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3945 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3946 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3947 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3948 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3949 | if ((profileType & outputDeviceTypes) == 0) { |
| 3950 | continue; |
| 3951 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3952 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3953 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3954 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3955 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 3956 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3957 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3958 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3959 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3960 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3961 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3962 | |
| 3963 | if (status != NO_ERROR) { |
| 3964 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3965 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3966 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3967 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3968 | for (const auto& dev : supportedDevices) { |
| 3969 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3970 | // 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] | 3971 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3972 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3973 | } |
| 3974 | } |
| 3975 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3976 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3977 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3978 | } |
| 3979 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3980 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3981 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3982 | true, |
| 3983 | 0, |
| 3984 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3985 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3986 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3987 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3988 | // open input streams needed to access attached devices to validate |
| 3989 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3990 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3991 | if (!inProfile->canOpenNewIo()) { |
| 3992 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 3993 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 3994 | continue; |
| 3995 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3996 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3997 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3998 | continue; |
| 3999 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4000 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4001 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4002 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4003 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4004 | if ((profileType & inputDeviceTypes) == 0) { |
| 4005 | continue; |
| 4006 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4007 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4008 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4009 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4010 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4011 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4012 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4013 | : String8(""); |
| 4014 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4015 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4016 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4017 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4018 | status_t status = inputDesc->open(nullptr, |
| 4019 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4020 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4021 | AUDIO_SOURCE_MIC, |
| 4022 | AUDIO_INPUT_FLAG_NONE, |
| 4023 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4024 | |
| 4025 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4026 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4027 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4028 | // 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] | 4029 | if (index >= 0) { |
| 4030 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4031 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4032 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4033 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4034 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4035 | } |
| 4036 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4037 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4038 | } else { |
| 4039 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4040 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4041 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4042 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | // make sure all attached devices have been allocated a unique ID |
| 4046 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4047 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4048 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4049 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4050 | continue; |
| 4051 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4052 | // The device is now validated and can be appended to the available devices of the engine |
| 4053 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4054 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4055 | i++; |
| 4056 | } |
| 4057 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4058 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4059 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4060 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4061 | continue; |
| 4062 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4063 | // The device is now validated and can be appended to the available devices of the engine |
| 4064 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4065 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4066 | i++; |
| 4067 | } |
| 4068 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4069 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4070 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4071 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4072 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4073 | // If microphones address is empty, set it according to device type |
| 4074 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4075 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4076 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4077 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4078 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4079 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4080 | } |
| 4081 | } |
| 4082 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4083 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4084 | if (mPrimaryOutput == 0) { |
| 4085 | ALOGE("Failed to open primary output"); |
| 4086 | status = NO_INIT; |
| 4087 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4088 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4089 | // Silence ALOGV statements |
| 4090 | property_set("log.tag." LOG_TAG, "D"); |
| 4091 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4092 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4093 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4094 | } |
| 4095 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4096 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4097 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4098 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4099 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4100 | } |
| 4101 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4102 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4103 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4104 | mAvailableOutputDevices.clear(); |
| 4105 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4106 | mOutputs.clear(); |
| 4107 | mInputs.clear(); |
| 4108 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4109 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4110 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4111 | } |
| 4112 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4113 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4114 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4115 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4116 | } |
| 4117 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4118 | // --- |
| 4119 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4120 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4121 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4122 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4123 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4124 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4125 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4126 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4127 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4130 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4131 | { |
| 4132 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4133 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4134 | } |
| 4135 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4136 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4137 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4138 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4139 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4140 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4141 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4142 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4143 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4144 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4145 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4146 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4147 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4148 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4149 | if (devDesc != 0) { |
| 4150 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4151 | desc->mIoHandle, address.string()); |
| 4152 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4156 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4157 | audio_policy_dev_state_t state, |
| 4158 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4159 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4160 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4161 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4162 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4163 | |
| 4164 | if (audio_device_is_digital(device)) { |
| 4165 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4166 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4167 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4168 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4169 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | // first list already open outputs that can be routed to this device |
| 4171 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4172 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4173 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4174 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4175 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4176 | outputs.add(mOutputs.keyAt(i)); |
| 4177 | } else { |
| 4178 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4179 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4180 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4181 | } |
| 4182 | } |
| 4183 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4184 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4185 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4186 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4187 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4188 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4189 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4190 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4191 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4192 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4193 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4194 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4195 | } |
| 4196 | } |
| 4197 | } |
| 4198 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4199 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4200 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4201 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4202 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4203 | return BAD_VALUE; |
| 4204 | } |
| 4205 | |
| 4206 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4207 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4208 | 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] | 4209 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4210 | |
| 4211 | // nothing to do if one output is already opened for this profile |
| 4212 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4213 | for (j = 0; j < outputs.size(); j++) { |
| 4214 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4216 | // matching profile: save the sample rates, format and channel masks supported |
| 4217 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4218 | if (audio_device_is_digital(device)) { |
| 4219 | devDesc->importAudioPort(profile); |
| 4220 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4221 | break; |
| 4222 | } |
| 4223 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4224 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4225 | continue; |
| 4226 | } |
| 4227 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4228 | if (!profile->canOpenNewIo()) { |
| 4229 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4230 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4231 | continue; |
| 4232 | } |
| 4233 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4234 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4235 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4236 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4237 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4238 | status_t status = desc->open(nullptr, device, address, |
| 4239 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4240 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4241 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4242 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4243 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4244 | char *param = audio_device_address_to_parameter(device, address); |
| 4245 | mpClientInterface->setParameters(output, String8(param)); |
| 4246 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4247 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4248 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4249 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4250 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4251 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4252 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4253 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4254 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4255 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4256 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4257 | profile->pickAudioProfile( |
| 4258 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4259 | config.offload_info.sample_rate = config.sample_rate; |
| 4260 | config.offload_info.channel_mask = config.channel_mask; |
| 4261 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4262 | |
| 4263 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4264 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4265 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4266 | output = AUDIO_IO_HANDLE_NONE; |
| 4267 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4268 | } |
| 4269 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4270 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4271 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4272 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4273 | sp<AudioPolicyMix> policyMix; |
| 4274 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4275 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4276 | address.string()); |
| 4277 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4278 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4279 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4280 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4281 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4282 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4283 | // no duplicated output for direct outputs and |
| 4284 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4285 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4287 | //TODO: configure audio effect output stage here |
| 4288 | |
| 4289 | // 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] | 4290 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4291 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4292 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4293 | &duplicatedOutput); |
| 4294 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4295 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4296 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4297 | } else { |
| 4298 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4299 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4300 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4301 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4302 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4303 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4304 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4305 | } |
| 4306 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4307 | } else { |
| 4308 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4309 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4310 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4311 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | profiles.removeAt(profile_index); |
| 4313 | profile_index--; |
| 4314 | } else { |
| 4315 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4316 | // Load digital format info only for digital devices |
| 4317 | if (audio_device_is_digital(device)) { |
| 4318 | devDesc->importAudioPort(profile); |
| 4319 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4320 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4321 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4322 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4323 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4324 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4325 | NULL/*patch handle*/, address.string()); |
| 4326 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4327 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4328 | } |
| 4329 | } |
| 4330 | |
| 4331 | if (profiles.isEmpty()) { |
| 4332 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4333 | return BAD_VALUE; |
| 4334 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4335 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | // check if one opened output is not needed any more after disconnecting one device |
| 4337 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4338 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4339 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4340 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4341 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4342 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4343 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4344 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4345 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4346 | mOutputs.keyAt(i)); |
| 4347 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4348 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4349 | } |
| 4350 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4351 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4352 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4353 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4354 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4355 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4356 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4357 | "clearing direct output profile %zu on module %s", |
| 4358 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4359 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | } |
| 4363 | } |
| 4364 | return NO_ERROR; |
| 4365 | } |
| 4366 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4367 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4368 | audio_policy_dev_state_t state, |
| 4369 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4370 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4371 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4372 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4373 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4374 | |
| 4375 | if (audio_device_is_digital(device)) { |
| 4376 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4377 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4378 | } |
| 4379 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4380 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4381 | // first list already open inputs that can be routed to this device |
| 4382 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4383 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4384 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4385 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4386 | inputs.add(mInputs.keyAt(input_index)); |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4391 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4392 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4393 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4394 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4395 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4396 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4397 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4398 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4399 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4400 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4401 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4402 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4403 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4404 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4405 | } |
| 4406 | } |
| 4407 | } |
| 4408 | |
| 4409 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4410 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4411 | return BAD_VALUE; |
| 4412 | } |
| 4413 | |
| 4414 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4415 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4416 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4417 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4418 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4419 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4420 | // nothing to do if one input is already opened for this profile |
| 4421 | size_t input_index; |
| 4422 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4423 | desc = mInputs.valueAt(input_index); |
| 4424 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4425 | if (audio_device_is_digital(device)) { |
| 4426 | devDesc->importAudioPort(profile); |
| 4427 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4428 | break; |
| 4429 | } |
| 4430 | } |
| 4431 | if (input_index != mInputs.size()) { |
| 4432 | continue; |
| 4433 | } |
| 4434 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4435 | if (!profile->canOpenNewIo()) { |
| 4436 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4437 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4438 | continue; |
| 4439 | } |
| 4440 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4441 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4442 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4443 | status_t status = desc->open(nullptr, |
| 4444 | device, |
| 4445 | address, |
| 4446 | AUDIO_SOURCE_MIC, |
| 4447 | AUDIO_INPUT_FLAG_NONE, |
| 4448 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4449 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4450 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4451 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4452 | char *param = audio_device_address_to_parameter(device, address); |
| 4453 | mpClientInterface->setParameters(input, String8(param)); |
| 4454 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4455 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4456 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4457 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4458 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4459 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4460 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4461 | } |
| 4462 | |
| 4463 | if (input != 0) { |
| 4464 | addInput(input, desc); |
| 4465 | } |
| 4466 | } // endif input != 0 |
| 4467 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4468 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4469 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4470 | profiles.removeAt(profile_index); |
| 4471 | profile_index--; |
| 4472 | } else { |
| 4473 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4474 | if (audio_device_is_digital(device)) { |
| 4475 | devDesc->importAudioPort(profile); |
| 4476 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4477 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4478 | } |
| 4479 | } // end scan profiles |
| 4480 | |
| 4481 | if (profiles.isEmpty()) { |
| 4482 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4483 | return BAD_VALUE; |
| 4484 | } |
| 4485 | } else { |
| 4486 | // Disconnect |
| 4487 | // check if one opened input is not needed any more after disconnecting one device |
| 4488 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4489 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4490 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4491 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4492 | mInputs.keyAt(input_index)); |
| 4493 | inputs.add(mInputs.keyAt(input_index)); |
| 4494 | } |
| 4495 | } |
| 4496 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4497 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4498 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4499 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4500 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4501 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4502 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4503 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4504 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4505 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4506 | } |
| 4507 | } |
| 4508 | } |
| 4509 | } // end disconnect |
| 4510 | |
| 4511 | return NO_ERROR; |
| 4512 | } |
| 4513 | |
| 4514 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4515 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4516 | { |
| 4517 | ALOGV("closeOutput(%d)", output); |
| 4518 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4519 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | if (outputDesc == NULL) { |
| 4521 | ALOGW("closeOutput() unknown output %d", output); |
| 4522 | return; |
| 4523 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4524 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4525 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4526 | // look for duplicated outputs connected to the output being removed. |
| 4527 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4528 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4529 | if (dupOutputDesc->isDuplicated() && |
| 4530 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4531 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4532 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4533 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4534 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4535 | } else { |
| 4536 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4537 | } |
| 4538 | // As all active tracks on duplicated output will be deleted, |
| 4539 | // and as they were also referenced on the other output, the reference |
| 4540 | // count for their stream type must be adjusted accordingly on |
| 4541 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4542 | const bool wasActive = outputDesc2->isActive(); |
| 4543 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4544 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4545 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4546 | // stop() will be a no op if the output is still active but is needed in case all |
| 4547 | // active streams refcounts where cleared above |
| 4548 | if (wasActive) { |
| 4549 | outputDesc2->stop(); |
| 4550 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4551 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4552 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4553 | |
| 4554 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4555 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4556 | } |
| 4557 | } |
| 4558 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4559 | nextAudioPortGeneration(); |
| 4560 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4561 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4562 | if (index >= 0) { |
| 4563 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4564 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4565 | mAudioPatches.removeItemsAt(index); |
| 4566 | mpClientInterface->onAudioPatchListUpdate(); |
| 4567 | } |
| 4568 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4569 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4570 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4571 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4572 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4573 | |
| 4574 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4575 | // no direct outputs are open. |
| 4576 | if (getMsdAudioOutDeviceTypes() != AUDIO_DEVICE_NONE) { |
| 4577 | bool directOutputOpen = false; |
| 4578 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4579 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4580 | directOutputOpen = true; |
| 4581 | break; |
| 4582 | } |
| 4583 | } |
| 4584 | if (!directOutputOpen) { |
| 4585 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4586 | setMsdPatch(); |
| 4587 | } |
| 4588 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4589 | } |
| 4590 | |
| 4591 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4592 | { |
| 4593 | ALOGV("closeInput(%d)", input); |
| 4594 | |
| 4595 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4596 | if (inputDesc == NULL) { |
| 4597 | ALOGW("closeInput() unknown input %d", input); |
| 4598 | return; |
| 4599 | } |
| 4600 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4601 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4602 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4603 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4604 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4605 | if (index >= 0) { |
| 4606 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4607 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4608 | mAudioPatches.removeItemsAt(index); |
| 4609 | mpClientInterface->onAudioPatchListUpdate(); |
| 4610 | } |
| 4611 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4612 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4613 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4614 | |
| 4615 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4616 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4617 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4618 | SoundTrigger::setCaptureState(false); |
| 4619 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4622 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4623 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4624 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4625 | { |
| 4626 | SortedVector<audio_io_handle_t> outputs; |
| 4627 | |
| 4628 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4629 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4630 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4631 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4632 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4633 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4634 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4635 | outputs.add(openOutputs.keyAt(i)); |
| 4636 | } |
| 4637 | } |
| 4638 | return outputs; |
| 4639 | } |
| 4640 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4641 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4642 | { |
| 4643 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4644 | // output is suspended before any tracks are moved to it |
| 4645 | checkA2dpSuspend(); |
| 4646 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4647 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4648 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 4649 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4650 | setMsdPatch(); |
| 4651 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4652 | } |
| 4653 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4654 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4655 | { |
| 4656 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4657 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4658 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4659 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4660 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4661 | // also take into account external policy-related changes: add all outputs which are |
| 4662 | // associated with policies in the "before" and "after" output vectors |
| 4663 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4664 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4665 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4666 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4667 | srcOutputs.add(desc->mIoHandle); |
| 4668 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4669 | } |
| 4670 | } |
| 4671 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4672 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4673 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4674 | dstOutputs.add(desc->mIoHandle); |
| 4675 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4676 | } |
| 4677 | } |
| 4678 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4679 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4680 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4681 | // audio from invalidated tracks will be rendered when unmuting |
| 4682 | uint32_t maxLatency = 0; |
| 4683 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4684 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4685 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4686 | maxLatency = desc->latency(); |
| 4687 | } |
| 4688 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4689 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4690 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4691 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4692 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4693 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4694 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4695 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4696 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4697 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4698 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4699 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4700 | if (source != 0){ |
| 4701 | connectAudioSource(source); |
| 4702 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | // Move effects associated to this strategy from previous output to new output |
| 4706 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4707 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4708 | } |
| 4709 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4710 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4711 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4712 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | } |
| 4717 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4718 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4719 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4720 | 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] | 4721 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4722 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4723 | 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] | 4724 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4725 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4726 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4727 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4729 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4730 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4731 | } |
| 4732 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4733 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4734 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4735 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4736 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4737 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4738 | return; |
| 4739 | } |
| 4740 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4741 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4742 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4743 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4744 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4745 | |
| 4746 | // if suspended, restore A2DP output if: |
| 4747 | // ((SCO device is NOT connected) || |
| 4748 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4749 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4750 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4751 | // if not suspended, suspend A2DP output if: |
| 4752 | // (SCO device is connected) && |
| 4753 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4754 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4755 | // |
| 4756 | if (mA2dpSuspended) { |
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->restoreOutput(a2dpOutput); |
| 4766 | mA2dpSuspended = false; |
| 4767 | } |
| 4768 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4769 | if (isScoConnected && |
| 4770 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4771 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4772 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4773 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4774 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4775 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4776 | |
| 4777 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4778 | mA2dpSuspended = true; |
| 4779 | } |
| 4780 | } |
| 4781 | } |
| 4782 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4783 | template <class IoDescriptor, class Filter> |
| 4784 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4785 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4786 | { |
| 4787 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4788 | auto activeClientsWithRoute = |
| 4789 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4790 | active = activeClients.size() > 0; |
| 4791 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4792 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4793 | } |
| 4794 | return nullptr; |
| 4795 | } |
| 4796 | |
| 4797 | template <class IoCollection, class Filter> |
| 4798 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4799 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4800 | { |
| 4801 | sp<DeviceDescriptor> device; |
| 4802 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4803 | auto desc = ioCollection.valueAt(i); |
| 4804 | bool active; |
| 4805 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4806 | if (active && curDevice == nullptr) { |
| 4807 | return nullptr; |
| 4808 | } else if (curDevice != nullptr) { |
| 4809 | device = curDevice; |
| 4810 | } |
| 4811 | } |
| 4812 | return device; |
| 4813 | } |
| 4814 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4815 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4816 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4817 | { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4818 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4819 | if (index >= 0) { |
| 4820 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4821 | if (patchDesc->mUid != mUidCached) { |
| 4822 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4823 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4824 | return outputDesc->device(); |
| 4825 | } |
| 4826 | } |
| 4827 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4828 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4829 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4830 | bool active; // unused |
| 4831 | sp<DeviceDescriptor> deviceDesc = |
| 4832 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
| 4833 | if (deviceDesc != nullptr) { |
| 4834 | return deviceDesc->type(); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4835 | } |
| 4836 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4837 | // 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] | 4838 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4839 | // use device for strategy enforced audible |
| 4840 | // 2: we are in call or the strategy phone is active on the output: |
| 4841 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4842 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4843 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4844 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4845 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4846 | // 5: the strategy accessibility is active on the output: |
| 4847 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4848 | // 6: the strategy "respectful" sonification is active on the output: |
| 4849 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4850 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4851 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4852 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4853 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4854 | // 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] | 4855 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4856 | |
| 4857 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4858 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4859 | // as opposed to all streams on the same audio HAL module. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4860 | audio_devices_t device = AUDIO_DEVICE_NONE; |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4861 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4862 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4863 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4864 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4865 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4866 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4867 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4868 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4869 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4870 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4871 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4872 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4873 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4874 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4875 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4876 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4877 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4878 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4879 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4880 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4881 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4882 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4883 | } |
| 4884 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4885 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4886 | return device; |
| 4887 | } |
| 4888 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4889 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4890 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4891 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4892 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4893 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4894 | if (index >= 0) { |
| 4895 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4896 | if (patchDesc->mUid != mUidCached) { |
| 4897 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4898 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4899 | return inputDesc->mDevice; |
| 4900 | } |
| 4901 | } |
| 4902 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4903 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4904 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4905 | bool active; |
| 4906 | sp<DeviceDescriptor> deviceDesc = |
| 4907 | findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4908 | if (deviceDesc != nullptr) { |
| 4909 | return deviceDesc->type(); |
| 4910 | } |
| 4911 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4912 | // If we are not in call and no client is active on this input, this methods returns |
| 4913 | // AUDIO_DEVICE_NONE, causing the patch on the input stream to be released. |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4914 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4915 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4916 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4917 | } |
| 4918 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4919 | device = getDeviceAndMixForInputSource(source); |
| 4920 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4921 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4922 | return device; |
| 4923 | } |
| 4924 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4925 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4926 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4927 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4928 | } |
| 4929 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4930 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4931 | return (uint32_t)getStrategy(stream); |
| 4932 | } |
| 4933 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4934 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 | // By checking the range of stream before calling getStrategy, we avoid |
| 4936 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4937 | // 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] | 4938 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4939 | return AUDIO_DEVICE_NONE; |
| 4940 | } |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4941 | audio_devices_t activeDevices = AUDIO_DEVICE_NONE; |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4942 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4943 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4944 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4945 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4946 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4947 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4948 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4949 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4950 | devices |= curDevices; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4951 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4952 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4953 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4954 | activeDevices |= outputDesc->device(); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4955 | } |
| 4956 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4957 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4958 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4959 | // Favor devices selected on active streams if any to report correct device in case of |
| 4960 | // explicit device selection |
| 4961 | if (activeDevices != AUDIO_DEVICE_NONE) { |
| 4962 | devices = activeDevices; |
| 4963 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4964 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4965 | and doesn't really need to.*/ |
| 4966 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4967 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4968 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4969 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4970 | return devices; |
| 4971 | } |
| 4972 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4973 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4974 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4975 | 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] | 4976 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4977 | } |
| 4978 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4979 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4980 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4981 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4982 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4983 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4984 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4985 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4986 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4987 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4988 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4989 | } |
| 4990 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4991 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4992 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4993 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4994 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4995 | updateDevicesAndOutputs(); |
| 4996 | break; |
| 4997 | default: |
| 4998 | break; |
| 4999 | } |
| 5000 | } |
| 5001 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5002 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5003 | |
| 5004 | // skip beacon mute management if a dedicated TTS output is available |
| 5005 | if (mTtsOutputAvailable) { |
| 5006 | return 0; |
| 5007 | } |
| 5008 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5009 | switch(event) { |
| 5010 | case STARTING_OUTPUT: |
| 5011 | mBeaconMuteRefCount++; |
| 5012 | break; |
| 5013 | case STOPPING_OUTPUT: |
| 5014 | if (mBeaconMuteRefCount > 0) { |
| 5015 | mBeaconMuteRefCount--; |
| 5016 | } |
| 5017 | break; |
| 5018 | case STARTING_BEACON: |
| 5019 | mBeaconPlayingRefCount++; |
| 5020 | break; |
| 5021 | case STOPPING_BEACON: |
| 5022 | if (mBeaconPlayingRefCount > 0) { |
| 5023 | mBeaconPlayingRefCount--; |
| 5024 | } |
| 5025 | break; |
| 5026 | } |
| 5027 | |
| 5028 | if (mBeaconMuteRefCount > 0) { |
| 5029 | // any playback causes beacon to be muted |
| 5030 | return setBeaconMute(true); |
| 5031 | } else { |
| 5032 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5033 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5034 | } |
| 5035 | } |
| 5036 | |
| 5037 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5038 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5039 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5040 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5041 | if (mBeaconMuted != mute) { |
| 5042 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5043 | ALOGV("\t muting %d", mute); |
| 5044 | uint32_t maxLatency = 0; |
| 5045 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5046 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5047 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5048 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5049 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5050 | const uint32_t latency = desc->latency() * 2; |
| 5051 | if (latency > maxLatency) { |
| 5052 | maxLatency = latency; |
| 5053 | } |
| 5054 | } |
| 5055 | mBeaconMuted = mute; |
| 5056 | return maxLatency; |
| 5057 | } |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5061 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5062 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5063 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5064 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5065 | // case the last active client route is used |
| 5066 | sp<DeviceDescriptor> deviceDesc = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5067 | if (deviceDesc != nullptr) { |
| 5068 | return deviceDesc->type(); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5069 | } |
| 5070 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5071 | if (fromCache) { |
| 5072 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5073 | strategy, mDeviceForStrategy[strategy]); |
| 5074 | return mDeviceForStrategy[strategy]; |
| 5075 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5076 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5077 | } |
| 5078 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5079 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5080 | { |
| 5081 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5082 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5083 | } |
| 5084 | mPreviousOutputs = mOutputs; |
| 5085 | } |
| 5086 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5087 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5088 | audio_devices_t prevDevice, |
| 5089 | uint32_t delayMs) |
| 5090 | { |
| 5091 | // mute/unmute strategies using an incompatible device combination |
| 5092 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5093 | // if unmuting, unmute only after the specified delay |
| 5094 | if (outputDesc->isDuplicated()) { |
| 5095 | return 0; |
| 5096 | } |
| 5097 | |
| 5098 | uint32_t muteWaitMs = 0; |
| 5099 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5100 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5101 | |
| 5102 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5103 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5104 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5105 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5106 | bool doMute = false; |
| 5107 | |
| 5108 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5109 | doMute = true; |
| 5110 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5111 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5112 | doMute = true; |
| 5113 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5114 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5115 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5116 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5117 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5118 | // skip output if it does not share any device with current output |
| 5119 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5120 | == AUDIO_DEVICE_NONE) { |
| 5121 | continue; |
| 5122 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5123 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5124 | mute ? "muting" : "unmuting", i, curDevice); |
| 5125 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5126 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5127 | if (mute) { |
| 5128 | // FIXME: should not need to double latency if volume could be applied |
| 5129 | // immediately by the audioflinger mixer. We must account for the delay |
| 5130 | // between now and the next time the audioflinger thread for this output |
| 5131 | // will process a buffer (which corresponds to one buffer size, |
| 5132 | // usually 1/2 or 1/4 of the latency). |
| 5133 | if (muteWaitMs < desc->latency() * 2) { |
| 5134 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | } |
| 5139 | } |
| 5140 | } |
| 5141 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5142 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5143 | // different per device volumes |
| 5144 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5145 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5146 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5147 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5148 | if (muteWaitMs < tempMuteWaitMs) { |
| 5149 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5150 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5151 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5152 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5153 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5154 | // make sure that we do not start the temporary mute period too early in case of |
| 5155 | // delayed device change |
| 5156 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5157 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5158 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5159 | } |
| 5160 | } |
| 5161 | } |
| 5162 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5163 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5164 | if (muteWaitMs > delayMs) { |
| 5165 | muteWaitMs -= delayMs; |
| 5166 | usleep(muteWaitMs * 1000); |
| 5167 | return muteWaitMs; |
| 5168 | } |
| 5169 | return 0; |
| 5170 | } |
| 5171 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5172 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5173 | audio_devices_t device, |
| 5174 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5175 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5176 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5177 | const char *address, |
| 5178 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5179 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5180 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5181 | AudioParameter param; |
| 5182 | uint32_t muteWaitMs; |
| 5183 | |
| 5184 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5185 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5186 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5187 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5188 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5189 | return muteWaitMs; |
| 5190 | } |
| 5191 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5192 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5193 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5194 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5199 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5200 | |
| 5201 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5202 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5203 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | |
| 5205 | if (device != AUDIO_DEVICE_NONE) { |
| 5206 | outputDesc->mDevice = device; |
| 5207 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5208 | |
| 5209 | // if the outputs are not materially active, there is no need to mute. |
| 5210 | if (requiresMuteCheck) { |
| 5211 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5212 | } else { |
| 5213 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5214 | muteWaitMs = 0; |
| 5215 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5216 | |
| 5217 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5218 | // the requested device is AUDIO_DEVICE_NONE |
| 5219 | // OR the requested device is the same as current device |
| 5220 | // AND force is not specified |
| 5221 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5222 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5223 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5224 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5225 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5226 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5227 | return muteWaitMs; |
| 5228 | } |
| 5229 | |
| 5230 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5231 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5232 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5233 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5234 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5235 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5236 | DeviceVector deviceList; |
| 5237 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5238 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5239 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5240 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5241 | device, String8(address)); |
| 5242 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5243 | } |
| 5244 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5245 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5246 | PatchBuilder patchBuilder; |
| 5247 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5248 | 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] | 5249 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5250 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5251 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5252 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5253 | |
| 5254 | // inform all input as well |
| 5255 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5256 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5257 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5258 | AudioParameter inputCmd = AudioParameter(); |
| 5259 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5260 | inputDescriptor->mIoHandle, device); |
| 5261 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5262 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5263 | inputCmd.toString(), |
| 5264 | delayMs); |
| 5265 | } |
| 5266 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5267 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5268 | |
| 5269 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5270 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5271 | |
| 5272 | return muteWaitMs; |
| 5273 | } |
| 5274 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5275 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5276 | int delayMs, |
| 5277 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5278 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5279 | ssize_t index; |
| 5280 | if (patchHandle) { |
| 5281 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5282 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5283 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5284 | } |
| 5285 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5286 | return INVALID_OPERATION; |
| 5287 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5288 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5289 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5290 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5291 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5292 | removeAudioPatch(patchDesc->mHandle); |
| 5293 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5294 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5295 | return status; |
| 5296 | } |
| 5297 | |
| 5298 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5299 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5300 | bool force, |
| 5301 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5302 | { |
| 5303 | status_t status = NO_ERROR; |
| 5304 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5305 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5306 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5307 | inputDesc->mDevice = device; |
| 5308 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5309 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5310 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5311 | PatchBuilder patchBuilder; |
| 5312 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5313 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5314 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5315 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5316 | auto result = usecase; |
| 5317 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5318 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5319 | } |
| 5320 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5321 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5322 | addSource(deviceList.itemAt(0)); |
| 5323 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5324 | } |
| 5325 | } |
| 5326 | return status; |
| 5327 | } |
| 5328 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5329 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5330 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5331 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5332 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5333 | ssize_t index; |
| 5334 | if (patchHandle) { |
| 5335 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5336 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5337 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5338 | } |
| 5339 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5340 | return INVALID_OPERATION; |
| 5341 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5342 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5343 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5344 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5345 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5346 | removeAudioPatch(patchDesc->mHandle); |
| 5347 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5348 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5349 | return status; |
| 5350 | } |
| 5351 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5352 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5353 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5354 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5355 | audio_format_t& format, |
| 5356 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5357 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5358 | { |
| 5359 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5360 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5361 | // |
| 5362 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5363 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5364 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5365 | sp<IOProfile> firstInexact; |
| 5366 | uint32_t updatedSamplingRate = 0; |
| 5367 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5368 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5369 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5370 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5371 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5372 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5373 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5374 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5375 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5376 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5377 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5378 | &channelMask /*updatedChannelMask*/, |
| 5379 | // FIXME ugly cast |
| 5380 | (audio_output_flags_t) flags, |
| 5381 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5382 | return profile; |
| 5383 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5384 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5385 | samplingRate, |
| 5386 | &updatedSamplingRate, |
| 5387 | format, |
| 5388 | &updatedFormat, |
| 5389 | channelMask, |
| 5390 | &updatedChannelMask, |
| 5391 | // FIXME ugly cast |
| 5392 | (audio_output_flags_t) flags, |
| 5393 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5394 | firstInexact = profile; |
| 5395 | } |
| 5396 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5397 | } |
| 5398 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5399 | if (firstInexact != nullptr) { |
| 5400 | samplingRate = updatedSamplingRate; |
| 5401 | format = updatedFormat; |
| 5402 | channelMask = updatedChannelMask; |
| 5403 | return firstInexact; |
| 5404 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5405 | return NULL; |
| 5406 | } |
| 5407 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5408 | |
| 5409 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5410 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5411 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5412 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5413 | // case the last active client route is used |
| 5414 | sp<DeviceDescriptor> deviceDesc = |
| 5415 | findPreferredDevice(mInputs, inputSource, mAvailableInputDevices); |
| 5416 | if (deviceDesc != nullptr) { |
| 5417 | return deviceDesc->type(); |
| 5418 | } |
| 5419 | |
| 5420 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5421 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5422 | audio_devices_t selectedDeviceFromMix = |
| 5423 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5424 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5425 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5426 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5427 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5428 | return getDeviceForInputSource(inputSource); |
| 5429 | } |
| 5430 | |
| 5431 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5432 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5433 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5436 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5437 | int index, |
| 5438 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5439 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5440 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5441 | |
| 5442 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5443 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5444 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5445 | // the ringtone volume |
| 5446 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5447 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5448 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5449 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5450 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5451 | } |
| 5452 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5453 | // in-call: always cap volume by voice volume + some low headroom |
| 5454 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5455 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5456 | switch (stream) { |
| 5457 | case AUDIO_STREAM_SYSTEM: |
| 5458 | case AUDIO_STREAM_RING: |
| 5459 | case AUDIO_STREAM_MUSIC: |
| 5460 | case AUDIO_STREAM_ALARM: |
| 5461 | case AUDIO_STREAM_NOTIFICATION: |
| 5462 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5463 | case AUDIO_STREAM_DTMF: |
| 5464 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5465 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5466 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5467 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5468 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5469 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5470 | if (volumeDB > maxVoiceVolDb) { |
| 5471 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5472 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5473 | volumeDB = maxVoiceVolDb; |
| 5474 | } |
| 5475 | } break; |
| 5476 | default: |
| 5477 | break; |
| 5478 | } |
| 5479 | } |
| 5480 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5481 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5482 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5483 | // - always attenuate notifications volume by 6dB |
| 5484 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5485 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5486 | // - if music is playing, always limit the volume to current music volume, |
| 5487 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5488 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5489 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5490 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5491 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5492 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5493 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5494 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5495 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5496 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5497 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5498 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5499 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5500 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5501 | // when the phone is ringing we must consider that music could have been paused just before |
| 5502 | // by the music application and behave as if music was active if the last music track was |
| 5503 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5504 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5505 | mLimitRingtoneVolume) { |
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 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5508 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5509 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5510 | musicDevice), |
| 5511 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5512 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5513 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5514 | if (volumeDB > minVolDB) { |
| 5515 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5516 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5517 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5518 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5519 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5520 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5521 | // intended to be played |
| 5522 | if ((volumeDB > -96.0f) && |
| 5523 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5524 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5525 | stream, device, |
| 5526 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5527 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5528 | } |
| 5529 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5530 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5531 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5532 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5533 | } |
| 5534 | } |
| 5535 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5536 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5537 | } |
| 5538 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5539 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5540 | audio_stream_type_t srcStream, |
| 5541 | audio_stream_type_t dstStream) |
| 5542 | { |
| 5543 | if (srcStream == dstStream) { |
| 5544 | return srcIndex; |
| 5545 | } |
| 5546 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5547 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5548 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5549 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5550 | |
| 5551 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5552 | } |
| 5553 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5554 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5555 | int index, |
| 5556 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5557 | audio_devices_t device, |
| 5558 | int delayMs, |
| 5559 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5560 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5561 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5562 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5563 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5564 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5565 | return NO_ERROR; |
| 5566 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5567 | audio_policy_forced_cfg_t forceUseForComm = |
| 5568 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5569 | // 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] | 5570 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5571 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5572 | 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] | 5573 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5574 | return INVALID_OPERATION; |
| 5575 | } |
| 5576 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5577 | if (device == AUDIO_DEVICE_NONE) { |
| 5578 | device = outputDesc->device(); |
| 5579 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5580 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5581 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5582 | if (outputDesc->isFixedVolume(device) || |
| 5583 | // Force VoIP volume to max for bluetooth SCO |
| 5584 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5585 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5586 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5587 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5588 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5589 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5590 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5591 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5592 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5593 | float voiceVolume; |
| 5594 | // 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] | 5595 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5596 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5597 | } else { |
| 5598 | voiceVolume = 1.0; |
| 5599 | } |
| 5600 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5601 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5602 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5603 | mLastVoiceVolume = voiceVolume; |
| 5604 | } |
| 5605 | } |
| 5606 | |
| 5607 | return NO_ERROR; |
| 5608 | } |
| 5609 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5610 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5611 | audio_devices_t device, |
| 5612 | int delayMs, |
| 5613 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5614 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5615 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5616 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5617 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5618 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5619 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5620 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5621 | device, |
| 5622 | delayMs, |
| 5623 | force); |
| 5624 | } |
| 5625 | } |
| 5626 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5627 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5628 | bool on, |
| 5629 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5630 | int delayMs, |
| 5631 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5632 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5633 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5634 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5635 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5636 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5637 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5638 | } |
| 5639 | } |
| 5640 | } |
| 5641 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5642 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5643 | bool on, |
| 5644 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5645 | int delayMs, |
| 5646 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5648 | if (device == AUDIO_DEVICE_NONE) { |
| 5649 | device = outputDesc->device(); |
| 5650 | } |
| 5651 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5652 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5653 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5654 | |
| 5655 | if (on) { |
| 5656 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5657 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5658 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5659 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5660 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5661 | } |
| 5662 | } |
| 5663 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5664 | outputDesc->mMuteCount[stream]++; |
| 5665 | } else { |
| 5666 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5667 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5668 | return; |
| 5669 | } |
| 5670 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5671 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5672 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5673 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5674 | device, |
| 5675 | delayMs); |
| 5676 | } |
| 5677 | } |
| 5678 | } |
| 5679 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5680 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5681 | { |
| 5682 | // flags to stream type mapping |
| 5683 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5684 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5685 | } |
| 5686 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5687 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5688 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5689 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5690 | return AUDIO_STREAM_TTS; |
| 5691 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5692 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5693 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5694 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5695 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5696 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5697 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5698 | // has flags that map to a strategy? |
| 5699 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5700 | return true; |
| 5701 | } |
| 5702 | |
| 5703 | // has known usage? |
| 5704 | switch (paa->usage) { |
| 5705 | case AUDIO_USAGE_UNKNOWN: |
| 5706 | case AUDIO_USAGE_MEDIA: |
| 5707 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5708 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5709 | case AUDIO_USAGE_ALARM: |
| 5710 | case AUDIO_USAGE_NOTIFICATION: |
| 5711 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5712 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5713 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5714 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5715 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5716 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5717 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5718 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5719 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5720 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5721 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5722 | break; |
| 5723 | default: |
| 5724 | return false; |
| 5725 | } |
| 5726 | return true; |
| 5727 | } |
| 5728 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5729 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5730 | routing_strategy strategy, uint32_t inPastMs, |
| 5731 | nsecs_t sysTime) const |
| 5732 | { |
| 5733 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5734 | sysTime = systemTime(); |
| 5735 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5736 | 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] | 5737 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5738 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5739 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5740 | return true; |
| 5741 | } |
| 5742 | } |
| 5743 | return false; |
| 5744 | } |
| 5745 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5746 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5747 | routing_strategy strategy, uint32_t inPastMs, |
| 5748 | nsecs_t sysTime) const |
| 5749 | { |
| 5750 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5751 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5752 | if (outputDesc->sharesHwModuleWith(desc) |
| 5753 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5754 | return true; |
| 5755 | } |
| 5756 | } |
| 5757 | return false; |
| 5758 | } |
| 5759 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5760 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5761 | { |
| 5762 | return mEngine->getForceUse(usage); |
| 5763 | } |
| 5764 | |
| 5765 | bool AudioPolicyManager::isInCall() |
| 5766 | { |
| 5767 | return isStateInCall(mEngine->getPhoneState()); |
| 5768 | } |
| 5769 | |
| 5770 | bool AudioPolicyManager::isStateInCall(int state) |
| 5771 | { |
| 5772 | return is_state_in_call(state); |
| 5773 | } |
| 5774 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5775 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5776 | { |
| 5777 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5778 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5779 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5780 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5781 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5782 | } |
| 5783 | } |
| 5784 | |
| 5785 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5786 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5787 | bool release = false; |
| 5788 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5789 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5790 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5791 | source->ext.device.type == deviceDesc->type()) { |
| 5792 | release = true; |
| 5793 | } |
| 5794 | } |
| 5795 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5796 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5797 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5798 | sink->ext.device.type == deviceDesc->type()) { |
| 5799 | release = true; |
| 5800 | } |
| 5801 | } |
| 5802 | if (release) { |
| 5803 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5804 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5805 | } |
| 5806 | } |
| 5807 | } |
| 5808 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5809 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5810 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5811 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5812 | // TODO Set this based on Config properties. |
| 5813 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5814 | |
| 5815 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5816 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5817 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5818 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5819 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5820 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5821 | formats.clear(); |
| 5822 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5823 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5824 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5825 | // Always enable IEC61937 when in MANUAL mode. |
| 5826 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5827 | } else { // NEVER, AUTO or ALWAYS |
| 5828 | // Analyze original support for various formats. |
| 5829 | bool supportsAC3 = false; |
| 5830 | bool supportsOtherSurround = false; |
| 5831 | bool supportsIEC61937 = false; |
| 5832 | mSurroundFormats.clear(); |
| 5833 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5834 | audio_format_t format = formats[formatIndex]; |
| 5835 | switch (format) { |
| 5836 | case AUDIO_FORMAT_AC3: |
| 5837 | supportsAC3 = true; |
| 5838 | break; |
| 5839 | case AUDIO_FORMAT_E_AC3: |
| 5840 | case AUDIO_FORMAT_DTS: |
| 5841 | case AUDIO_FORMAT_DTS_HD: |
| 5842 | // If ALWAYS, remove all other surround formats here |
| 5843 | // since we will add them later. |
| 5844 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5845 | formats.removeAt(formatIndex); |
| 5846 | formatIndex--; |
| 5847 | } |
| 5848 | supportsOtherSurround = true; |
| 5849 | break; |
| 5850 | case AUDIO_FORMAT_IEC61937: |
| 5851 | supportsIEC61937 = true; |
| 5852 | break; |
| 5853 | default: |
| 5854 | break; |
| 5855 | } |
| 5856 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5857 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5858 | // Modify formats based on surround preferences. |
| 5859 | // If NEVER, remove support for surround formats. |
| 5860 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5861 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5862 | // Remove surround sound related formats. |
| 5863 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5864 | audio_format_t format = formats[formatIndex]; |
| 5865 | switch(format) { |
| 5866 | case AUDIO_FORMAT_AC3: |
| 5867 | case AUDIO_FORMAT_E_AC3: |
| 5868 | case AUDIO_FORMAT_DTS: |
| 5869 | case AUDIO_FORMAT_DTS_HD: |
| 5870 | case AUDIO_FORMAT_IEC61937: |
| 5871 | formats.removeAt(formatIndex); |
| 5872 | break; |
| 5873 | default: |
| 5874 | formatIndex++; // keep it |
| 5875 | break; |
| 5876 | } |
| 5877 | } |
| 5878 | supportsAC3 = false; |
| 5879 | supportsOtherSurround = false; |
| 5880 | supportsIEC61937 = false; |
| 5881 | } |
| 5882 | } else { // AUTO or ALWAYS |
| 5883 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5884 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5885 | && !supportsAC3) { |
| 5886 | formats.add(AUDIO_FORMAT_AC3); |
| 5887 | supportsAC3 = true; |
| 5888 | } |
| 5889 | |
| 5890 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5891 | // This assumes that if any of these formats are reported by the HAL |
| 5892 | // then the report is valid and should not be modified. |
| 5893 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5894 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5895 | formats.add(AUDIO_FORMAT_DTS); |
| 5896 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5897 | supportsOtherSurround = true; |
| 5898 | } |
| 5899 | |
| 5900 | // Add support for IEC61937 if any raw surround supported. |
| 5901 | // The HAL could do this but add it here, just in case. |
| 5902 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5903 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5904 | supportsIEC61937 = true; |
| 5905 | } |
| 5906 | |
| 5907 | // Add reported surround sound formats to enabled surround formats. |
| 5908 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5909 | audio_format_t format = formats[formatIndex]; |
Mikhail Naganov | 02743e2 | 2018-11-28 15:56:55 -0800 | [diff] [blame] | 5910 | if (mConfig.getSurroundFormats().count(format) != 0) { |
| 5911 | mSurroundFormats.insert(format); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5912 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5913 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5914 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5915 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | // Modify the list of channel masks supported. |
| 5919 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5920 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5921 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5922 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5923 | |
| 5924 | // If NEVER, then remove support for channelMasks > stereo. |
| 5925 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5926 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5927 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5928 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5929 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5930 | channelMasks.removeAt(maskIndex); |
| 5931 | } else { |
| 5932 | maskIndex++; |
| 5933 | } |
| 5934 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5935 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5936 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5937 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5938 | bool supports5dot1 = false; |
| 5939 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5940 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5941 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5942 | supports5dot1 = true; |
| 5943 | break; |
| 5944 | } |
| 5945 | } |
| 5946 | // If not then add 5.1 support. |
| 5947 | if (!supports5dot1) { |
| 5948 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5949 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5950 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5951 | } |
| 5952 | } |
| 5953 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5954 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5955 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5956 | AudioProfileVector &profiles) |
| 5957 | { |
| 5958 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5959 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5960 | // Format MUST be checked first to update the list of AudioProfile |
| 5961 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5962 | reply = mpClientInterface->getParameters( |
| 5963 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5964 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5965 | AudioParameter repliedParameters(reply); |
| 5966 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5967 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5968 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5969 | return; |
| 5970 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5971 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5972 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5973 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5974 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5975 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5976 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5977 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5978 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5979 | ChannelsVector channelMasks; |
| 5980 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5981 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5982 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5983 | |
| 5984 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5985 | reply = mpClientInterface->getParameters( |
| 5986 | ioHandle, |
| 5987 | requestedParameters.toString() + ";" + |
| 5988 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5989 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5990 | AudioParameter repliedParameters(reply); |
| 5991 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5992 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5993 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5994 | } |
| 5995 | } |
| 5996 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5997 | reply = mpClientInterface->getParameters(ioHandle, |
| 5998 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5999 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6000 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6001 | AudioParameter repliedParameters(reply); |
| 6002 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6003 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6004 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6005 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6006 | filterSurroundChannelMasks(&channelMasks); |
| 6007 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6008 | } |
| 6009 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6010 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6011 | } |
| 6012 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6013 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6014 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6015 | audio_patch_handle_t *patchHandle, |
| 6016 | AudioIODescriptorInterface *ioDescriptor, |
| 6017 | const struct audio_patch *patch, |
| 6018 | int delayMs) |
| 6019 | { |
| 6020 | ssize_t index = mAudioPatches.indexOfKey( |
| 6021 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6022 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6023 | sp<AudioPatch> patchDesc; |
| 6024 | status_t status = installPatch( |
| 6025 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6026 | if (status == NO_ERROR) { |
| 6027 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6028 | } |
| 6029 | return status; |
| 6030 | } |
| 6031 | |
| 6032 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6033 | ssize_t index, |
| 6034 | audio_patch_handle_t *patchHandle, |
| 6035 | const struct audio_patch *patch, |
| 6036 | int delayMs, |
| 6037 | uid_t uid, |
| 6038 | sp<AudioPatch> *patchDescPtr) |
| 6039 | { |
| 6040 | sp<AudioPatch> patchDesc; |
| 6041 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6042 | if (index >= 0) { |
| 6043 | patchDesc = mAudioPatches.valueAt(index); |
| 6044 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6045 | } |
| 6046 | |
| 6047 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6048 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6049 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6050 | if (status == NO_ERROR) { |
| 6051 | if (index < 0) { |
| 6052 | patchDesc = new AudioPatch(patch, uid); |
| 6053 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6054 | } else { |
| 6055 | patchDesc->mPatch = *patch; |
| 6056 | } |
| 6057 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6058 | if (patchHandle) { |
| 6059 | *patchHandle = patchDesc->mHandle; |
| 6060 | } |
| 6061 | nextAudioPortGeneration(); |
| 6062 | mpClientInterface->onAudioPatchListUpdate(); |
| 6063 | } |
| 6064 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6065 | return status; |
| 6066 | } |
| 6067 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6068 | } // namespace android |