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); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 485 | audio_io_handle_t output = selectOutput(outputs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 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); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 752 | audio_io_handle_t output = selectOutput(outputs); |
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); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 1067 | output = selectOutput(outputs, *flags, config->format, |
| 1068 | config->channel_mask, config->sample_rate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1069 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 1070 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1071 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1072 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1074 | return output; |
| 1075 | } |
| 1076 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1077 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1078 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1079 | if (msdModule != 0) { |
| 1080 | DeviceVector msdInputDevices = mAvailableInputDevices.getDevicesFromHwModule( |
| 1081 | msdModule->getHandle()); |
| 1082 | if (!msdInputDevices.isEmpty()) return msdInputDevices.itemAt(0); |
| 1083 | } |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | audio_devices_t AudioPolicyManager::getMsdAudioOutDeviceTypes() const { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1088 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1089 | if (msdModule != 0) { |
| 1090 | return mAvailableOutputDevices.getDeviceTypesFromHwModule(msdModule->getHandle()); |
| 1091 | } |
| 1092 | return AUDIO_DEVICE_NONE; |
| 1093 | } |
| 1094 | |
| 1095 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1096 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1097 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1098 | if (msdModule != 0) { |
| 1099 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1100 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1101 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1102 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1103 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1104 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1105 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1106 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | return msdPatches; |
| 1111 | } |
| 1112 | |
| 1113 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(audio_devices_t outputDevice, |
| 1114 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1115 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1116 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1117 | if (msdModule == nullptr) { |
| 1118 | ALOGE("%s() unable to get MSD module", __func__); |
| 1119 | return NO_INIT; |
| 1120 | } |
| 1121 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1122 | if (deviceModule == nullptr) { |
| 1123 | ALOGE("%s() unable to get module for %#x", __func__, outputDevice); |
| 1124 | return NO_INIT; |
| 1125 | } |
| 1126 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1127 | if (inputProfiles.isEmpty()) { |
| 1128 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1129 | return NO_INIT; |
| 1130 | } |
| 1131 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1132 | if (outputProfiles.isEmpty()) { |
| 1133 | ALOGE("%s() no output profiles for device %#x", __func__, outputDevice); |
| 1134 | return NO_INIT; |
| 1135 | } |
| 1136 | AudioProfileVector msdProfiles; |
| 1137 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1138 | for (const auto &inProfile : inputProfiles) { |
| 1139 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1140 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1141 | } |
| 1142 | } |
| 1143 | AudioProfileVector deviceProfiles; |
| 1144 | for (const auto &outProfile : outputProfiles) { |
| 1145 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1146 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1147 | } |
| 1148 | } |
| 1149 | struct audio_config_base bestSinkConfig; |
| 1150 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1151 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1152 | &bestSinkConfig); |
| 1153 | if (result != NO_ERROR) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1154 | ALOGD("%s() no matching profiles found for device: %#x, hwAvSync: %d", |
| 1155 | __func__, outputDevice, hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1156 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1157 | } |
| 1158 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1159 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1160 | sinkConfig->format = bestSinkConfig.format; |
| 1161 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1162 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1163 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1164 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1165 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1166 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1167 | sourceConfig->format = bestSinkConfig.format; |
| 1168 | // Copy input stream directly without any processing (e.g. resampling). |
| 1169 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1170 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1171 | if (hwAvSync) { |
| 1172 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1173 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1174 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1175 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1176 | } |
| 1177 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1178 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1179 | sinkConfig->config_mask |= config_mask; |
| 1180 | sourceConfig->config_mask |= config_mask; |
| 1181 | return NO_ERROR; |
| 1182 | } |
| 1183 | |
| 1184 | PatchBuilder AudioPolicyManager::buildMsdPatch(audio_devices_t outputDevice) const |
| 1185 | { |
| 1186 | PatchBuilder patchBuilder; |
| 1187 | patchBuilder.addSource(getMsdAudioInDevice()). |
| 1188 | addSink(findDevice(mAvailableOutputDevices, outputDevice)); |
| 1189 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1190 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1191 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1192 | // For now, we just forcefully try with HwAvSync first. |
| 1193 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1194 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1195 | getBestMsdAudioProfileFor( |
| 1196 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1197 | if (res == NO_ERROR) { |
| 1198 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1199 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1200 | } |
| 1201 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1202 | " supporting PCM format conversion.", __func__); |
| 1203 | return patchBuilder; |
| 1204 | } |
| 1205 | |
| 1206 | status_t AudioPolicyManager::setMsdPatch(audio_devices_t outputDevice) { |
| 1207 | ALOGV("%s() for outputDevice %#x", __func__, outputDevice); |
| 1208 | if (outputDevice == AUDIO_DEVICE_NONE) { |
| 1209 | // Use media strategy for unspecified output device. This should only |
| 1210 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1211 | // therefore invalidate explicit routing requests. |
| 1212 | outputDevice = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1213 | } |
| 1214 | PatchBuilder patchBuilder = buildMsdPatch(outputDevice); |
| 1215 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1216 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1217 | if (!msdPatches.isEmpty()) { |
| 1218 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1219 | "The current MSD prototype only supports one output patch"); |
| 1220 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1221 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1222 | return NO_ERROR; |
| 1223 | } |
| 1224 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1225 | } |
| 1226 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1227 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1228 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1229 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
| 1230 | "device:%#x (format:%#x channels:%#x samplerate:%d)", __func__, outputDevice, |
| 1231 | patch->sources[0].format, patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
| 1232 | return status; |
| 1233 | } |
| 1234 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1235 | 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] | 1236 | audio_output_flags_t flags, |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 1237 | audio_format_t format, |
| 1238 | audio_channel_mask_t channelMask, |
| 1239 | uint32_t samplingRate) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1240 | { |
| 1241 | // select one output among several that provide a path to a particular device or set of |
| 1242 | // devices (the list was previously build by getOutputsForDevice()). |
| 1243 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 1244 | // 1: the output supporting haptic playback when requesting haptic playback |
| 1245 | // 2: the output with the highest number of requested policy flags |
| 1246 | // 3: the output with the bit depth the closest to the requested one |
| 1247 | // 4: the primary output |
| 1248 | // 5: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1249 | |
| 1250 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1251 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | } |
| 1253 | if (outputs.size() == 1) { |
| 1254 | return outputs[0]; |
| 1255 | } |
| 1256 | |
| 1257 | int maxCommonFlags = 0; |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 1258 | const size_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1259 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1260 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1261 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1262 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1263 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1264 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1265 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1266 | for (audio_io_handle_t output : outputs) { |
| 1267 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1268 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1269 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1270 | continue; |
| 1271 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 1272 | // If haptic channel is specified, use the haptic output if present. |
| 1273 | // When using haptic output, same audio format and sample rate are required. |
| 1274 | if (hapticChannelCount > 0) { |
| 1275 | // If haptic channel is specified, use the first output that |
| 1276 | // support haptic playback. |
| 1277 | if (audio_channel_count_from_out_mask( |
| 1278 | outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) >= hapticChannelCount |
| 1279 | && format == outputDesc->mFormat |
| 1280 | && samplingRate == outputDesc->mSamplingRate) { |
| 1281 | return output; |
| 1282 | } |
| 1283 | } else { |
| 1284 | // When haptic channel is not specified, skip haptic output. |
| 1285 | if (outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 1286 | continue; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1290 | // if a valid format is specified, skip output if not compatible |
| 1291 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1292 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1293 | continue; |
| 1294 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1295 | if (AudioPort::isBetterFormatMatch( |
| 1296 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1297 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1298 | bestFormat = outputDesc->mFormat; |
| 1299 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1302 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1303 | if (commonFlags >= maxCommonFlags) { |
| 1304 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1305 | if (format != AUDIO_FORMAT_INVALID |
| 1306 | && AudioPort::isBetterFormatMatch( |
| 1307 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1308 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1309 | bestFormatForFlags = outputDesc->mFormat; |
| 1310 | } |
| 1311 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1312 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1313 | maxCommonFlags = commonFlags; |
| 1314 | bestFormatForFlags = outputDesc->mFormat; |
| 1315 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1316 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1317 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1318 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1319 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1320 | } |
| 1321 | } |
| 1322 | } |
| 1323 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1324 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1325 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1326 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1327 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1328 | return outputForFormat; |
| 1329 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1330 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1331 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | return outputs[0]; |
| 1335 | } |
| 1336 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1337 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1338 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1339 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1340 | |
| 1341 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1342 | if (outputDesc == 0) { |
| 1343 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1344 | return BAD_VALUE; |
| 1345 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1346 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1347 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1348 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1349 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1350 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1351 | status_t status = outputDesc->start(); |
| 1352 | if (status != NO_ERROR) { |
| 1353 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1356 | uint32_t delayMs; |
| 1357 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1358 | |
| 1359 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1360 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1361 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1362 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1363 | if (delayMs != 0) { |
| 1364 | usleep(delayMs * 1000); |
| 1365 | } |
| 1366 | |
| 1367 | return status; |
| 1368 | } |
| 1369 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1370 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1371 | const sp<TrackClientDescriptor>& client, |
| 1372 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1373 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1374 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1375 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1376 | |
| 1377 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1378 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1379 | if (stream == AUDIO_STREAM_TTS) { |
| 1380 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1381 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1382 | return INVALID_OPERATION; |
| 1383 | } else { |
| 1384 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1385 | } |
| 1386 | } else { |
| 1387 | // some playback other than beacon starts |
| 1388 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1389 | } |
| 1390 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1391 | // 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] | 1392 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1393 | bool force = !outputDesc->isActive() && |
| 1394 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1395 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1396 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 1397 | AudioMix *policyMix = NULL; |
| 1398 | const char *address = NULL; |
| 1399 | if (outputDesc->mPolicyMix != NULL) { |
| 1400 | policyMix = outputDesc->mPolicyMix; |
| 1401 | address = policyMix->mDeviceAddress.string(); |
| 1402 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1403 | device = policyMix->mDeviceType; |
| 1404 | } else { |
| 1405 | device = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1406 | } |
| 1407 | } |
| 1408 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1409 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1410 | // It covers a common case when there is no materially active audio |
| 1411 | // and muting would result in unnecessary delay and dropped audio. |
| 1412 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1413 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1414 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1415 | // increment usage count for this stream on the requested output: |
| 1416 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1417 | // 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] | 1418 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1419 | |
| 1420 | if (client->hasPreferredDevice(true)) { |
| 1421 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 1422 | if (device != outputDesc->device()) { |
| 1423 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1424 | } |
| 1425 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1426 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1427 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1428 | selectOutputForMusicEffects(); |
| 1429 | } |
| 1430 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1431 | if (outputDesc->streamActiveCount(stream) == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1432 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1433 | if (device == AUDIO_DEVICE_NONE) { |
| 1434 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1435 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1436 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1437 | routing_strategy strategy = getStrategy(stream); |
| 1438 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1439 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1440 | (beaconMuteLatency > 0); |
| 1441 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1442 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1443 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1445 | // An output has a shared device if |
| 1446 | // - managed by the same hw module |
| 1447 | // - supports the currently selected device |
| 1448 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1449 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1450 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1451 | // force a device change if any other output is: |
| 1452 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1453 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1454 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1455 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1456 | // In this case, the audio HAL must receive the new device selection so that it can |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1457 | // change the device currently selected by the other output. |
| 1458 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1459 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1460 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1461 | force = true; |
| 1462 | } |
| 1463 | // wait for audio on other active outputs to be presented when starting |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1464 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1465 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1466 | const uint32_t latencyMs = desc->latency(); |
| 1467 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1468 | |
| 1469 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1470 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1471 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1472 | |
| 1473 | // Require mute check if another output is on a shared device |
| 1474 | // and currently active to have proper drain and avoid pops. |
| 1475 | // Note restoring AudioTracks onto this output needs to invoke |
| 1476 | // a volume ramp if there is no mute. |
| 1477 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1478 | } |
| 1479 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1480 | |
| 1481 | const uint32_t muteWaitMs = |
| 1482 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1483 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1484 | // apply volume rules for current stream and device if necessary |
| 1485 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1486 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1487 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1488 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1489 | |
| 1490 | // update the outputs if starting an output with a stream that can affect notification |
| 1491 | // routing |
| 1492 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1493 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1494 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1495 | if (strategy == STRATEGY_SONIFICATION) { |
| 1496 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1497 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1498 | |
| 1499 | if (waitMs > muteWaitMs) { |
| 1500 | *delayMs = waitMs - muteWaitMs; |
| 1501 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1502 | |
| 1503 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1504 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1505 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1506 | // change occurs after the MixerThread starts and causes a stream volume |
| 1507 | // glitch. |
| 1508 | // |
| 1509 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1510 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1511 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1512 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1513 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1514 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1515 | } |
| 1516 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1517 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1518 | // of type MIX_TYPE_RECORDERS |
| 1519 | if (audio_is_remote_submix_device(device) && policyMix != NULL && |
| 1520 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1521 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1522 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1523 | address, |
| 1524 | "remote-submix"); |
| 1525 | } |
| 1526 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | return NO_ERROR; |
| 1528 | } |
| 1529 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1530 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1531 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1532 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1533 | |
| 1534 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1535 | if (outputDesc == 0) { |
| 1536 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1537 | return BAD_VALUE; |
| 1538 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1539 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1540 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1541 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1542 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1543 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1544 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1545 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1546 | if (status == NO_ERROR ) { |
| 1547 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1548 | } |
| 1549 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1550 | } |
| 1551 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1552 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1553 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1554 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1555 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1556 | audio_stream_type_t stream = client->stream(); |
| 1557 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1558 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1559 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1560 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1561 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1562 | // Automatically disable the remote submix input when output is stopped on a |
| 1563 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1564 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1565 | outputDesc->mPolicyMix != NULL && |
| 1566 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1567 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1568 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1569 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1570 | "remote-submix"); |
| 1571 | } |
| 1572 | } |
| 1573 | bool forceDeviceUpdate = false; |
| 1574 | if (client->hasPreferredDevice(true)) { |
| 1575 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1576 | forceDeviceUpdate = true; |
| 1577 | } |
| 1578 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1579 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1580 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1581 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1582 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1583 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1584 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1585 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1586 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1587 | // the track stop() command is received and at that time the audio track buffer can |
| 1588 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1589 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1590 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1591 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1592 | |
| 1593 | // force restoring the device selection on other active outputs if it differs from the |
| 1594 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1595 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1596 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1597 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1598 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1599 | desc->isActive() && |
| 1600 | outputDesc->sharesHwModuleWith(desc) && |
| 1601 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1602 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1603 | bool force = desc->device() != newDevice2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1604 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1605 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1606 | newDevice2, |
| 1607 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1608 | delayMs); |
| 1609 | // re-apply device specific volume if not done by setOutputDevice() |
| 1610 | if (!force) { |
| 1611 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1612 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1616 | handleNotificationRoutingForStream(stream); |
| 1617 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1618 | |
| 1619 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1620 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1621 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1622 | } |
| 1623 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1624 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1625 | selectOutputForMusicEffects(); |
| 1626 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1627 | return NO_ERROR; |
| 1628 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1629 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1630 | return INVALID_OPERATION; |
| 1631 | } |
| 1632 | } |
| 1633 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1634 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1635 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1636 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1637 | |
| 1638 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1639 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1640 | // If an output descriptor is closed due to a device routing change, |
| 1641 | // then there are race conditions with releaseOutput from tracks |
| 1642 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1643 | // destroyed shortly thereafter. |
| 1644 | // |
| 1645 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1646 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1647 | return; |
| 1648 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1649 | |
| 1650 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1651 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1652 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1653 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1654 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1655 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1656 | return; |
| 1657 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1658 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1659 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1660 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1663 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1664 | // otherwise there may be inaccurate stream reference counts. |
| 1665 | // This is checked in outputDesc->removeClient below. |
| 1666 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1667 | } |
| 1668 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1669 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1670 | audio_io_handle_t *input, |
| 1671 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1672 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1673 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1674 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1675 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1676 | input_type_t *inputType, |
| 1677 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1679 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1680 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1681 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1682 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1683 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1684 | // handle legacy remote submix case where the address was not always specified |
| 1685 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1686 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1687 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1688 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1689 | DeviceVector inputDevices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1690 | sp<AudioInputDescriptor> inputDesc; |
| 1691 | sp<RecordClientDescriptor> clientDesc; |
| 1692 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1693 | bool isSoundTrigger; |
| 1694 | audio_devices_t device; |
| 1695 | |
| 1696 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1697 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1698 | return INVALID_OPERATION; |
| 1699 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1700 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1701 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1702 | inputSource = AUDIO_SOURCE_MIC; |
| 1703 | } |
| 1704 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1705 | // Explicit routing? |
| 1706 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1707 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
jiabin | c0c831a | 2018-01-29 17:55:15 -0800 | [diff] [blame] | 1708 | deviceDesc = mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1709 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1710 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1711 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1712 | // possible |
| 1713 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1714 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1715 | ssize_t index = mInputs.indexOfKey(*input); |
| 1716 | if (index < 0) { |
| 1717 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1718 | status = BAD_VALUE; |
| 1719 | goto error; |
| 1720 | } |
| 1721 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1722 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1723 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1724 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1725 | status = BAD_VALUE; |
| 1726 | goto error; |
| 1727 | } |
| 1728 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1729 | // 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] | 1730 | // corresponds to a new client and is only permitted from the same UID. |
| 1731 | // 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] | 1732 | if (clients.size() > 1) { |
| 1733 | for (const auto& client : clients) { |
| 1734 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1735 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1736 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1737 | // to an actual client |
| 1738 | if (client == *clients.cbegin()) { |
| 1739 | continue; |
| 1740 | } |
| 1741 | if (uid != client->uid() && !client->isSilenced()) { |
| 1742 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1743 | uid, client->portId(), client->uid()); |
| 1744 | status = INVALID_OPERATION; |
| 1745 | goto error; |
| 1746 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1747 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1748 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1749 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1750 | device = inputDesc->mDevice; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1751 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1752 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1753 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | *input = AUDIO_IO_HANDLE_NONE; |
| 1757 | *inputType = API_INPUT_INVALID; |
| 1758 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1759 | halInputSource = inputSource; |
| 1760 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1761 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1762 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1763 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1764 | if (status != NO_ERROR) { |
| 1765 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1766 | } |
| 1767 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1768 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1769 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1770 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1771 | if (deviceDesc != 0) { |
| 1772 | device = deviceDesc->type(); |
| 1773 | } else { |
| 1774 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
| 1775 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1776 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1777 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1778 | status = BAD_VALUE; |
| 1779 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1780 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1781 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1782 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1783 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1784 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1785 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1786 | // know about it and is therefore considered "legacy" |
| 1787 | *inputType = API_INPUT_LEGACY; |
| 1788 | } else { |
| 1789 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1790 | // an external policy |
| 1791 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1792 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1793 | } else if (audio_is_remote_submix_device(device)) { |
| 1794 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1795 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1796 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1797 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1798 | } else { |
| 1799 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1800 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1801 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1802 | } |
| 1803 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1804 | *input = getInputForDevice(device, address, session, inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1805 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1806 | policyMix); |
| 1807 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1808 | status = INVALID_OPERATION; |
| 1809 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1810 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1811 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1812 | exit: |
| 1813 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1814 | inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1815 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1816 | : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1817 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1818 | isSoundTrigger = inputSource == AUDIO_SOURCE_HOTWORD && |
| 1819 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1820 | *portId = AudioPort::getNextUniqueId(); |
| 1821 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1822 | clientDesc = new RecordClientDescriptor(*portId, uid, session, |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1823 | *attr, *config, requestedDeviceId, |
| 1824 | inputSource,flags, isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1825 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1826 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1827 | |
| 1828 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1829 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1830 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1831 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1832 | |
| 1833 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1834 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | |
| 1838 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1839 | String8 address, |
| 1840 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1841 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1842 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1843 | audio_input_flags_t flags, |
| 1844 | AudioMix *policyMix) |
| 1845 | { |
| 1846 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1847 | audio_source_t halInputSource = inputSource; |
| 1848 | bool isSoundTrigger = false; |
| 1849 | |
| 1850 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1851 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1852 | if (index >= 0) { |
| 1853 | input = mSoundTriggerSessions.valueFor(session); |
| 1854 | isSoundTrigger = true; |
| 1855 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1856 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1857 | } else { |
| 1858 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1859 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1860 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1861 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1862 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1865 | // find a compatible input profile (not necessarily identical in parameters) |
| 1866 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1867 | // sampling rate and flags may be updated by getInputProfile |
| 1868 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1869 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1870 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1871 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1872 | audio_input_flags_t profileFlags = flags; |
| 1873 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1874 | 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] | 1875 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1876 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1877 | profileFlags); |
| 1878 | if (profile != 0) { |
| 1879 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1880 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1881 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1882 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1883 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1884 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1885 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1886 | "sampling rate %u, format %#x, channel mask 0x%X, flags %#x", |
| 1887 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1888 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1889 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1890 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1891 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1892 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1893 | if (samplingRate == 0) { |
| 1894 | samplingRate = profileSamplingRate; |
| 1895 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1896 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1897 | if (profile->getModuleHandle() == 0) { |
| 1898 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1899 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1902 | if (!profile->canOpenNewIo()) { |
| 1903 | return AUDIO_IO_HANDLE_NONE; |
| 1904 | } |
| 1905 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1906 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1907 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1908 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1909 | lConfig.sample_rate = profileSamplingRate; |
| 1910 | lConfig.channel_mask = profileChannelMask; |
| 1911 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1912 | |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1913 | if (address == "") { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 1914 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1915 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 1916 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1917 | } |
| 1918 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1919 | status_t status = inputDesc->open(&lConfig, device, address, |
| 1920 | halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1921 | |
| 1922 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1923 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1924 | (profileSamplingRate != lConfig.sample_rate) || |
| 1925 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1926 | (profileChannelMask != lConfig.channel_mask)) { |
| 1927 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1928 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1929 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1930 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1931 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1932 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1933 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1936 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1937 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1938 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1939 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1940 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1941 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1944 | //static |
| 1945 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1946 | { |
| 1947 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1948 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1949 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1950 | } |
| 1951 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1952 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1953 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1954 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1955 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1956 | unsigned int numModules = 0; |
| 1957 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1958 | |
| 1959 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1960 | if (status == NO_ERROR && numModules != 0) { |
| 1961 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1962 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1963 | if (nModules == NULL) { |
| 1964 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1965 | // ram the next time this function is called. |
| 1966 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1967 | return false; |
| 1968 | } |
| 1969 | |
| 1970 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1971 | if (status == NO_ERROR) { |
| 1972 | soundTriggerSupportsConcurrentCapture = true; |
| 1973 | for (size_t i = 0; i < numModules; ++i) { |
| 1974 | soundTriggerSupportsConcurrentCapture &= |
| 1975 | nModules[i].properties.concurrent_capture; |
| 1976 | } |
| 1977 | } |
| 1978 | free(nModules); |
| 1979 | } |
| 1980 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1981 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1982 | } |
| 1983 | return mSoundTriggerSupportsConcurrentCapture; |
| 1984 | } |
| 1985 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1986 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1987 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId, |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 1988 | bool silenced, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1989 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1990 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1991 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
| 1992 | |
| 1993 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1994 | |
| 1995 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 1996 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1997 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1998 | return BAD_VALUE; |
| 1999 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2000 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2001 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2002 | if (client->active()) { |
| 2003 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2004 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2005 | } |
| 2006 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2007 | audio_session_t session = client->session(); |
| 2008 | |
| 2009 | ALOGV("%s input:%d, session:%d, silenced:%d, concurrency:%d)", |
| 2010 | __FUNCTION__, input, session, silenced, *concurrency); |
| 2011 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2012 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 2013 | if (mCallTxPatch != 0 && |
| 2014 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 2015 | ALOGW("startInput(%d) failed: call in progress", input); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2016 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2017 | return INVALID_OPERATION; |
| 2018 | } |
| 2019 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2020 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2021 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2022 | // If a UID is idle and records silence and another not silenced recording starts |
| 2023 | // from another UID (idle or active) we stop the current idle UID recording in |
| 2024 | // favor of the new one - "There can be only one" TM |
| 2025 | if (!silenced) { |
| 2026 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2027 | if ((activeDesc->getAudioPort()->getFlags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2028 | activeDesc->getId() == inputDesc->getId()) { |
| 2029 | continue; |
| 2030 | } |
| 2031 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2032 | RecordClientVector activeClients = activeDesc->clientsList(true /*activeOnly*/); |
| 2033 | for (const auto& activeClient : activeClients) { |
| 2034 | if (activeClient->isSilenced()) { |
| 2035 | closeClient(activeClient->portId()); |
| 2036 | ALOGV("%s client %d stopping silenced client %d", __FUNCTION__, |
| 2037 | portId, activeClient->portId()); |
| 2038 | activeInputs = mInputs.getActiveInputs(); |
| 2039 | } |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | for (const auto& activeDesc : activeInputs) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2045 | if ((client->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 2046 | activeDesc->getId() == inputDesc->getId()) { |
| 2047 | continue; |
| 2048 | } |
| 2049 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2050 | audio_source_t activeSource = activeDesc->inputSource(true); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2051 | if (client->source() == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2052 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 2053 | if (activeDesc->hasPreemptedSession(session)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2054 | ALOGW("%s input %d failed for HOTWORD: " |
| 2055 | "other input %d already started for HOTWORD", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2056 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2057 | *concurrency |= API_INPUT_CONCURRENCY_HOTWORD; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2058 | return INVALID_OPERATION; |
| 2059 | } |
| 2060 | } else { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2061 | ALOGV("%s input %d failed for HOTWORD: other input %d already started", |
| 2062 | __FUNCTION__, input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2063 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2064 | return INVALID_OPERATION; |
| 2065 | } |
| 2066 | } else { |
| 2067 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2068 | ALOGW("%s input %d failed: other input %d already started", __FUNCTION__, |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2069 | input, activeDesc->mIoHandle); |
Ray Essick | 84e84a5 | 2018-05-03 18:45:07 -0700 | [diff] [blame] | 2070 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2071 | return INVALID_OPERATION; |
| 2072 | } |
| 2073 | } |
| 2074 | } |
| 2075 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2076 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 2077 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 2078 | // that's running. |
| 2079 | const bool allowConcurrentWithSoundTrigger = |
| 2080 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 2081 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2082 | // if capture is allowed, preempt currently active HOTWORD captures |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2083 | for (const auto& activeDesc : activeInputs) { |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 2084 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 2085 | continue; |
| 2086 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2087 | RecordClientVector activeHotwordClients = |
| 2088 | activeDesc->clientsList(true, AUDIO_SOURCE_HOTWORD); |
| 2089 | if (activeHotwordClients.size() > 0) { |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2090 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2091 | |
| 2092 | for (const auto& activeClient : activeHotwordClients) { |
| 2093 | *concurrency |= API_INPUT_CONCURRENCY_PREEMPT; |
| 2094 | sessions.add(activeClient->session()); |
| 2095 | closeClient(activeClient->portId()); |
| 2096 | ALOGV("%s input %d for HOTWORD preempting HOTWORD input %d", __FUNCTION__, |
| 2097 | input, activeDesc->mIoHandle); |
| 2098 | } |
| 2099 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2100 | inputDesc->setPreemptedSessions(sessions); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2104 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2105 | // Make sure we start with the correct silence state |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2106 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 2107 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2108 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 2109 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2110 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2111 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2112 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2113 | // primary HW module |
| 2114 | audio_devices_t device = getNewInputDevice(inputDesc); |
| 2115 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2116 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2117 | status_t status = inputDesc->start(); |
| 2118 | if (status != NO_ERROR) { |
| 2119 | inputDesc->setClientActive(client, false); |
| 2120 | return status; |
| 2121 | } |
| 2122 | |
| 2123 | if (inputDesc->activeCount() == 1) { |
| 2124 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2125 | if ((inputDesc->mPolicyMix != NULL) |
| 2126 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2127 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2128 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2129 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2130 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2131 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2132 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2133 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2134 | SoundTrigger::setCaptureState(true); |
| 2135 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2136 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2137 | // automatically enable the remote submix output when input is started if not |
| 2138 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2139 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2140 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2141 | String8 address = String8(""); |
| 2142 | if (inputDesc->mPolicyMix == NULL) { |
| 2143 | address = String8("0"); |
| 2144 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2145 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2146 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2147 | if (address != "") { |
| 2148 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2149 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2150 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2151 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2152 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2153 | } |
| 2154 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2155 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2156 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2157 | return NO_ERROR; |
| 2158 | } |
| 2159 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2160 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2161 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2162 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2163 | |
| 2164 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2165 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2166 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2167 | return BAD_VALUE; |
| 2168 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2169 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2170 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2171 | if (!client->active()) { |
| 2172 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2173 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2176 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2177 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2178 | inputDesc->stop(); |
| 2179 | if (inputDesc->isActive()) { |
| 2180 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2181 | } else { |
| 2182 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2183 | if ((inputDesc->mPolicyMix != NULL) |
| 2184 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2185 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2186 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2187 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2188 | |
| 2189 | // automatically disable the remote submix output when input is stopped if not |
| 2190 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2191 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2192 | String8 address = String8(""); |
| 2193 | if (inputDesc->mPolicyMix == NULL) { |
| 2194 | address = String8("0"); |
| 2195 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2196 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2197 | } |
| 2198 | if (address != "") { |
| 2199 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2200 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2201 | address, "remote-submix"); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | audio_devices_t device = inputDesc->mDevice; |
| 2206 | resetInputDevice(input); |
| 2207 | |
| 2208 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2209 | // primary HW module |
| 2210 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2211 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2212 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2213 | SoundTrigger::setCaptureState(false); |
| 2214 | } |
| 2215 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2216 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2217 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2218 | } |
| 2219 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2220 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2221 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2222 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2223 | |
| 2224 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2225 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2226 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2227 | return; |
| 2228 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2229 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2230 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2231 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2232 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2233 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2234 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2235 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2236 | if (inputDesc->getClientCount() > 0) { |
| 2237 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2238 | return; |
| 2239 | } |
| 2240 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2241 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2242 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2243 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2246 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2247 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2248 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2249 | |
| 2250 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2251 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2255 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2256 | { |
| 2257 | stopInput(portId); |
| 2258 | releaseInput(portId); |
| 2259 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2260 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2261 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2262 | bool patchRemoved = false; |
| 2263 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2264 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2265 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2266 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2267 | if (patch_index >= 0) { |
| 2268 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2269 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2270 | mAudioPatches.removeItemsAt(patch_index); |
| 2271 | patchRemoved = true; |
| 2272 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2273 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2274 | } |
| 2275 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2276 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2277 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2278 | |
| 2279 | if (patchRemoved) { |
| 2280 | mpClientInterface->onAudioPatchListUpdate(); |
| 2281 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2282 | } |
| 2283 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2284 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2285 | int indexMin, |
| 2286 | int indexMax) |
| 2287 | { |
| 2288 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2289 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2290 | |
| 2291 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2292 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2293 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2294 | continue; |
| 2295 | } |
| 2296 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2297 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2298 | } |
| 2299 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2300 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2301 | int index, |
| 2302 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2303 | { |
| 2304 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2305 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2306 | // app that has MODIFY_PHONE_STATE permission. |
| 2307 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2308 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2309 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2310 | return BAD_VALUE; |
| 2311 | } |
| 2312 | if (!audio_is_output_device(device)) { |
| 2313 | return BAD_VALUE; |
| 2314 | } |
| 2315 | |
| 2316 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2317 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2318 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2319 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2320 | stream, device, index); |
| 2321 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2322 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2323 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2324 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2325 | continue; |
| 2326 | } |
| 2327 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2328 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2329 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2330 | // update volume on all outputs and streams matching the following: |
| 2331 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2332 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2333 | // the requested device |
| 2334 | // - For non default requested device, currently selected device on the output is either the |
| 2335 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2336 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2337 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2338 | status_t status = NO_ERROR; |
| 2339 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2340 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2341 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2342 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2343 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2344 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2345 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2346 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2347 | continue; |
| 2348 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2349 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2350 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2351 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2352 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2353 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2354 | continue; |
| 2355 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2356 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2357 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2358 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2359 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2360 | } else { |
| 2361 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2362 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2363 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2364 | // rescale index before applying to curStream as ranges may be different for |
| 2365 | // stream and curStream |
| 2366 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2367 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2368 | //FIXME: workaround for truncated touch sounds |
| 2369 | // delayed volume change for system stream to be removed when the problem is |
| 2370 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2371 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2372 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2373 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2374 | if (volStatus != NO_ERROR) { |
| 2375 | status = volStatus; |
| 2376 | } |
| 2377 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2378 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2379 | } |
| 2380 | return status; |
| 2381 | } |
| 2382 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2383 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2384 | int *index, |
| 2385 | audio_devices_t device) |
| 2386 | { |
| 2387 | if (index == NULL) { |
| 2388 | return BAD_VALUE; |
| 2389 | } |
| 2390 | if (!audio_is_output_device(device)) { |
| 2391 | return BAD_VALUE; |
| 2392 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2393 | // 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] | 2394 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2395 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2396 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2397 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2398 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2399 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2400 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2401 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2402 | return NO_ERROR; |
| 2403 | } |
| 2404 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2405 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2406 | { |
| 2407 | // select one output among several suitable for global effects. |
| 2408 | // The priority is as follows: |
| 2409 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2410 | // AudioFlinger will invalidate the track and the offloaded output |
| 2411 | // will be closed causing the effect to be moved to a PCM output. |
| 2412 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2413 | // 3: The primary output |
| 2414 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2415 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2416 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2417 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2418 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2419 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2420 | if (outputs.size() == 0) { |
| 2421 | return AUDIO_IO_HANDLE_NONE; |
| 2422 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2423 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2424 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2425 | bool activeOnly = true; |
| 2426 | |
| 2427 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2428 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2429 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2430 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2431 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2432 | for (audio_io_handle_t output : outputs) { |
| 2433 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2434 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2435 | continue; |
| 2436 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2437 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2438 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2439 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2440 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2441 | } |
| 2442 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2443 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2444 | } |
| 2445 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2446 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2447 | } |
| 2448 | } |
| 2449 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2450 | output = outputOffloaded; |
| 2451 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2452 | output = outputDeepBuffer; |
| 2453 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2454 | output = outputPrimary; |
| 2455 | } else { |
| 2456 | output = outputs[0]; |
| 2457 | } |
| 2458 | activeOnly = false; |
| 2459 | } |
| 2460 | |
| 2461 | if (output != mMusicEffectOutput) { |
| 2462 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2463 | mMusicEffectOutput = output; |
| 2464 | } |
| 2465 | |
| 2466 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2467 | return output; |
| 2468 | } |
| 2469 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2470 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2471 | { |
| 2472 | return selectOutputForMusicEffects(); |
| 2473 | } |
| 2474 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2475 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2476 | audio_io_handle_t io, |
| 2477 | uint32_t strategy, |
| 2478 | int session, |
| 2479 | int id) |
| 2480 | { |
| 2481 | ssize_t index = mOutputs.indexOfKey(io); |
| 2482 | if (index < 0) { |
| 2483 | index = mInputs.indexOfKey(io); |
| 2484 | if (index < 0) { |
| 2485 | ALOGW("registerEffect() unknown io %d", io); |
| 2486 | return INVALID_OPERATION; |
| 2487 | } |
| 2488 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2489 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2490 | } |
| 2491 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2492 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2493 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2494 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2495 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2496 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2497 | continue; |
| 2498 | } |
| 2499 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2500 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2501 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2505 | { |
| 2506 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2507 | } |
| 2508 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2509 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2510 | { |
| 2511 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2512 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2513 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2514 | return true; |
| 2515 | } |
| 2516 | } |
| 2517 | return false; |
| 2518 | } |
| 2519 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2520 | // Register a list of custom mixes with their attributes and format. |
| 2521 | // When a mix is registered, corresponding input and output profiles are |
| 2522 | // added to the remote submix hw module. The profile contains only the |
| 2523 | // parameters (sampling rate, format...) specified by the mix. |
| 2524 | // The corresponding input remote submix device is also connected. |
| 2525 | // |
| 2526 | // When a remote submix device is connected, the address is checked to select the |
| 2527 | // appropriate profile and the corresponding input or output stream is opened. |
| 2528 | // |
| 2529 | // When capture starts, getInputForAttr() will: |
| 2530 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2531 | // - 2 if none found, getDeviceForInputSource() will: |
| 2532 | // - 2.1 look for a mix matching the attributes source |
| 2533 | // - 2.2 if none found, default to device selection by policy rules |
| 2534 | // At this time, the corresponding output remote submix device is also connected |
| 2535 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2536 | // after AudioTracks are invalidated |
| 2537 | // |
| 2538 | // When playback starts, getOutputForAttr() will: |
| 2539 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2540 | // - 2 if none found, look for a mix matching the attributes usage |
| 2541 | // - 3 if none found, default to device and output selection by policy rules. |
| 2542 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2543 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2544 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2545 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2546 | status_t res = NO_ERROR; |
| 2547 | |
| 2548 | sp<HwModule> rSubmixModule; |
| 2549 | // examine each mix's route type |
| 2550 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2551 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2552 | // 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] | 2553 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2554 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2555 | break; |
| 2556 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2557 | 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] | 2558 | 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] | 2559 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2560 | rSubmixModule = mHwModules.getModuleFromName( |
| 2561 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2562 | if (rSubmixModule == 0) { |
| 2563 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2564 | i); |
| 2565 | res = INVALID_OPERATION; |
| 2566 | break; |
| 2567 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2568 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2569 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2570 | String8 address = mix.mDeviceAddress; |
| 2571 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2572 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2573 | } else { |
| 2574 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2575 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2576 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2577 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2578 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2579 | res = INVALID_OPERATION; |
| 2580 | break; |
| 2581 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2582 | audio_config_t outputConfig = mix.mFormat; |
| 2583 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2584 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2585 | // stereo and let audio flinger do the channel conversion if needed. |
| 2586 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2587 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2588 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2589 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2590 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2591 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2592 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2593 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2594 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2595 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2596 | address.string(), "remote-submix"); |
| 2597 | } else { |
| 2598 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2599 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2600 | address.string(), "remote-submix"); |
| 2601 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2602 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2603 | String8 address = mix.mDeviceAddress; |
| 2604 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2605 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2606 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2607 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2608 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2609 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2610 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2611 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2612 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2613 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2614 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2615 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2616 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2617 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2618 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2619 | } else { |
| 2620 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2621 | } |
| 2622 | break; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | if (res != NO_ERROR) { |
| 2627 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2628 | i, device, address.string()); |
| 2629 | res = INVALID_OPERATION; |
| 2630 | break; |
| 2631 | } else if (!foundOutput) { |
| 2632 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2633 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2634 | res = INVALID_OPERATION; |
| 2635 | break; |
| 2636 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2637 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2638 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2639 | if (res != NO_ERROR) { |
| 2640 | unregisterPolicyMixes(mixes); |
| 2641 | } |
| 2642 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2646 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2647 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2648 | status_t res = NO_ERROR; |
| 2649 | sp<HwModule> rSubmixModule; |
| 2650 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2651 | for (const auto& mix : mixes) { |
| 2652 | 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] | 2653 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2654 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2655 | rSubmixModule = mHwModules.getModuleFromName( |
| 2656 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2657 | if (rSubmixModule == 0) { |
| 2658 | res = INVALID_OPERATION; |
| 2659 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2660 | } |
| 2661 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2662 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2663 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2664 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2665 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2666 | res = INVALID_OPERATION; |
| 2667 | continue; |
| 2668 | } |
| 2669 | |
| 2670 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2671 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2672 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2673 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2674 | address.string(), "remote-submix"); |
| 2675 | } |
| 2676 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2677 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2678 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2679 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2680 | address.string(), "remote-submix"); |
| 2681 | } |
| 2682 | rSubmixModule->removeOutputProfile(address); |
| 2683 | rSubmixModule->removeInputProfile(address); |
| 2684 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2685 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2686 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2687 | res = INVALID_OPERATION; |
| 2688 | continue; |
| 2689 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2690 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2691 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2692 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2693 | } |
| 2694 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2695 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2696 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2697 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2698 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2699 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2700 | std::string stateLiteral; |
| 2701 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2702 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2703 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2704 | "communications", "media", "record", "dock", "system", |
| 2705 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2706 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2707 | 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] | 2708 | dst->appendFormat(" Force use for %s: %d\n", |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2709 | forceUses[i], mEngine->getForceUse(i)); |
| 2710 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2711 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2712 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2713 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2714 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2715 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2716 | mHwModulesAll.dump(dst); |
| 2717 | mOutputs.dump(dst); |
| 2718 | mInputs.dump(dst); |
| 2719 | mVolumeCurves->dump(dst); |
| 2720 | mEffects.dump(dst); |
| 2721 | mAudioPatches.dump(dst); |
| 2722 | mPolicyMixes.dump(dst); |
| 2723 | mAudioSources.dump(dst); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2724 | if (!mSurroundFormats.empty()) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2725 | dst->append("\nEnabled Surround Formats:\n"); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2726 | size_t i = 0; |
| 2727 | for (const auto& fmt : mSurroundFormats) { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2728 | dst->append(i++ == 0 ? " " : ", "); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2729 | std::string sfmt; |
| 2730 | FormatConverter::toString(fmt, sfmt); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2731 | dst->append(sfmt.c_str()); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2732 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2733 | dst->append("\n"); |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 2734 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | status_t AudioPolicyManager::dump(int fd) |
| 2738 | { |
| 2739 | String8 result; |
| 2740 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2741 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2742 | return NO_ERROR; |
| 2743 | } |
| 2744 | |
| 2745 | // This function checks for the parameters which can be offloaded. |
| 2746 | // This can be enhanced depending on the capability of the DSP and policy |
| 2747 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2748 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2749 | { |
| 2750 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2751 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2752 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2753 | offloadInfo.format, |
| 2754 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2755 | offloadInfo.has_video); |
| 2756 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2757 | if (mMasterMono) { |
| 2758 | return false; // no offloading if mono is set. |
| 2759 | } |
| 2760 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2761 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2762 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2763 | ALOGV("offload disabled by audio.offload.disable"); |
| 2764 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | // Check if stream type is music, then only allow offload as of now. |
| 2768 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2769 | { |
| 2770 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2771 | return false; |
| 2772 | } |
| 2773 | |
| 2774 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2775 | const bool allowOffloadWithVideo = |
| 2776 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2777 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2778 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2779 | return false; |
| 2780 | } |
| 2781 | |
| 2782 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2783 | const int min_duration_secs = property_get_int32( |
| 2784 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2785 | if (min_duration_secs >= 0) { |
| 2786 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2787 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2788 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2789 | return false; |
| 2790 | } |
| 2791 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2792 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2793 | return false; |
| 2794 | } |
| 2795 | |
| 2796 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2797 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2798 | // detects there is an active non offloadable effect. |
| 2799 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2800 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2801 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2802 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2803 | return false; |
| 2804 | } |
| 2805 | |
| 2806 | // See if there is a profile to support this. |
| 2807 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2808 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2809 | offloadInfo.sample_rate, |
| 2810 | offloadInfo.format, |
| 2811 | offloadInfo.channel_mask, |
| 2812 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2813 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2814 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2815 | } |
| 2816 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2817 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2818 | audio_port_type_t type, |
| 2819 | unsigned int *num_ports, |
| 2820 | struct audio_port *ports, |
| 2821 | unsigned int *generation) |
| 2822 | { |
| 2823 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2824 | generation == NULL) { |
| 2825 | return BAD_VALUE; |
| 2826 | } |
| 2827 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2828 | if (ports == NULL) { |
| 2829 | *num_ports = 0; |
| 2830 | } |
| 2831 | |
| 2832 | size_t portsWritten = 0; |
| 2833 | size_t portsMax = *num_ports; |
| 2834 | *num_ports = 0; |
| 2835 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2836 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2837 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2838 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2839 | for (const auto& dev : mAvailableOutputDevices) { |
| 2840 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2841 | continue; |
| 2842 | } |
| 2843 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2844 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2845 | } |
| 2846 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2847 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2848 | } |
| 2849 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2850 | for (const auto& dev : mAvailableInputDevices) { |
| 2851 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2852 | continue; |
| 2853 | } |
| 2854 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2855 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2856 | } |
| 2857 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2858 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2862 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2863 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2864 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2865 | } |
| 2866 | *num_ports += mInputs.size(); |
| 2867 | } |
| 2868 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2869 | size_t numOutputs = 0; |
| 2870 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2871 | if (!mOutputs[i]->isDuplicated()) { |
| 2872 | numOutputs++; |
| 2873 | if (portsWritten < portsMax) { |
| 2874 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2875 | } |
| 2876 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2877 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2878 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2879 | } |
| 2880 | } |
| 2881 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2882 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2883 | return NO_ERROR; |
| 2884 | } |
| 2885 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2886 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2887 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2888 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2889 | return BAD_VALUE; |
| 2890 | } |
| 2891 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2892 | if (dev != 0) { |
| 2893 | dev->toAudioPort(port); |
| 2894 | return NO_ERROR; |
| 2895 | } |
| 2896 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2897 | if (dev != 0) { |
| 2898 | dev->toAudioPort(port); |
| 2899 | return NO_ERROR; |
| 2900 | } |
| 2901 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2902 | if (out != 0) { |
| 2903 | out->toAudioPort(port); |
| 2904 | return NO_ERROR; |
| 2905 | } |
| 2906 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2907 | if (in != 0) { |
| 2908 | in->toAudioPort(port); |
| 2909 | return NO_ERROR; |
| 2910 | } |
| 2911 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2912 | } |
| 2913 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2914 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2915 | audio_patch_handle_t *handle, |
| 2916 | uid_t uid) |
| 2917 | { |
| 2918 | ALOGV("createAudioPatch()"); |
| 2919 | |
| 2920 | if (handle == NULL || patch == NULL) { |
| 2921 | return BAD_VALUE; |
| 2922 | } |
| 2923 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2924 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2925 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2926 | return BAD_VALUE; |
| 2927 | } |
| 2928 | // only one source per audio patch supported for now |
| 2929 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2930 | return INVALID_OPERATION; |
| 2931 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2932 | |
| 2933 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2934 | return INVALID_OPERATION; |
| 2935 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2936 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2937 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2938 | return INVALID_OPERATION; |
| 2939 | } |
| 2940 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2941 | |
| 2942 | sp<AudioPatch> patchDesc; |
| 2943 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2944 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2945 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2946 | patch->sources[0].role, |
| 2947 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2948 | #if LOG_NDEBUG == 0 |
| 2949 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2950 | 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] | 2951 | patch->sinks[i].role, |
| 2952 | patch->sinks[i].type); |
| 2953 | } |
| 2954 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2955 | |
| 2956 | if (index >= 0) { |
| 2957 | patchDesc = mAudioPatches.valueAt(index); |
| 2958 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2959 | mUidCached, patchDesc->mUid, uid); |
| 2960 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2961 | return INVALID_OPERATION; |
| 2962 | } |
| 2963 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2964 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2965 | } |
| 2966 | |
| 2967 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2968 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2969 | if (outputDesc == NULL) { |
| 2970 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2971 | return BAD_VALUE; |
| 2972 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2973 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2974 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2975 | if (patchDesc != 0) { |
| 2976 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2977 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2978 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2979 | return BAD_VALUE; |
| 2980 | } |
| 2981 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2982 | DeviceVector devices; |
| 2983 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2984 | // Only support mix to devices connection |
| 2985 | // TODO add support for mix to mix connection |
| 2986 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2987 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2988 | return INVALID_OPERATION; |
| 2989 | } |
| 2990 | sp<DeviceDescriptor> devDesc = |
| 2991 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2992 | if (devDesc == 0) { |
| 2993 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2994 | return BAD_VALUE; |
| 2995 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2996 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2997 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2998 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2999 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3000 | NULL, // updatedSamplingRate |
| 3001 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3002 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3003 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3004 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3005 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3006 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 3007 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3008 | return INVALID_OPERATION; |
| 3009 | } |
| 3010 | devices.add(devDesc); |
| 3011 | } |
| 3012 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3013 | return INVALID_OPERATION; |
| 3014 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3015 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3016 | // TODO: reconfigure output format and channels here |
| 3017 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3018 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3019 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3020 | index = mAudioPatches.indexOfKey(*handle); |
| 3021 | if (index >= 0) { |
| 3022 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3023 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3024 | } |
| 3025 | patchDesc = mAudioPatches.valueAt(index); |
| 3026 | patchDesc->mUid = uid; |
| 3027 | ALOGV("createAudioPatch() success"); |
| 3028 | } else { |
| 3029 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3030 | return INVALID_OPERATION; |
| 3031 | } |
| 3032 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3033 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3034 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3035 | // only one sink supported when connecting an input device to a mix |
| 3036 | if (patch->num_sinks > 1) { |
| 3037 | return INVALID_OPERATION; |
| 3038 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3039 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3040 | if (inputDesc == NULL) { |
| 3041 | return BAD_VALUE; |
| 3042 | } |
| 3043 | if (patchDesc != 0) { |
| 3044 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3045 | return BAD_VALUE; |
| 3046 | } |
| 3047 | } |
| 3048 | sp<DeviceDescriptor> devDesc = |
| 3049 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 3050 | if (devDesc == 0) { |
| 3051 | return BAD_VALUE; |
| 3052 | } |
| 3053 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3054 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3055 | devDesc->mAddress, |
| 3056 | patch->sinks[0].sample_rate, |
| 3057 | NULL, /*updatedSampleRate*/ |
| 3058 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3059 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3060 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3061 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3062 | // FIXME for the parameter type, |
| 3063 | // and the NONE |
| 3064 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3065 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3066 | return INVALID_OPERATION; |
| 3067 | } |
| 3068 | // TODO: reconfigure output format and channels here |
| 3069 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3070 | devDesc->type(), inputDesc->mIoHandle); |
| 3071 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3072 | index = mAudioPatches.indexOfKey(*handle); |
| 3073 | if (index >= 0) { |
| 3074 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3075 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3076 | } |
| 3077 | patchDesc = mAudioPatches.valueAt(index); |
| 3078 | patchDesc->mUid = uid; |
| 3079 | ALOGV("createAudioPatch() success"); |
| 3080 | } else { |
| 3081 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3082 | return INVALID_OPERATION; |
| 3083 | } |
| 3084 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3085 | // device to device connection |
| 3086 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3087 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3088 | return BAD_VALUE; |
| 3089 | } |
| 3090 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3091 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3092 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3093 | if (srcDeviceDesc == 0) { |
| 3094 | return BAD_VALUE; |
| 3095 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3096 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3097 | //update source and sink with our own data as the data passed in the patch may |
| 3098 | // be incomplete. |
| 3099 | struct audio_patch newPatch = *patch; |
| 3100 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3101 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3102 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3103 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3104 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3105 | return INVALID_OPERATION; |
| 3106 | } |
| 3107 | |
| 3108 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3109 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3110 | if (sinkDeviceDesc == 0) { |
| 3111 | return BAD_VALUE; |
| 3112 | } |
| 3113 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 3114 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3115 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3116 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3117 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3118 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3119 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3120 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3121 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3122 | return INVALID_OPERATION; |
| 3123 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3124 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3125 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3126 | // if the sink device is reachable via an opened output stream, request to go via |
| 3127 | // this output stream by adding a second source to the patch description |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 3128 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3129 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3130 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3131 | if (outputDesc->isDuplicated()) { |
| 3132 | return INVALID_OPERATION; |
| 3133 | } |
| 3134 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3135 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3136 | newPatch.num_sources = 2; |
| 3137 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3138 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3139 | } |
| 3140 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3141 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3142 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3143 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3144 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3145 | status); |
| 3146 | return INVALID_OPERATION; |
| 3147 | } |
| 3148 | } else { |
| 3149 | return BAD_VALUE; |
| 3150 | } |
| 3151 | } else { |
| 3152 | return BAD_VALUE; |
| 3153 | } |
| 3154 | return NO_ERROR; |
| 3155 | } |
| 3156 | |
| 3157 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3158 | uid_t uid) |
| 3159 | { |
| 3160 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3161 | |
| 3162 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3163 | |
| 3164 | if (index < 0) { |
| 3165 | return BAD_VALUE; |
| 3166 | } |
| 3167 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3168 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3169 | mUidCached, patchDesc->mUid, uid); |
| 3170 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3171 | return INVALID_OPERATION; |
| 3172 | } |
| 3173 | |
| 3174 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3175 | patchDesc->mUid = mUidCached; |
| 3176 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3177 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3178 | if (outputDesc == NULL) { |
| 3179 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3180 | return BAD_VALUE; |
| 3181 | } |
| 3182 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3183 | setOutputDevice(outputDesc, |
| 3184 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3185 | true, |
| 3186 | 0, |
| 3187 | NULL); |
| 3188 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3189 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3190 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3191 | if (inputDesc == NULL) { |
| 3192 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3193 | return BAD_VALUE; |
| 3194 | } |
| 3195 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3196 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3197 | true, |
| 3198 | NULL); |
| 3199 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3200 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3201 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3202 | status, patchDesc->mAfPatchHandle); |
| 3203 | removeAudioPatch(patchDesc->mHandle); |
| 3204 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3205 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3206 | } else { |
| 3207 | return BAD_VALUE; |
| 3208 | } |
| 3209 | } else { |
| 3210 | return BAD_VALUE; |
| 3211 | } |
| 3212 | return NO_ERROR; |
| 3213 | } |
| 3214 | |
| 3215 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3216 | struct audio_patch *patches, |
| 3217 | unsigned int *generation) |
| 3218 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3219 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3220 | return BAD_VALUE; |
| 3221 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3222 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3223 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3224 | } |
| 3225 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3226 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3227 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3228 | ALOGV("setAudioPortConfig()"); |
| 3229 | |
| 3230 | if (config == NULL) { |
| 3231 | return BAD_VALUE; |
| 3232 | } |
| 3233 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3234 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3235 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3236 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3237 | } |
| 3238 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3239 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3240 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3241 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3242 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3243 | if (outputDesc == NULL) { |
| 3244 | return BAD_VALUE; |
| 3245 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3246 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3247 | "setAudioPortConfig() called on duplicated output %d", |
| 3248 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3249 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3250 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3251 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3252 | if (inputDesc == NULL) { |
| 3253 | return BAD_VALUE; |
| 3254 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3255 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3256 | } else { |
| 3257 | return BAD_VALUE; |
| 3258 | } |
| 3259 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3260 | sp<DeviceDescriptor> deviceDesc; |
| 3261 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3262 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3263 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3264 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3265 | } else { |
| 3266 | return BAD_VALUE; |
| 3267 | } |
| 3268 | if (deviceDesc == NULL) { |
| 3269 | return BAD_VALUE; |
| 3270 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3271 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3272 | } else { |
| 3273 | return BAD_VALUE; |
| 3274 | } |
| 3275 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3276 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3277 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3278 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3279 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3280 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3281 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3282 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3283 | if (status != NO_ERROR) { |
| 3284 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3285 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3286 | |
| 3287 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3288 | } |
| 3289 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3290 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3291 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3292 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3293 | clearAudioPatches(uid); |
| 3294 | clearSessionRoutes(uid); |
| 3295 | } |
| 3296 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3297 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3298 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3299 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3300 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3301 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3302 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | } |
| 3306 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3307 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3308 | audio_io_handle_t ouptutToSkip) |
| 3309 | { |
| 3310 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3311 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3312 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3313 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3314 | continue; |
| 3315 | } |
| 3316 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3317 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3318 | continue; |
| 3319 | } |
| 3320 | // If the default device for this strategy is on another output mix, |
| 3321 | // invalidate all tracks in this strategy to force re connection. |
| 3322 | // Otherwise select new device on the output mix. |
| 3323 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3324 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3325 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3326 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3327 | } |
| 3328 | } |
| 3329 | } else { |
| 3330 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3331 | setOutputDevice(outputDesc, newDevice, false); |
| 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3337 | { |
| 3338 | // remove output routes associated with this uid |
| 3339 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3340 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3341 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3342 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3343 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3344 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3345 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3346 | } |
| 3347 | } |
| 3348 | } |
| 3349 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3350 | for (const auto& strategy : affectedStrategies) { |
| 3351 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3352 | } |
| 3353 | |
| 3354 | // remove input routes associated with this uid |
| 3355 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3356 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3357 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3358 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3359 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3360 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3361 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | } |
| 3365 | // reroute inputs if necessary |
| 3366 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3367 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3368 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3369 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3370 | inputsToClose.add(inputDesc->mIoHandle); |
| 3371 | } |
| 3372 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3373 | for (const auto& input : inputsToClose) { |
| 3374 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3378 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3379 | { |
| 3380 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3381 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3382 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3383 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3384 | } |
| 3385 | } |
| 3386 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3387 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3388 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3389 | audio_io_handle_t *ioHandle, |
| 3390 | audio_devices_t *device) |
| 3391 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3392 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3393 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3394 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3395 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3396 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3397 | } |
| 3398 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3399 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3400 | const audio_attributes_t *attributes, |
| 3401 | audio_port_handle_t *portId, |
| 3402 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3403 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3404 | ALOGV("%s", __FUNCTION__); |
| 3405 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3406 | |
| 3407 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3408 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3409 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3410 | return BAD_VALUE; |
| 3411 | } |
| 3412 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3413 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3414 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3415 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3416 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3417 | return INVALID_OPERATION; |
| 3418 | } |
| 3419 | |
| 3420 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3421 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3422 | String8(source->ext.device.address)); |
| 3423 | if (srcDeviceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3424 | 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] | 3425 | return BAD_VALUE; |
| 3426 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3427 | |
| 3428 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3429 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3430 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3431 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3432 | |
| 3433 | sp<SourceClientDescriptor> sourceDesc = |
| 3434 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDeviceDesc, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3435 | streamTypefromAttributesInt(attributes), |
| 3436 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3437 | |
| 3438 | status_t status = connectAudioSource(sourceDesc); |
| 3439 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3440 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3441 | } |
| 3442 | return status; |
| 3443 | } |
| 3444 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3445 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3446 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3447 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3448 | |
| 3449 | // make sure we only have one patch per source. |
| 3450 | disconnectAudioSource(sourceDesc); |
| 3451 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3452 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3453 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3454 | audio_stream_type_t stream = sourceDesc->stream(); |
| 3455 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3456 | |
| 3457 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3458 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3459 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3460 | |
| 3461 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3462 | |
| 3463 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3464 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3465 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3466 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3467 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3468 | // create patch between src device and output device |
| 3469 | // create Hwoutput and add to mHwOutputs |
| 3470 | } else { |
| 3471 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame^] | 3472 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3473 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3474 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3475 | return INVALID_OPERATION; |
| 3476 | } |
| 3477 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3478 | if (outputDesc->isDuplicated()) { |
| 3479 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3480 | return INVALID_OPERATION; |
| 3481 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3482 | status_t status = outputDesc->start(); |
| 3483 | if (status != NO_ERROR) { |
| 3484 | return status; |
| 3485 | } |
| 3486 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3487 | // create a special patch with no sink and two sources: |
| 3488 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3489 | // be connected as well as the stream type for volume control |
| 3490 | // - the sink is defined by whatever output device is currently selected for the output |
| 3491 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3492 | PatchBuilder patchBuilder; |
| 3493 | patchBuilder.addSource(srcDeviceDesc).addSource(outputDesc, { .stream = stream }); |
| 3494 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3495 | &afPatchHandle, |
| 3496 | 0); |
| 3497 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3498 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3499 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3500 | if (status != NO_ERROR) { |
| 3501 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3502 | __FUNCTION__, status); |
| 3503 | return INVALID_OPERATION; |
| 3504 | } |
| 3505 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3506 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3507 | |
| 3508 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3509 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3510 | return status; |
| 3511 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3512 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3513 | if (delayMs != 0) { |
| 3514 | usleep(delayMs * 1000); |
| 3515 | } |
| 3516 | } |
| 3517 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3518 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3519 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3520 | |
| 3521 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3524 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3525 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3526 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3527 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3528 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3529 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3530 | return BAD_VALUE; |
| 3531 | } |
| 3532 | status_t status = disconnectAudioSource(sourceDesc); |
| 3533 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3534 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3535 | return status; |
| 3536 | } |
| 3537 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3538 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3539 | { |
| 3540 | if (mMasterMono == mono) { |
| 3541 | return NO_ERROR; |
| 3542 | } |
| 3543 | mMasterMono = mono; |
| 3544 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3545 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3546 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3547 | // |
| 3548 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3549 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3550 | // play back offloaded. |
| 3551 | if (mMasterMono) { |
| 3552 | Vector<audio_io_handle_t> offloaded; |
| 3553 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3554 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3555 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3556 | offloaded.push(desc->mIoHandle); |
| 3557 | } |
| 3558 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3559 | for (const auto& handle : offloaded) { |
| 3560 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3561 | } |
| 3562 | } |
| 3563 | // update master mono for all remaining outputs |
| 3564 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3565 | updateMono(mOutputs.keyAt(i)); |
| 3566 | } |
| 3567 | return NO_ERROR; |
| 3568 | } |
| 3569 | |
| 3570 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3571 | { |
| 3572 | *mono = mMasterMono; |
| 3573 | return NO_ERROR; |
| 3574 | } |
| 3575 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3576 | float AudioPolicyManager::getStreamVolumeDB( |
| 3577 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3578 | { |
| 3579 | return computeVolume(stream, index, device); |
| 3580 | } |
| 3581 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3582 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3583 | audio_format_t *surroundFormats, |
| 3584 | bool *surroundFormatsEnabled, |
| 3585 | bool reported) |
| 3586 | { |
| 3587 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3588 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3589 | return BAD_VALUE; |
| 3590 | } |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3591 | ALOGV("getSurroundFormats() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p" |
| 3592 | " reported %d", *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3593 | |
| 3594 | // Only return value if there is HDMI output. |
| 3595 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
| 3596 | return INVALID_OPERATION; |
| 3597 | } |
| 3598 | |
| 3599 | size_t formatsWritten = 0; |
| 3600 | size_t formatsMax = *numSurroundFormats; |
| 3601 | *numSurroundFormats = 0; |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3602 | std::unordered_set<audio_format_t> formats; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3603 | if (reported) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3604 | // Return formats from HDMI profiles, that have already been resolved by |
| 3605 | // checkOutputsForDevice(). |
| 3606 | DeviceVector hdmiOutputDevs = mAvailableOutputDevices.getDevicesFromTypeMask( |
| 3607 | AUDIO_DEVICE_OUT_HDMI); |
| 3608 | for (size_t i = 0; i < hdmiOutputDevs.size(); i++) { |
| 3609 | FormatVector supportedFormats = |
| 3610 | hdmiOutputDevs[i]->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3611 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3612 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3613 | formats.insert(supportedFormats[j]); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3614 | } else { |
| 3615 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3616 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3617 | formats.insert(pair.first); |
| 3618 | break; |
| 3619 | } |
| 3620 | } |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3621 | } |
| 3622 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3623 | } |
| 3624 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3625 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3626 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3627 | } |
| 3628 | } |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3629 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3630 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3631 | surroundFormats[formatsWritten] = format; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3632 | bool formatEnabled = false; |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3633 | if (mConfig.getSurroundFormats().count(format) == 0) { |
| 3634 | // Check sub-formats |
| 3635 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3636 | for (const auto& subformat : pair.second) { |
| 3637 | formatEnabled = mSurroundFormats.count(subformat) != 0; |
| 3638 | if (formatEnabled) break; |
| 3639 | } |
| 3640 | if (formatEnabled) break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3641 | } |
| 3642 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3643 | formatEnabled = mSurroundFormats.count(format) != 0; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3644 | } |
| 3645 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3646 | } |
| 3647 | (*numSurroundFormats)++; |
| 3648 | } |
| 3649 | return NO_ERROR; |
| 3650 | } |
| 3651 | |
| 3652 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3653 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3654 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3655 | // Check if audio format is a surround formats. |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3656 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3657 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3658 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3659 | return BAD_VALUE; |
| 3660 | } |
| 3661 | |
| 3662 | // Should only be called when MANUAL. |
| 3663 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3664 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 3665 | if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3666 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3667 | return INVALID_OPERATION; |
| 3668 | } |
| 3669 | |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3670 | if ((mSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3671 | return NO_ERROR; |
| 3672 | } |
| 3673 | |
| 3674 | // The operation is valid only when there is HDMI output available. |
| 3675 | if ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_HDMI) == 0) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3676 | ALOGW("%s() no HDMI out devices found", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3677 | return INVALID_OPERATION; |
| 3678 | } |
| 3679 | |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3680 | std::unordered_set<audio_format_t> surroundFormatsBackup(mSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3681 | if (enabled) { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3682 | mSurroundFormats.insert(audioFormat); |
| 3683 | for (const auto& subFormat : formatIter->second) { |
| 3684 | mSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3685 | } |
| 3686 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3687 | mSurroundFormats.erase(audioFormat); |
| 3688 | for (const auto& subFormat : formatIter->second) { |
| 3689 | mSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3690 | } |
| 3691 | } |
| 3692 | |
| 3693 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3694 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3695 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3696 | AUDIO_DEVICE_OUT_HDMI); |
| 3697 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3698 | // Simulate reconnection to update enabled surround sound formats. |
| 3699 | String8 address = hdmiOutputDevices[i]->mAddress; |
| 3700 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3701 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3702 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3703 | address.c_str(), |
| 3704 | name.c_str()); |
| 3705 | if (status != NO_ERROR) { |
| 3706 | continue; |
| 3707 | } |
| 3708 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3709 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3710 | address.c_str(), |
| 3711 | name.c_str()); |
| 3712 | profileUpdated |= (status == NO_ERROR); |
| 3713 | } |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3714 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3715 | AUDIO_DEVICE_IN_HDMI); |
| 3716 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3717 | // Simulate reconnection to update enabled surround sound formats. |
| 3718 | String8 address = hdmiInputDevices[i]->mAddress; |
| 3719 | String8 name = hdmiInputDevices[i]->getName(); |
| 3720 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3721 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3722 | address.c_str(), |
| 3723 | name.c_str()); |
| 3724 | if (status != NO_ERROR) { |
| 3725 | continue; |
| 3726 | } |
| 3727 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3728 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3729 | address.c_str(), |
| 3730 | name.c_str()); |
| 3731 | profileUpdated |= (status == NO_ERROR); |
| 3732 | } |
| 3733 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3734 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3735 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3736 | mSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3740 | } |
| 3741 | |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3742 | void AudioPolicyManager::setAppState(uid_t uid, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3743 | { |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3744 | Vector<sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 3745 | bool silenced = state == APP_STATE_IDLE; |
| 3746 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3747 | ALOGV("AudioPolicyManager:setRecordSilenced(uid:%d, silenced:%d)", uid, silenced); |
| 3748 | |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3749 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 3750 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3751 | RecordClientVector clients = activeDesc->clientsList(true /*activeOnly*/); |
| 3752 | for (const auto& client : clients) { |
| 3753 | if (uid == client->uid()) { |
| 3754 | client->setSilenced(silenced); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3755 | } |
| 3756 | } |
| 3757 | } |
| 3758 | } |
| 3759 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3760 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3761 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3762 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3763 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3764 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3765 | if (patchDesc == 0) { |
| 3766 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3767 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3768 | return BAD_VALUE; |
| 3769 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3770 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3771 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3772 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3773 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3774 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3775 | if (status == NO_ERROR) { |
| 3776 | swOutputDesc->stop(); |
| 3777 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3778 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3779 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3780 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3781 | if (hwOutputDesc != 0) { |
| 3782 | // release patch between src device and output device |
| 3783 | // close Hwoutput and remove from mHwOutputs |
| 3784 | } else { |
| 3785 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3786 | } |
| 3787 | } |
| 3788 | return NO_ERROR; |
| 3789 | } |
| 3790 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3791 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3792 | audio_io_handle_t output, routing_strategy strategy) |
| 3793 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3794 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3795 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3796 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3797 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3798 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3799 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3800 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3801 | source = sourceDesc; |
| 3802 | break; |
| 3803 | } |
| 3804 | } |
| 3805 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3806 | } |
| 3807 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3809 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3810 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3811 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3812 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3813 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3816 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3817 | static const char *kConfigLocationList[] = |
| 3818 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3819 | static const int kConfigLocationListSize = |
| 3820 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3821 | |
| 3822 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3823 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3824 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3825 | status_t ret; |
| 3826 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3827 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3828 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3829 | // A2DP offload supported but disabled: try to use special XML file |
| 3830 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3831 | } |
| 3832 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3833 | |
| 3834 | for (const char* fileName : fileNames) { |
| 3835 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3836 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3837 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3838 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3839 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3840 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3841 | return ret; |
| 3842 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3843 | } |
| 3844 | } |
| 3845 | return ret; |
| 3846 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3847 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3848 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3849 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3850 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3851 | 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] | 3852 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3853 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3854 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3855 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3856 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3857 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3858 | mAudioPortGeneration(1), |
| 3859 | mBeaconMuteRefCount(0), |
| 3860 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3861 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3862 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3863 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3864 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3865 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3866 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3867 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3868 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3869 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3870 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3871 | { |
| 3872 | loadConfig(); |
| 3873 | initialize(); |
| 3874 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3875 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3876 | // This check is to catch any legacy platform updating to Q without having |
| 3877 | // switched to XML since its deprecation on O. |
| 3878 | // TODO: after Q release, remove this check and flag as XML is now the only |
| 3879 | // option and all legacy platform should have transitioned to XML. |
| 3880 | #ifndef USE_XML_AUDIO_POLICY_CONF |
| 3881 | #error Audio policy no longer supports legacy .conf configuration format |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3882 | #endif |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3883 | |
| 3884 | void AudioPolicyManager::loadConfig() { |
| 3885 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3886 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3887 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3888 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3889 | } |
| 3890 | |
| 3891 | status_t AudioPolicyManager::initialize() { |
| 3892 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3893 | |
| 3894 | // 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] | 3895 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3896 | if (!engineInstance) { |
| 3897 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3898 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3899 | } |
| 3900 | // Retrieve the Policy Manager Interface |
| 3901 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3902 | if (mEngine == NULL) { |
| 3903 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3904 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3905 | } |
| 3906 | mEngine->setObserver(this); |
| 3907 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3908 | if (status != NO_ERROR) { |
| 3909 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3910 | return status; |
| 3911 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3912 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3913 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3915 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3916 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3917 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3918 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3919 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3920 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3921 | continue; |
| 3922 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3923 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3924 | // open all output streams needed to access attached devices |
| 3925 | // except for direct output streams that are only opened when they are actually |
| 3926 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3927 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3928 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3929 | if (!outProfile->canOpenNewIo()) { |
| 3930 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3931 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3932 | continue; |
| 3933 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3934 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3935 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3936 | continue; |
| 3937 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3938 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3939 | mTtsOutputAvailable = true; |
| 3940 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3941 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3942 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3943 | continue; |
| 3944 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3945 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3946 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3947 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3948 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3949 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3950 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3951 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3952 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3953 | if ((profileType & outputDeviceTypes) == 0) { |
| 3954 | continue; |
| 3955 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3956 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3957 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3958 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3959 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromTypeMask( |
| 3960 | profileType); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3961 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3962 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3963 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3964 | status_t status = outputDesc->open(nullptr, profileType, address, |
| 3965 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3966 | |
| 3967 | if (status != NO_ERROR) { |
| 3968 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3969 | outputDesc->mDevice, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3970 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3971 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3972 | for (const auto& dev : supportedDevices) { |
| 3973 | ssize_t index = mAvailableOutputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3974 | // 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] | 3975 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3976 | mAvailableOutputDevices[index]->attach(hwModule); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3977 | } |
| 3978 | } |
| 3979 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3980 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3981 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3982 | } |
| 3983 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3984 | setOutputDevice(outputDesc, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3985 | profileType, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3986 | true, |
| 3987 | 0, |
| 3988 | NULL, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 3989 | address); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3990 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3991 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3992 | // open input streams needed to access attached devices to validate |
| 3993 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3994 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3995 | if (!inProfile->canOpenNewIo()) { |
| 3996 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 3997 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 3998 | continue; |
| 3999 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4000 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4001 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4002 | continue; |
| 4003 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4004 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4005 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4006 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 4007 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4008 | if ((profileType & inputDeviceTypes) == 0) { |
| 4009 | continue; |
| 4010 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4011 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4012 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4013 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 4014 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromTypeMask(profileType); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4015 | // the inputs vector must be of size >= 1, but we don't want to crash here |
| 4016 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 4017 | : String8(""); |
| 4018 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 4019 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 4020 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4021 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4022 | status_t status = inputDesc->open(nullptr, |
| 4023 | profileType, |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 4024 | address, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4025 | AUDIO_SOURCE_MIC, |
| 4026 | AUDIO_INPUT_FLAG_NONE, |
| 4027 | &input); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4028 | |
| 4029 | if (status == NO_ERROR) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4030 | for (const auto& dev : inProfile->getSupportedDevices()) { |
| 4031 | ssize_t index = mAvailableInputDevices.indexOf(dev); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4032 | // 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] | 4033 | if (index >= 0) { |
| 4034 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 4035 | if (!devDesc->isAttached()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4036 | devDesc->attach(hwModule); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4037 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 4038 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4039 | } |
| 4040 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4041 | inputDesc->close(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4042 | } else { |
| 4043 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4044 | profileType, |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4045 | hwModule->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4046 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4047 | } |
| 4048 | } |
| 4049 | // make sure all attached devices have been allocated a unique ID |
| 4050 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4051 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4052 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4053 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 4054 | continue; |
| 4055 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4056 | // The device is now validated and can be appended to the available devices of the engine |
| 4057 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 4058 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4059 | i++; |
| 4060 | } |
| 4061 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4062 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4063 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4064 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 4065 | continue; |
| 4066 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4067 | // The device is now validated and can be appended to the available devices of the engine |
| 4068 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 4069 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4070 | i++; |
| 4071 | } |
| 4072 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4073 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4074 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4075 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4076 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4077 | // If microphones address is empty, set it according to device type |
| 4078 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 4079 | if (mAvailableInputDevices[i]->mAddress.isEmpty()) { |
| 4080 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 4081 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS); |
| 4082 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
| 4083 | mAvailableInputDevices[i]->mAddress = String8(AUDIO_BACK_MICROPHONE_ADDRESS); |
| 4084 | } |
| 4085 | } |
| 4086 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4087 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4088 | if (mPrimaryOutput == 0) { |
| 4089 | ALOGE("Failed to open primary output"); |
| 4090 | status = NO_INIT; |
| 4091 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4092 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4093 | // Silence ALOGV statements |
| 4094 | property_set("log.tag." LOG_TAG, "D"); |
| 4095 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4096 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4097 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4098 | } |
| 4099 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4100 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4101 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4102 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4103 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4104 | } |
| 4105 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4106 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4108 | mAvailableOutputDevices.clear(); |
| 4109 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4110 | mOutputs.clear(); |
| 4111 | mInputs.clear(); |
| 4112 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4113 | mHwModulesAll.clear(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 4114 | mSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4115 | } |
| 4116 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4117 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4118 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4119 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4120 | } |
| 4121 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4122 | // --- |
| 4123 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4124 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4125 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4126 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4127 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4128 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4129 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4130 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4131 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4132 | } |
| 4133 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4134 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4135 | { |
| 4136 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4137 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4138 | } |
| 4139 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4140 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4141 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4142 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4143 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4144 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4145 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4146 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4147 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4148 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4149 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4150 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4151 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4152 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4153 | if (devDesc != 0) { |
| 4154 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4155 | desc->mIoHandle, address.string()); |
| 4156 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4157 | } |
| 4158 | } |
| 4159 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4160 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4161 | audio_policy_dev_state_t state, |
| 4162 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4163 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4164 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4165 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4166 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4167 | |
| 4168 | if (audio_device_is_digital(device)) { |
| 4169 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4170 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4171 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4172 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4173 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4174 | // first list already open outputs that can be routed to this device |
| 4175 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4176 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4177 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4178 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4179 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4180 | outputs.add(mOutputs.keyAt(i)); |
| 4181 | } else { |
| 4182 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4183 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4184 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4185 | } |
| 4186 | } |
| 4187 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4188 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4189 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4190 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4191 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4192 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4193 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4194 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4195 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4196 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4197 | j, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4198 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4199 | } |
| 4200 | } |
| 4201 | } |
| 4202 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4203 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4204 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4205 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4206 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4207 | return BAD_VALUE; |
| 4208 | } |
| 4209 | |
| 4210 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4211 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4212 | 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] | 4213 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4214 | |
| 4215 | // nothing to do if one output is already opened for this profile |
| 4216 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4217 | for (j = 0; j < outputs.size(); j++) { |
| 4218 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4219 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4220 | // matching profile: save the sample rates, format and channel masks supported |
| 4221 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4222 | if (audio_device_is_digital(device)) { |
| 4223 | devDesc->importAudioPort(profile); |
| 4224 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4225 | break; |
| 4226 | } |
| 4227 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4228 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4229 | continue; |
| 4230 | } |
| 4231 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4232 | if (!profile->canOpenNewIo()) { |
| 4233 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4234 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4235 | continue; |
| 4236 | } |
| 4237 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4238 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 4239 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4240 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4241 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4242 | status_t status = desc->open(nullptr, device, address, |
| 4243 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4244 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4245 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4246 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4247 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4248 | char *param = audio_device_address_to_parameter(device, address); |
| 4249 | mpClientInterface->setParameters(output, String8(param)); |
| 4250 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4251 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4252 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4253 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4254 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4255 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4256 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4257 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4258 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4259 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4260 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4261 | profile->pickAudioProfile( |
| 4262 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4263 | config.offload_info.sample_rate = config.sample_rate; |
| 4264 | config.offload_info.channel_mask = config.channel_mask; |
| 4265 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4266 | |
| 4267 | status_t status = desc->open(&config, device, address, AUDIO_STREAM_DEFAULT, |
| 4268 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4269 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4270 | output = AUDIO_IO_HANDLE_NONE; |
| 4271 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4274 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4275 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4276 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4277 | sp<AudioPolicyMix> policyMix; |
| 4278 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4279 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4280 | address.string()); |
| 4281 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4282 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4283 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4284 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4285 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4286 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4287 | // no duplicated output for direct outputs and |
| 4288 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4289 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4290 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4291 | //TODO: configure audio effect output stage here |
| 4292 | |
| 4293 | // 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] | 4294 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4295 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4296 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4297 | &duplicatedOutput); |
| 4298 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4299 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4300 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4301 | } else { |
| 4302 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4303 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4304 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4305 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4306 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4307 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4308 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4309 | } |
| 4310 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4311 | } else { |
| 4312 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4313 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4314 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4315 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4316 | profiles.removeAt(profile_index); |
| 4317 | profile_index--; |
| 4318 | } else { |
| 4319 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4320 | // Load digital format info only for digital devices |
| 4321 | if (audio_device_is_digital(device)) { |
| 4322 | devDesc->importAudioPort(profile); |
| 4323 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4324 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4325 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4326 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4327 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4328 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4329 | NULL/*patch handle*/, address.string()); |
| 4330 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4331 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | if (profiles.isEmpty()) { |
| 4336 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4337 | return BAD_VALUE; |
| 4338 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4339 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4340 | // check if one opened output is not needed any more after disconnecting one device |
| 4341 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4342 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4343 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4344 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4345 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4346 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4347 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4348 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4349 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4350 | mOutputs.keyAt(i)); |
| 4351 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4352 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4353 | } |
| 4354 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4355 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4356 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4357 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4358 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4359 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4360 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4361 | "clearing direct output profile %zu on module %s", |
| 4362 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4363 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | } |
| 4367 | } |
| 4368 | return NO_ERROR; |
| 4369 | } |
| 4370 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4371 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4372 | audio_policy_dev_state_t state, |
| 4373 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4374 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4375 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4376 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4377 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4378 | |
| 4379 | if (audio_device_is_digital(device)) { |
| 4380 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4381 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4382 | } |
| 4383 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4384 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4385 | // first list already open inputs that can be routed to this device |
| 4386 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4387 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4388 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4389 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4390 | inputs.add(mInputs.keyAt(input_index)); |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4395 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4396 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4397 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4398 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4399 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4400 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4401 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4402 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4403 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4404 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4405 | profiles.add(profile); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4406 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4407 | profile_index, hwModule->getName()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4408 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4409 | } |
| 4410 | } |
| 4411 | } |
| 4412 | |
| 4413 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4414 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4415 | return BAD_VALUE; |
| 4416 | } |
| 4417 | |
| 4418 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4419 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4420 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4421 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4422 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4423 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4424 | // nothing to do if one input is already opened for this profile |
| 4425 | size_t input_index; |
| 4426 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4427 | desc = mInputs.valueAt(input_index); |
| 4428 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4429 | if (audio_device_is_digital(device)) { |
| 4430 | devDesc->importAudioPort(profile); |
| 4431 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4432 | break; |
| 4433 | } |
| 4434 | } |
| 4435 | if (input_index != mInputs.size()) { |
| 4436 | continue; |
| 4437 | } |
| 4438 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4439 | if (!profile->canOpenNewIo()) { |
| 4440 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4441 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4442 | continue; |
| 4443 | } |
| 4444 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4445 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4446 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4447 | status_t status = desc->open(nullptr, |
| 4448 | device, |
| 4449 | address, |
| 4450 | AUDIO_SOURCE_MIC, |
| 4451 | AUDIO_INPUT_FLAG_NONE, |
| 4452 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4453 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4454 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4455 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4456 | char *param = audio_device_address_to_parameter(device, address); |
| 4457 | mpClientInterface->setParameters(input, String8(param)); |
| 4458 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4459 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4460 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4461 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4462 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4463 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4464 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4465 | } |
| 4466 | |
| 4467 | if (input != 0) { |
| 4468 | addInput(input, desc); |
| 4469 | } |
| 4470 | } // endif input != 0 |
| 4471 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4472 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4473 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4474 | profiles.removeAt(profile_index); |
| 4475 | profile_index--; |
| 4476 | } else { |
| 4477 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4478 | if (audio_device_is_digital(device)) { |
| 4479 | devDesc->importAudioPort(profile); |
| 4480 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4481 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4482 | } |
| 4483 | } // end scan profiles |
| 4484 | |
| 4485 | if (profiles.isEmpty()) { |
| 4486 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4487 | return BAD_VALUE; |
| 4488 | } |
| 4489 | } else { |
| 4490 | // Disconnect |
| 4491 | // check if one opened input is not needed any more after disconnecting one device |
| 4492 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4493 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4494 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4495 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4496 | mInputs.keyAt(input_index)); |
| 4497 | inputs.add(mInputs.keyAt(input_index)); |
| 4498 | } |
| 4499 | } |
| 4500 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4501 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4502 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4503 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4504 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4505 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4506 | if (profile->supportDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4507 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4508 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4509 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4510 | } |
| 4511 | } |
| 4512 | } |
| 4513 | } // end disconnect |
| 4514 | |
| 4515 | return NO_ERROR; |
| 4516 | } |
| 4517 | |
| 4518 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4519 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | { |
| 4521 | ALOGV("closeOutput(%d)", output); |
| 4522 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4523 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4524 | if (outputDesc == NULL) { |
| 4525 | ALOGW("closeOutput() unknown output %d", output); |
| 4526 | return; |
| 4527 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4528 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4529 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4530 | // look for duplicated outputs connected to the output being removed. |
| 4531 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4532 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4533 | if (dupOutputDesc->isDuplicated() && |
| 4534 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4535 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4536 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4537 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4538 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4539 | } else { |
| 4540 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4541 | } |
| 4542 | // As all active tracks on duplicated output will be deleted, |
| 4543 | // and as they were also referenced on the other output, the reference |
| 4544 | // count for their stream type must be adjusted accordingly on |
| 4545 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4546 | const bool wasActive = outputDesc2->isActive(); |
| 4547 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4548 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4549 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4550 | // stop() will be a no op if the output is still active but is needed in case all |
| 4551 | // active streams refcounts where cleared above |
| 4552 | if (wasActive) { |
| 4553 | outputDesc2->stop(); |
| 4554 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4555 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4556 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4557 | |
| 4558 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4559 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4560 | } |
| 4561 | } |
| 4562 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4563 | nextAudioPortGeneration(); |
| 4564 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4565 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4566 | if (index >= 0) { |
| 4567 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4568 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4569 | mAudioPatches.removeItemsAt(index); |
| 4570 | mpClientInterface->onAudioPatchListUpdate(); |
| 4571 | } |
| 4572 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4573 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4574 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4575 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4576 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4577 | |
| 4578 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4579 | // no direct outputs are open. |
| 4580 | if (getMsdAudioOutDeviceTypes() != AUDIO_DEVICE_NONE) { |
| 4581 | bool directOutputOpen = false; |
| 4582 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4583 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4584 | directOutputOpen = true; |
| 4585 | break; |
| 4586 | } |
| 4587 | } |
| 4588 | if (!directOutputOpen) { |
| 4589 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4590 | setMsdPatch(); |
| 4591 | } |
| 4592 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4596 | { |
| 4597 | ALOGV("closeInput(%d)", input); |
| 4598 | |
| 4599 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4600 | if (inputDesc == NULL) { |
| 4601 | ALOGW("closeInput() unknown input %d", input); |
| 4602 | return; |
| 4603 | } |
| 4604 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4605 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4606 | |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4607 | audio_devices_t device = inputDesc->mDevice; |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4608 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4609 | if (index >= 0) { |
| 4610 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4611 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4612 | mAudioPatches.removeItemsAt(index); |
| 4613 | mpClientInterface->onAudioPatchListUpdate(); |
| 4614 | } |
| 4615 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4616 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4617 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4618 | |
| 4619 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 4620 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 4621 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4622 | SoundTrigger::setCaptureState(false); |
| 4623 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | } |
| 4625 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4626 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4627 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4628 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4629 | { |
| 4630 | SortedVector<audio_io_handle_t> outputs; |
| 4631 | |
| 4632 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4633 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4634 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4635 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4636 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4637 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4638 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4639 | outputs.add(openOutputs.keyAt(i)); |
| 4640 | } |
| 4641 | } |
| 4642 | return outputs; |
| 4643 | } |
| 4644 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4645 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4646 | { |
| 4647 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4648 | // output is suspended before any tracks are moved to it |
| 4649 | checkA2dpSuspend(); |
| 4650 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4651 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4652 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 4653 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4654 | setMsdPatch(); |
| 4655 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4656 | } |
| 4657 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4658 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4659 | { |
| 4660 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4661 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4662 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4663 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4664 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4665 | // also take into account external policy-related changes: add all outputs which are |
| 4666 | // associated with policies in the "before" and "after" output vectors |
| 4667 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4668 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4669 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4670 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4671 | srcOutputs.add(desc->mIoHandle); |
| 4672 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4673 | } |
| 4674 | } |
| 4675 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4676 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4677 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4678 | dstOutputs.add(desc->mIoHandle); |
| 4679 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4680 | } |
| 4681 | } |
| 4682 | |
Mikhail Naganov | 9de8bd1 | 2018-07-23 16:41:31 -0700 | [diff] [blame] | 4683 | if (srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4684 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4685 | // audio from invalidated tracks will be rendered when unmuting |
| 4686 | uint32_t maxLatency = 0; |
| 4687 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4688 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4689 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4690 | maxLatency = desc->latency(); |
| 4691 | } |
| 4692 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4693 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4694 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4695 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4696 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4697 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4698 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4699 | setStrategyMute(strategy, true, desc); |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4700 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4701 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4702 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4703 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4704 | if (source != 0){ |
| 4705 | connectAudioSource(source); |
| 4706 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4707 | } |
| 4708 | |
| 4709 | // Move effects associated to this strategy from previous output to new output |
| 4710 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4711 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4712 | } |
| 4713 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4714 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4715 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4716 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4717 | } |
| 4718 | } |
| 4719 | } |
| 4720 | } |
| 4721 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4722 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4723 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4724 | 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] | 4725 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4727 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4728 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4729 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4730 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4731 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4732 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4733 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4734 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4737 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4738 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4739 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4740 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4741 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4742 | return; |
| 4743 | } |
| 4744 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4745 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4746 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4747 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4748 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4749 | |
| 4750 | // if suspended, restore A2DP output if: |
| 4751 | // ((SCO device is NOT connected) || |
| 4752 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4753 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4754 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4755 | // if not suspended, suspend A2DP output if: |
| 4756 | // (SCO device is connected) && |
| 4757 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4758 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4759 | // |
| 4760 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4761 | if (!isScoConnected || |
| 4762 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4763 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4764 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4765 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4766 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4767 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4768 | |
| 4769 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4770 | mA2dpSuspended = false; |
| 4771 | } |
| 4772 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4773 | if (isScoConnected && |
| 4774 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4775 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4776 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4777 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4778 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4779 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4780 | |
| 4781 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4782 | mA2dpSuspended = true; |
| 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4787 | template <class IoDescriptor, class Filter> |
| 4788 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4789 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4790 | { |
| 4791 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4792 | auto activeClientsWithRoute = |
| 4793 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4794 | active = activeClients.size() > 0; |
| 4795 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4796 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4797 | } |
| 4798 | return nullptr; |
| 4799 | } |
| 4800 | |
| 4801 | template <class IoCollection, class Filter> |
| 4802 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4803 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4804 | { |
| 4805 | sp<DeviceDescriptor> device; |
| 4806 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4807 | auto desc = ioCollection.valueAt(i); |
| 4808 | bool active; |
| 4809 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4810 | if (active && curDevice == nullptr) { |
| 4811 | return nullptr; |
| 4812 | } else if (curDevice != nullptr) { |
| 4813 | device = curDevice; |
| 4814 | } |
| 4815 | } |
| 4816 | return device; |
| 4817 | } |
| 4818 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4819 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4820 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4821 | { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4822 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4823 | if (index >= 0) { |
| 4824 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4825 | if (patchDesc->mUid != mUidCached) { |
| 4826 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4827 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4828 | return outputDesc->device(); |
| 4829 | } |
| 4830 | } |
| 4831 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4832 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4833 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4834 | bool active; // unused |
| 4835 | sp<DeviceDescriptor> deviceDesc = |
| 4836 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
| 4837 | if (deviceDesc != nullptr) { |
| 4838 | return deviceDesc->type(); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4839 | } |
| 4840 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | // 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] | 4842 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4843 | // use device for strategy enforced audible |
| 4844 | // 2: we are in call or the strategy phone is active on the output: |
| 4845 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4846 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4847 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4848 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4849 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4850 | // 5: the strategy accessibility is active on the output: |
| 4851 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4852 | // 6: the strategy "respectful" sonification is active on the output: |
| 4853 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4854 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4855 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4856 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4857 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4858 | // 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] | 4859 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4860 | |
| 4861 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4862 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4863 | // as opposed to all streams on the same audio HAL module. |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4864 | audio_devices_t device = AUDIO_DEVICE_NONE; |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4865 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4866 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4867 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4868 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4869 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4870 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4871 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4872 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4873 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4874 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4875 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4876 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4877 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4878 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4879 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4880 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4881 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4882 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4883 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4884 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4885 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4886 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4887 | } |
| 4888 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4889 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4890 | return device; |
| 4891 | } |
| 4892 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4893 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4894 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4895 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4896 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4897 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4898 | if (index >= 0) { |
| 4899 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4900 | if (patchDesc->mUid != mUidCached) { |
| 4901 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4902 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4903 | return inputDesc->mDevice; |
| 4904 | } |
| 4905 | } |
| 4906 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4907 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4908 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4909 | bool active; |
| 4910 | sp<DeviceDescriptor> deviceDesc = |
| 4911 | findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4912 | if (deviceDesc != nullptr) { |
| 4913 | return deviceDesc->type(); |
| 4914 | } |
| 4915 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4916 | // If we are not in call and no client is active on this input, this methods returns |
| 4917 | // 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] | 4918 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4919 | if (source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4920 | source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 4921 | } |
| 4922 | if (source != AUDIO_SOURCE_DEFAULT) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4923 | device = getDeviceAndMixForInputSource(source); |
| 4924 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4925 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4926 | return device; |
| 4927 | } |
| 4928 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4929 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4930 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4931 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4932 | } |
| 4933 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4934 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4935 | return (uint32_t)getStrategy(stream); |
| 4936 | } |
| 4937 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4938 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4939 | // By checking the range of stream before calling getStrategy, we avoid |
| 4940 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4941 | // 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] | 4942 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4943 | return AUDIO_DEVICE_NONE; |
| 4944 | } |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4945 | audio_devices_t activeDevices = AUDIO_DEVICE_NONE; |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4946 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4947 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4948 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4949 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4950 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4951 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4952 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4953 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4954 | devices |= curDevices; |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4955 | for (audio_io_handle_t output : getOutputsForDevice(curDevices, mOutputs)) { |
| 4956 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4957 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4958 | activeDevices |= outputDesc->device(); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4959 | } |
| 4960 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4961 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4962 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4963 | // Favor devices selected on active streams if any to report correct device in case of |
| 4964 | // explicit device selection |
| 4965 | if (activeDevices != AUDIO_DEVICE_NONE) { |
| 4966 | devices = activeDevices; |
| 4967 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4968 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4969 | and doesn't really need to.*/ |
| 4970 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4971 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4972 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4973 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4974 | return devices; |
| 4975 | } |
| 4976 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4977 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4978 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4979 | 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] | 4980 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4981 | } |
| 4982 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4983 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4984 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4985 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4986 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4987 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4988 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4989 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4990 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4991 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4992 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4993 | } |
| 4994 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4995 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4996 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4997 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4998 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4999 | updateDevicesAndOutputs(); |
| 5000 | break; |
| 5001 | default: |
| 5002 | break; |
| 5003 | } |
| 5004 | } |
| 5005 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5006 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5007 | |
| 5008 | // skip beacon mute management if a dedicated TTS output is available |
| 5009 | if (mTtsOutputAvailable) { |
| 5010 | return 0; |
| 5011 | } |
| 5012 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5013 | switch(event) { |
| 5014 | case STARTING_OUTPUT: |
| 5015 | mBeaconMuteRefCount++; |
| 5016 | break; |
| 5017 | case STOPPING_OUTPUT: |
| 5018 | if (mBeaconMuteRefCount > 0) { |
| 5019 | mBeaconMuteRefCount--; |
| 5020 | } |
| 5021 | break; |
| 5022 | case STARTING_BEACON: |
| 5023 | mBeaconPlayingRefCount++; |
| 5024 | break; |
| 5025 | case STOPPING_BEACON: |
| 5026 | if (mBeaconPlayingRefCount > 0) { |
| 5027 | mBeaconPlayingRefCount--; |
| 5028 | } |
| 5029 | break; |
| 5030 | } |
| 5031 | |
| 5032 | if (mBeaconMuteRefCount > 0) { |
| 5033 | // any playback causes beacon to be muted |
| 5034 | return setBeaconMute(true); |
| 5035 | } else { |
| 5036 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5037 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5038 | } |
| 5039 | } |
| 5040 | |
| 5041 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5042 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5043 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5044 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5045 | if (mBeaconMuted != mute) { |
| 5046 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5047 | ALOGV("\t muting %d", mute); |
| 5048 | uint32_t maxLatency = 0; |
| 5049 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5050 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5051 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5052 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5053 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5054 | const uint32_t latency = desc->latency() * 2; |
| 5055 | if (latency > maxLatency) { |
| 5056 | maxLatency = latency; |
| 5057 | } |
| 5058 | } |
| 5059 | mBeaconMuted = mute; |
| 5060 | return maxLatency; |
| 5061 | } |
| 5062 | return 0; |
| 5063 | } |
| 5064 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5065 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5066 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5067 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5068 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5069 | // case the last active client route is used |
| 5070 | sp<DeviceDescriptor> deviceDesc = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5071 | if (deviceDesc != nullptr) { |
| 5072 | return deviceDesc->type(); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5075 | if (fromCache) { |
| 5076 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 5077 | strategy, mDeviceForStrategy[strategy]); |
| 5078 | return mDeviceForStrategy[strategy]; |
| 5079 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5080 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5081 | } |
| 5082 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5083 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5084 | { |
| 5085 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 5086 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5087 | } |
| 5088 | mPreviousOutputs = mOutputs; |
| 5089 | } |
| 5090 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5091 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5092 | audio_devices_t prevDevice, |
| 5093 | uint32_t delayMs) |
| 5094 | { |
| 5095 | // mute/unmute strategies using an incompatible device combination |
| 5096 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5097 | // if unmuting, unmute only after the specified delay |
| 5098 | if (outputDesc->isDuplicated()) { |
| 5099 | return 0; |
| 5100 | } |
| 5101 | |
| 5102 | uint32_t muteWaitMs = 0; |
| 5103 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5104 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5105 | |
| 5106 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 5107 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5108 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5109 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 5110 | bool doMute = false; |
| 5111 | |
| 5112 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5113 | doMute = true; |
| 5114 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5115 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5116 | doMute = true; |
| 5117 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5118 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5119 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5120 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5121 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5122 | // skip output if it does not share any device with current output |
| 5123 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 5124 | == AUDIO_DEVICE_NONE) { |
| 5125 | continue; |
| 5126 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5127 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5128 | mute ? "muting" : "unmuting", i, curDevice); |
| 5129 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5130 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5131 | if (mute) { |
| 5132 | // FIXME: should not need to double latency if volume could be applied |
| 5133 | // immediately by the audioflinger mixer. We must account for the delay |
| 5134 | // between now and the next time the audioflinger thread for this output |
| 5135 | // will process a buffer (which corresponds to one buffer size, |
| 5136 | // usually 1/2 or 1/4 of the latency). |
| 5137 | if (muteWaitMs < desc->latency() * 2) { |
| 5138 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5139 | } |
| 5140 | } |
| 5141 | } |
| 5142 | } |
| 5143 | } |
| 5144 | } |
| 5145 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5146 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5147 | // different per device volumes |
| 5148 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5149 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5150 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5151 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5152 | if (muteWaitMs < tempMuteWaitMs) { |
| 5153 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5154 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5155 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5156 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5157 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5158 | // make sure that we do not start the temporary mute period too early in case of |
| 5159 | // delayed device change |
| 5160 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5161 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5162 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5163 | } |
| 5164 | } |
| 5165 | } |
| 5166 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5167 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5168 | if (muteWaitMs > delayMs) { |
| 5169 | muteWaitMs -= delayMs; |
| 5170 | usleep(muteWaitMs * 1000); |
| 5171 | return muteWaitMs; |
| 5172 | } |
| 5173 | return 0; |
| 5174 | } |
| 5175 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5176 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5177 | audio_devices_t device, |
| 5178 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5179 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5180 | audio_patch_handle_t *patchHandle, |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5181 | const char *address, |
| 5182 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5183 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5184 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5185 | AudioParameter param; |
| 5186 | uint32_t muteWaitMs; |
| 5187 | |
| 5188 | if (outputDesc->isDuplicated()) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5189 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 5190 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 5191 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 5192 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5193 | return muteWaitMs; |
| 5194 | } |
| 5195 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5196 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5197 | if ((device != AUDIO_DEVICE_NONE) && |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5198 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5199 | return 0; |
| 5200 | } |
| 5201 | |
| 5202 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5203 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | |
| 5205 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5206 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5207 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5208 | |
| 5209 | if (device != AUDIO_DEVICE_NONE) { |
| 5210 | outputDesc->mDevice = device; |
| 5211 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5212 | |
| 5213 | // if the outputs are not materially active, there is no need to mute. |
| 5214 | if (requiresMuteCheck) { |
| 5215 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5216 | } else { |
| 5217 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5218 | muteWaitMs = 0; |
| 5219 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5220 | |
| 5221 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5222 | // the requested device is AUDIO_DEVICE_NONE |
| 5223 | // OR the requested device is the same as current device |
| 5224 | // AND force is not specified |
| 5225 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5226 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5227 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5228 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5229 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5230 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5231 | return muteWaitMs; |
| 5232 | } |
| 5233 | |
| 5234 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5235 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5236 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5237 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5238 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5239 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5240 | DeviceVector deviceList; |
| 5241 | if ((address == NULL) || (strlen(address) == 0)) { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5242 | deviceList = mAvailableOutputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5243 | } else { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5244 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice( |
| 5245 | device, String8(address)); |
| 5246 | if (deviceDesc) deviceList.add(deviceDesc); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5247 | } |
| 5248 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5249 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5250 | PatchBuilder patchBuilder; |
| 5251 | patchBuilder.addSource(outputDesc); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5252 | 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] | 5253 | patchBuilder.addSink(deviceList.itemAt(i)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5254 | } |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5255 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5256 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5257 | |
| 5258 | // inform all input as well |
| 5259 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5260 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5261 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5262 | AudioParameter inputCmd = AudioParameter(); |
| 5263 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5264 | inputDescriptor->mIoHandle, device); |
| 5265 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5266 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5267 | inputCmd.toString(), |
| 5268 | delayMs); |
| 5269 | } |
| 5270 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5271 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5272 | |
| 5273 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5274 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5275 | |
| 5276 | return muteWaitMs; |
| 5277 | } |
| 5278 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5279 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5280 | int delayMs, |
| 5281 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5282 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5283 | ssize_t index; |
| 5284 | if (patchHandle) { |
| 5285 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5286 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5287 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5288 | } |
| 5289 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5290 | return INVALID_OPERATION; |
| 5291 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5292 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5293 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5294 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5295 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5296 | removeAudioPatch(patchDesc->mHandle); |
| 5297 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5298 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5299 | return status; |
| 5300 | } |
| 5301 | |
| 5302 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5303 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5304 | bool force, |
| 5305 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5306 | { |
| 5307 | status_t status = NO_ERROR; |
| 5308 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5309 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5310 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5311 | inputDesc->mDevice = device; |
| 5312 | |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 5313 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromTypeMask(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5314 | if (!deviceList.isEmpty()) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5315 | PatchBuilder patchBuilder; |
| 5316 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5317 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5318 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5319 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5320 | auto result = usecase; |
| 5321 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5322 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5323 | } |
| 5324 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5325 | //only one input device for now |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5326 | addSource(deviceList.itemAt(0)); |
| 5327 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5328 | } |
| 5329 | } |
| 5330 | return status; |
| 5331 | } |
| 5332 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5333 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5334 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5335 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5336 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5337 | ssize_t index; |
| 5338 | if (patchHandle) { |
| 5339 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5340 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5341 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5342 | } |
| 5343 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5344 | return INVALID_OPERATION; |
| 5345 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5346 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5347 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5348 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5349 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5350 | removeAudioPatch(patchDesc->mHandle); |
| 5351 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5352 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5353 | return status; |
| 5354 | } |
| 5355 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5356 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5357 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5358 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5359 | audio_format_t& format, |
| 5360 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5361 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5362 | { |
| 5363 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5364 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5365 | // |
| 5366 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5367 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5368 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5369 | sp<IOProfile> firstInexact; |
| 5370 | uint32_t updatedSamplingRate = 0; |
| 5371 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5372 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5373 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5374 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5375 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5376 | //updatedFormat = format; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5377 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5378 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5379 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5380 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5381 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5382 | &channelMask /*updatedChannelMask*/, |
| 5383 | // FIXME ugly cast |
| 5384 | (audio_output_flags_t) flags, |
| 5385 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5386 | return profile; |
| 5387 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5388 | if (firstInexact == nullptr && profile->isCompatibleProfile(device, address, |
| 5389 | samplingRate, |
| 5390 | &updatedSamplingRate, |
| 5391 | format, |
| 5392 | &updatedFormat, |
| 5393 | channelMask, |
| 5394 | &updatedChannelMask, |
| 5395 | // FIXME ugly cast |
| 5396 | (audio_output_flags_t) flags, |
| 5397 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5398 | firstInexact = profile; |
| 5399 | } |
| 5400 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5401 | } |
| 5402 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5403 | if (firstInexact != nullptr) { |
| 5404 | samplingRate = updatedSamplingRate; |
| 5405 | format = updatedFormat; |
| 5406 | channelMask = updatedChannelMask; |
| 5407 | return firstInexact; |
| 5408 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5409 | return NULL; |
| 5410 | } |
| 5411 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5412 | |
| 5413 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5414 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5415 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5416 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5417 | // case the last active client route is used |
| 5418 | sp<DeviceDescriptor> deviceDesc = |
| 5419 | findPreferredDevice(mInputs, inputSource, mAvailableInputDevices); |
| 5420 | if (deviceDesc != nullptr) { |
| 5421 | return deviceDesc->type(); |
| 5422 | } |
| 5423 | |
| 5424 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5425 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5426 | audio_devices_t selectedDeviceFromMix = |
| 5427 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5428 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5429 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5430 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5431 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5432 | return getDeviceForInputSource(inputSource); |
| 5433 | } |
| 5434 | |
| 5435 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5436 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5437 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5438 | } |
| 5439 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5440 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5441 | int index, |
| 5442 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5443 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5444 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5445 | |
| 5446 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5447 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5448 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5449 | // the ringtone volume |
| 5450 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5451 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5452 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5453 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5454 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5455 | } |
| 5456 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5457 | // in-call: always cap volume by voice volume + some low headroom |
| 5458 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5459 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5460 | switch (stream) { |
| 5461 | case AUDIO_STREAM_SYSTEM: |
| 5462 | case AUDIO_STREAM_RING: |
| 5463 | case AUDIO_STREAM_MUSIC: |
| 5464 | case AUDIO_STREAM_ALARM: |
| 5465 | case AUDIO_STREAM_NOTIFICATION: |
| 5466 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5467 | case AUDIO_STREAM_DTMF: |
| 5468 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5469 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5470 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5471 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5472 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5473 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5474 | if (volumeDB > maxVoiceVolDb) { |
| 5475 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5476 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5477 | volumeDB = maxVoiceVolDb; |
| 5478 | } |
| 5479 | } break; |
| 5480 | default: |
| 5481 | break; |
| 5482 | } |
| 5483 | } |
| 5484 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5485 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5486 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5487 | // - always attenuate notifications volume by 6dB |
| 5488 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5489 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5490 | // - if music is playing, always limit the volume to current music volume, |
| 5491 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5492 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5493 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5494 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5495 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5496 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5497 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5498 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5499 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5500 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5501 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5502 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5503 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5504 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5505 | // when the phone is ringing we must consider that music could have been paused just before |
| 5506 | // by the music application and behave as if music was active if the last music track was |
| 5507 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5508 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5509 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5510 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5511 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5512 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5513 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5514 | musicDevice), |
| 5515 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5516 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5517 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5518 | if (volumeDB > minVolDB) { |
| 5519 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5520 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5521 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5522 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5523 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5524 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5525 | // intended to be played |
| 5526 | if ((volumeDB > -96.0f) && |
| 5527 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5528 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5529 | stream, device, |
| 5530 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5531 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5532 | } |
| 5533 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5534 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5535 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5536 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5537 | } |
| 5538 | } |
| 5539 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5540 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5541 | } |
| 5542 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5543 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5544 | audio_stream_type_t srcStream, |
| 5545 | audio_stream_type_t dstStream) |
| 5546 | { |
| 5547 | if (srcStream == dstStream) { |
| 5548 | return srcIndex; |
| 5549 | } |
| 5550 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5551 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5552 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5553 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5554 | |
| 5555 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5556 | } |
| 5557 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5558 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5559 | int index, |
| 5560 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5561 | audio_devices_t device, |
| 5562 | int delayMs, |
| 5563 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5564 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5565 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5566 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5567 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5568 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5569 | return NO_ERROR; |
| 5570 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5571 | audio_policy_forced_cfg_t forceUseForComm = |
| 5572 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5573 | // 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] | 5574 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5575 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5576 | 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] | 5577 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5578 | return INVALID_OPERATION; |
| 5579 | } |
| 5580 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5581 | if (device == AUDIO_DEVICE_NONE) { |
| 5582 | device = outputDesc->device(); |
| 5583 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5584 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5585 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5586 | if (outputDesc->isFixedVolume(device) || |
| 5587 | // Force VoIP volume to max for bluetooth SCO |
| 5588 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5589 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5590 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5591 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5592 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5593 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5594 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5595 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5596 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5597 | float voiceVolume; |
| 5598 | // 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] | 5599 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5600 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5601 | } else { |
| 5602 | voiceVolume = 1.0; |
| 5603 | } |
| 5604 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5605 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5606 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5607 | mLastVoiceVolume = voiceVolume; |
| 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | return NO_ERROR; |
| 5612 | } |
| 5613 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5614 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5615 | audio_devices_t device, |
| 5616 | int delayMs, |
| 5617 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5618 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5619 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5620 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5621 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5622 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5623 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5624 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5625 | device, |
| 5626 | delayMs, |
| 5627 | force); |
| 5628 | } |
| 5629 | } |
| 5630 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5631 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5632 | bool on, |
| 5633 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5634 | int delayMs, |
| 5635 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5636 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5637 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5638 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5639 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5640 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5641 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5642 | } |
| 5643 | } |
| 5644 | } |
| 5645 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5646 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5647 | bool on, |
| 5648 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5649 | int delayMs, |
| 5650 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5651 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5652 | if (device == AUDIO_DEVICE_NONE) { |
| 5653 | device = outputDesc->device(); |
| 5654 | } |
| 5655 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5656 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5657 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5658 | |
| 5659 | if (on) { |
| 5660 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5661 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5662 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5663 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5664 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5665 | } |
| 5666 | } |
| 5667 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5668 | outputDesc->mMuteCount[stream]++; |
| 5669 | } else { |
| 5670 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5671 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5672 | return; |
| 5673 | } |
| 5674 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5675 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5676 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5677 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5678 | device, |
| 5679 | delayMs); |
| 5680 | } |
| 5681 | } |
| 5682 | } |
| 5683 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5684 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5685 | { |
| 5686 | // flags to stream type mapping |
| 5687 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5688 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5689 | } |
| 5690 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5691 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5692 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5693 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5694 | return AUDIO_STREAM_TTS; |
| 5695 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5696 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5697 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5698 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5699 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5700 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5701 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5702 | // has flags that map to a strategy? |
| 5703 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5704 | return true; |
| 5705 | } |
| 5706 | |
| 5707 | // has known usage? |
| 5708 | switch (paa->usage) { |
| 5709 | case AUDIO_USAGE_UNKNOWN: |
| 5710 | case AUDIO_USAGE_MEDIA: |
| 5711 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5712 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5713 | case AUDIO_USAGE_ALARM: |
| 5714 | case AUDIO_USAGE_NOTIFICATION: |
| 5715 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5716 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5717 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5718 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5719 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5720 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5721 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5722 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5723 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5724 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5725 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5726 | break; |
| 5727 | default: |
| 5728 | return false; |
| 5729 | } |
| 5730 | return true; |
| 5731 | } |
| 5732 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5733 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5734 | routing_strategy strategy, uint32_t inPastMs, |
| 5735 | nsecs_t sysTime) const |
| 5736 | { |
| 5737 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5738 | sysTime = systemTime(); |
| 5739 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5740 | 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] | 5741 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5742 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5743 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5744 | return true; |
| 5745 | } |
| 5746 | } |
| 5747 | return false; |
| 5748 | } |
| 5749 | |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5750 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<AudioOutputDescriptor>& outputDesc, |
| 5751 | routing_strategy strategy, uint32_t inPastMs, |
| 5752 | nsecs_t sysTime) const |
| 5753 | { |
| 5754 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5755 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5756 | if (outputDesc->sharesHwModuleWith(desc) |
| 5757 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5758 | return true; |
| 5759 | } |
| 5760 | } |
| 5761 | return false; |
| 5762 | } |
| 5763 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5764 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5765 | { |
| 5766 | return mEngine->getForceUse(usage); |
| 5767 | } |
| 5768 | |
| 5769 | bool AudioPolicyManager::isInCall() |
| 5770 | { |
| 5771 | return isStateInCall(mEngine->getPhoneState()); |
| 5772 | } |
| 5773 | |
| 5774 | bool AudioPolicyManager::isStateInCall(int state) |
| 5775 | { |
| 5776 | return is_state_in_call(state); |
| 5777 | } |
| 5778 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5779 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5780 | { |
| 5781 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5782 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5783 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5784 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5785 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5786 | } |
| 5787 | } |
| 5788 | |
| 5789 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5790 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5791 | bool release = false; |
| 5792 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5793 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5794 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5795 | source->ext.device.type == deviceDesc->type()) { |
| 5796 | release = true; |
| 5797 | } |
| 5798 | } |
| 5799 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5800 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5801 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5802 | sink->ext.device.type == deviceDesc->type()) { |
| 5803 | release = true; |
| 5804 | } |
| 5805 | } |
| 5806 | if (release) { |
| 5807 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5808 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5809 | } |
| 5810 | } |
| 5811 | } |
| 5812 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5813 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5814 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5815 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5816 | // TODO Set this based on Config properties. |
| 5817 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5818 | |
| 5819 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5820 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5821 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5822 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5823 | // If MANUAL, keep the supported surround sound formats as current enabled ones. |
| 5824 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 5825 | formats.clear(); |
| 5826 | for (auto it = mSurroundFormats.begin(); it != mSurroundFormats.end(); it++) { |
| 5827 | formats.add(*it); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5828 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5829 | // Always enable IEC61937 when in MANUAL mode. |
| 5830 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5831 | } else { // NEVER, AUTO or ALWAYS |
| 5832 | // Analyze original support for various formats. |
| 5833 | bool supportsAC3 = false; |
| 5834 | bool supportsOtherSurround = false; |
| 5835 | bool supportsIEC61937 = false; |
| 5836 | mSurroundFormats.clear(); |
| 5837 | for (ssize_t formatIndex = 0; formatIndex < (ssize_t)formats.size(); formatIndex++) { |
| 5838 | audio_format_t format = formats[formatIndex]; |
| 5839 | switch (format) { |
| 5840 | case AUDIO_FORMAT_AC3: |
| 5841 | supportsAC3 = true; |
| 5842 | break; |
| 5843 | case AUDIO_FORMAT_E_AC3: |
| 5844 | case AUDIO_FORMAT_DTS: |
| 5845 | case AUDIO_FORMAT_DTS_HD: |
| 5846 | // If ALWAYS, remove all other surround formats here |
| 5847 | // since we will add them later. |
| 5848 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5849 | formats.removeAt(formatIndex); |
| 5850 | formatIndex--; |
| 5851 | } |
| 5852 | supportsOtherSurround = true; |
| 5853 | break; |
| 5854 | case AUDIO_FORMAT_IEC61937: |
| 5855 | supportsIEC61937 = true; |
| 5856 | break; |
| 5857 | default: |
| 5858 | break; |
| 5859 | } |
| 5860 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5861 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5862 | // Modify formats based on surround preferences. |
| 5863 | // If NEVER, remove support for surround formats. |
| 5864 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5865 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5866 | // Remove surround sound related formats. |
| 5867 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5868 | audio_format_t format = formats[formatIndex]; |
| 5869 | switch(format) { |
| 5870 | case AUDIO_FORMAT_AC3: |
| 5871 | case AUDIO_FORMAT_E_AC3: |
| 5872 | case AUDIO_FORMAT_DTS: |
| 5873 | case AUDIO_FORMAT_DTS_HD: |
| 5874 | case AUDIO_FORMAT_IEC61937: |
| 5875 | formats.removeAt(formatIndex); |
| 5876 | break; |
| 5877 | default: |
| 5878 | formatIndex++; // keep it |
| 5879 | break; |
| 5880 | } |
| 5881 | } |
| 5882 | supportsAC3 = false; |
| 5883 | supportsOtherSurround = false; |
| 5884 | supportsIEC61937 = false; |
| 5885 | } |
| 5886 | } else { // AUTO or ALWAYS |
| 5887 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5888 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5889 | && !supportsAC3) { |
| 5890 | formats.add(AUDIO_FORMAT_AC3); |
| 5891 | supportsAC3 = true; |
| 5892 | } |
| 5893 | |
| 5894 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5895 | // This assumes that if any of these formats are reported by the HAL |
| 5896 | // then the report is valid and should not be modified. |
| 5897 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5898 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5899 | formats.add(AUDIO_FORMAT_DTS); |
| 5900 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5901 | supportsOtherSurround = true; |
| 5902 | } |
| 5903 | |
| 5904 | // Add support for IEC61937 if any raw surround supported. |
| 5905 | // The HAL could do this but add it here, just in case. |
| 5906 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5907 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5908 | supportsIEC61937 = true; |
| 5909 | } |
| 5910 | |
| 5911 | // Add reported surround sound formats to enabled surround formats. |
| 5912 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5913 | audio_format_t format = formats[formatIndex]; |
Mikhail Naganov | 02743e2 | 2018-11-28 15:56:55 -0800 | [diff] [blame] | 5914 | if (mConfig.getSurroundFormats().count(format) != 0) { |
| 5915 | mSurroundFormats.insert(format); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5916 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5917 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5918 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5919 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | // Modify the list of channel masks supported. |
| 5923 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5924 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5925 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5926 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5927 | |
| 5928 | // If NEVER, then remove support for channelMasks > stereo. |
| 5929 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5930 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5931 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5932 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5933 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5934 | channelMasks.removeAt(maskIndex); |
| 5935 | } else { |
| 5936 | maskIndex++; |
| 5937 | } |
| 5938 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5939 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5940 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5941 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5942 | bool supports5dot1 = false; |
| 5943 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5944 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5945 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5946 | supports5dot1 = true; |
| 5947 | break; |
| 5948 | } |
| 5949 | } |
| 5950 | // If not then add 5.1 support. |
| 5951 | if (!supports5dot1) { |
| 5952 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5953 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5954 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5955 | } |
| 5956 | } |
| 5957 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5958 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5959 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5960 | AudioProfileVector &profiles) |
| 5961 | { |
| 5962 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5963 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5964 | // Format MUST be checked first to update the list of AudioProfile |
| 5965 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5966 | reply = mpClientInterface->getParameters( |
| 5967 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5968 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5969 | AudioParameter repliedParameters(reply); |
| 5970 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5971 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5972 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5973 | return; |
| 5974 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5975 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5976 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5977 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5978 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5979 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5980 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5981 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5982 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5983 | ChannelsVector channelMasks; |
| 5984 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5985 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5986 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5987 | |
| 5988 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5989 | reply = mpClientInterface->getParameters( |
| 5990 | ioHandle, |
| 5991 | requestedParameters.toString() + ";" + |
| 5992 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5993 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5994 | AudioParameter repliedParameters(reply); |
| 5995 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5996 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5997 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5998 | } |
| 5999 | } |
| 6000 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6001 | reply = mpClientInterface->getParameters(ioHandle, |
| 6002 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6003 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6004 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6005 | AudioParameter repliedParameters(reply); |
| 6006 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6007 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6008 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6009 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 6010 | filterSurroundChannelMasks(&channelMasks); |
| 6011 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6012 | } |
| 6013 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6014 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6015 | } |
| 6016 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6017 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6018 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6019 | audio_patch_handle_t *patchHandle, |
| 6020 | AudioIODescriptorInterface *ioDescriptor, |
| 6021 | const struct audio_patch *patch, |
| 6022 | int delayMs) |
| 6023 | { |
| 6024 | ssize_t index = mAudioPatches.indexOfKey( |
| 6025 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6026 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6027 | sp<AudioPatch> patchDesc; |
| 6028 | status_t status = installPatch( |
| 6029 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6030 | if (status == NO_ERROR) { |
| 6031 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6032 | } |
| 6033 | return status; |
| 6034 | } |
| 6035 | |
| 6036 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6037 | ssize_t index, |
| 6038 | audio_patch_handle_t *patchHandle, |
| 6039 | const struct audio_patch *patch, |
| 6040 | int delayMs, |
| 6041 | uid_t uid, |
| 6042 | sp<AudioPatch> *patchDescPtr) |
| 6043 | { |
| 6044 | sp<AudioPatch> patchDesc; |
| 6045 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6046 | if (index >= 0) { |
| 6047 | patchDesc = mAudioPatches.valueAt(index); |
| 6048 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6049 | } |
| 6050 | |
| 6051 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6052 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6053 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6054 | if (status == NO_ERROR) { |
| 6055 | if (index < 0) { |
| 6056 | patchDesc = new AudioPatch(patch, uid); |
| 6057 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6058 | } else { |
| 6059 | patchDesc->mPatch = *patch; |
| 6060 | } |
| 6061 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6062 | if (patchHandle) { |
| 6063 | *patchHandle = patchDesc->mHandle; |
| 6064 | } |
| 6065 | nextAudioPortGeneration(); |
| 6066 | mpClientInterface->onAudioPatchListUpdate(); |
| 6067 | } |
| 6068 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6069 | return status; |
| 6070 | } |
| 6071 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6072 | } // namespace android |