Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 18 | |
| 19 | // Need to keep the log statements even in production builds |
| 20 | // to enable VERBOSE logging dynamically. |
| 21 | // You can enable VERBOSE logging as follows: |
| 22 | // adb shell setprop log.tag.APM_AudioPolicyManager V |
| 23 | #define LOG_NDEBUG 0 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 24 | |
| 25 | //#define VERY_VERBOSE_LOGGING |
| 26 | #ifdef VERY_VERBOSE_LOGGING |
| 27 | #define ALOGVV ALOGV |
| 28 | #else |
| 29 | #define ALOGVV(a...) do { } while(0) |
| 30 | #endif |
| 31 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 32 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 |
| 33 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 34 | #define AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME \ |
| 35 | "audio_policy_configuration_a2dp_offload_disabled.xml" |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 36 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 37 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 38 | #include <math.h> |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 39 | #include <unordered_set> |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 40 | #include <vector> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 42 | #include <AudioPolicyManagerInterface.h> |
| 43 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 44 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 45 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 46 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 47 | #include <media/AudioPolicyHelper.h> |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 48 | #include <private/android_filesystem_config.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 49 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 50 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 51 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 52 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 53 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 54 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 55 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 56 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 57 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 58 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 59 | //FIXME: workaround for truncated touch sounds |
| 60 | // to be removed when the problem is handled by system UI |
| 61 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 62 | |
| 63 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 64 | // media / notification / system volume. |
| 65 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 66 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 67 | // Compressed formats for MSD module, ordered from most preferred to least preferred. |
| 68 | static const std::vector<audio_format_t> compressedFormatsOrder = {{ |
| 69 | AUDIO_FORMAT_MAT_2_1, AUDIO_FORMAT_MAT_2_0, AUDIO_FORMAT_E_AC3, |
| 70 | AUDIO_FORMAT_AC3, AUDIO_FORMAT_PCM_16_BIT }}; |
| 71 | // Channel masks for MSD module, 3D > 2D > 1D ordering (most preferred to least preferred). |
| 72 | static const std::vector<audio_channel_mask_t> surroundChannelMasksOrder = {{ |
| 73 | AUDIO_CHANNEL_OUT_3POINT1POINT2, AUDIO_CHANNEL_OUT_3POINT0POINT2, |
| 74 | AUDIO_CHANNEL_OUT_2POINT1POINT2, AUDIO_CHANNEL_OUT_2POINT0POINT2, |
| 75 | AUDIO_CHANNEL_OUT_5POINT1, AUDIO_CHANNEL_OUT_STEREO }}; |
| 76 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 77 | // ---------------------------------------------------------------------------- |
| 78 | // AudioPolicyInterface implementation |
| 79 | // ---------------------------------------------------------------------------- |
| 80 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 81 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 82 | audio_policy_dev_state_t state, |
| 83 | const char *device_address, |
| 84 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 85 | { |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 86 | status_t status = setDeviceConnectionStateInt(device, state, device_address, device_name); |
| 87 | nextAudioPortGeneration(); |
| 88 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 89 | } |
| 90 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 91 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
| 92 | audio_policy_dev_state_t state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 93 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 94 | AudioParameter param(device->address()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 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 | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 97 | param.addInt(key, device->type()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 98 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 99 | } |
| 100 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 101 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
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", |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 107 | deviceType, 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 |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 110 | if (!audio_is_output_device(deviceType) && !audio_is_input_device(deviceType)) return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 111 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 112 | sp<DeviceDescriptor> device = |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 113 | mHwModules.getDeviceDescriptor(deviceType, device_address, device_name, |
| 114 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 115 | if (device == 0) { |
| 116 | return INVALID_OPERATION; |
| 117 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 118 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 119 | // handle output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 120 | if (audio_is_output_device(deviceType)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 121 | SortedVector <audio_io_handle_t> outputs; |
| 122 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 123 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 124 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 125 | // save a copy of the opened output descriptors before any output is opened or closed |
| 126 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 127 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 128 | switch (state) |
| 129 | { |
| 130 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 131 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 132 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 133 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 134 | return INVALID_OPERATION; |
| 135 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 136 | ALOGV("%s() connecting device %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 137 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 138 | // register new device as available |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 139 | if (mAvailableOutputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 140 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 141 | } |
| 142 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 143 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 144 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 145 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 146 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 147 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 148 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 149 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 150 | mHwModules.cleanUpForDevice(device); |
| 151 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 152 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 153 | return INVALID_OPERATION; |
| 154 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 155 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 156 | mEngine->setDeviceConnectionState(device, state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 157 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 158 | // outputs should never be empty here |
| 159 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 160 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 161 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 162 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 163 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 164 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 165 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 166 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 167 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 168 | return INVALID_OPERATION; |
| 169 | } |
| 170 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 171 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 172 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 173 | // Send Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 174 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 175 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 176 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 177 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 178 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 179 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 180 | |
| 181 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 182 | mEngine->setDeviceConnectionState(device, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | } break; |
| 184 | |
| 185 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 186 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 187 | return BAD_VALUE; |
| 188 | } |
| 189 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 190 | checkForDeviceAndOutputChanges([&]() { |
| 191 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 192 | if (!outputs.isEmpty()) { |
| 193 | for (audio_io_handle_t output : outputs) { |
| 194 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 195 | // close unused outputs after device disconnection or direct outputs that have |
| 196 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 197 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 198 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 199 | (desc->mDirectOpenCount == 0))) { |
| 200 | closeOutput(output); |
| 201 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 202 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 203 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 204 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 205 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 206 | return false; |
| 207 | }); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 208 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 209 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 210 | DeviceVector newDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 211 | updateCallRouting(newDevices); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 212 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 213 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 214 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 215 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 216 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 217 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 218 | // do not force device change on duplicated output because if device is 0, it will |
| 219 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 220 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 221 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 222 | && !desc->isDuplicated() |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 223 | && (!device_distinguishes_on_address(deviceType) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 224 | // always force when disconnecting (a non-duplicated device) |
| 225 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 226 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 227 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 230 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 231 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 234 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 235 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 236 | } // end if is output device |
| 237 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 238 | // handle input devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 239 | if (audio_is_input_device(deviceType)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 240 | SortedVector <audio_io_handle_t> inputs; |
| 241 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 242 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 243 | switch (state) |
| 244 | { |
| 245 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 246 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 247 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 248 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 249 | return INVALID_OPERATION; |
| 250 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 251 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 252 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 253 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 254 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 255 | if (checkInputsForDevice(device, state, inputs) != NO_ERROR) { |
| 256 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 257 | |
| 258 | mHwModules.cleanUpForDevice(device); |
| 259 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 260 | return INVALID_OPERATION; |
| 261 | } |
| 262 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 263 | if (mAvailableInputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 264 | return NO_MEMORY; |
| 265 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 266 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 267 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 268 | mEngine->setDeviceConnectionState(device, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 269 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 270 | |
| 271 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 272 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 273 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 274 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 275 | return INVALID_OPERATION; |
| 276 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 277 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 278 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 279 | |
| 280 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 281 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 282 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 283 | checkInputsForDevice(device, state, inputs); |
| 284 | mAvailableInputDevices.remove(device); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 285 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 286 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 287 | mEngine->setDeviceConnectionState(device, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 288 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 289 | |
| 290 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 291 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 | return BAD_VALUE; |
| 293 | } |
| 294 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 295 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 296 | // As the input device list can impact the output device selection, update |
| 297 | // getDeviceForStrategy() cache |
| 298 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 299 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 300 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 301 | DeviceVector newDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 302 | updateCallRouting(newDevices); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 305 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 306 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 309 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 310 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 311 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 313 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | return BAD_VALUE; |
| 315 | } |
| 316 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 317 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 318 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 319 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 320 | sp<DeviceDescriptor> devDesc = |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 321 | mHwModules.getDeviceDescriptor(device, device_address, "", false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 322 | (strlen(device_address) != 0)/*matchAddress*/); |
| 323 | |
| 324 | if (devDesc == 0) { |
| 325 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 326 | device, device_address); |
| 327 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 328 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 329 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 330 | DeviceVector *deviceVector; |
| 331 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 333 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 334 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 335 | deviceVector = &mAvailableInputDevices; |
| 336 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 337 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 338 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 339 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 340 | |
| 341 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 342 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 345 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 346 | const char *device_address, |
| 347 | const char *device_name) |
| 348 | { |
| 349 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 350 | String8 reply; |
| 351 | AudioParameter param; |
| 352 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 353 | |
| 354 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 355 | device, device_address, device_name); |
| 356 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 357 | // connect/disconnect only 1 device at a time |
| 358 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 359 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 360 | // Check if the device is currently connected |
| 361 | sp<DeviceDescriptor> devDesc = |
| 362 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 363 | if (devDesc == 0 || mAvailableOutputDevices.indexOf(devDesc) < 0) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 364 | // Nothing to do: device is not connected |
| 365 | return NO_ERROR; |
| 366 | } |
| 367 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 368 | // For offloaded A2DP, Hw modules may have the capability to |
| 369 | // configure codecs. Check if any of the loaded hw modules |
| 370 | // supports this. |
| 371 | // If supported, send a set parameter to configure A2DP codecs |
| 372 | // and return. No need to toggle device state. |
| 373 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 374 | reply = mpClientInterface->getParameters( |
| 375 | AUDIO_IO_HANDLE_NONE, |
| 376 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 377 | AudioParameter repliedParameters(reply); |
| 378 | repliedParameters.getInt( |
| 379 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 380 | if (isReconfigA2dpSupported) { |
| 381 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 382 | param.add(key, String8("true")); |
| 383 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 384 | return NO_ERROR; |
| 385 | } |
| 386 | } |
| 387 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 388 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 389 | // This will force reading again the device configuration |
| 390 | status = setDeviceConnectionState(device, |
| 391 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 392 | device_address, device_name); |
| 393 | if (status != NO_ERROR) { |
| 394 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 395 | status); |
| 396 | return status; |
| 397 | } |
| 398 | |
| 399 | status = setDeviceConnectionState(device, |
| 400 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 401 | device_address, device_name); |
| 402 | if (status != NO_ERROR) { |
| 403 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 404 | status); |
| 405 | return status; |
| 406 | } |
| 407 | |
| 408 | return NO_ERROR; |
| 409 | } |
| 410 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 411 | uint32_t AudioPolicyManager::updateCallRouting(const DeviceVector &rxDevices, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 412 | { |
| 413 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 414 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 415 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 416 | if(!hasPrimaryOutput() || mPrimaryOutput->devices().types() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 417 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 418 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 419 | ALOG_ASSERT(!rxDevices.isEmpty(), "updateCallRouting() no selected output device"); |
| 420 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 421 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
| 422 | auto txDevice = getDeviceAndMixForAttributes(attr); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 423 | ALOGV("updateCallRouting device rxDevice %s txDevice %s", |
| 424 | rxDevices.toString().c_str(), txDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 425 | |
| 426 | // release existing RX patch if any |
| 427 | if (mCallRxPatch != 0) { |
| 428 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 429 | mCallRxPatch.clear(); |
| 430 | } |
| 431 | // release TX patch if any |
| 432 | if (mCallTxPatch != 0) { |
| 433 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 434 | mCallTxPatch.clear(); |
| 435 | } |
| 436 | |
| 437 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 438 | // via setOutputDevice() on primary output. |
| 439 | // Otherwise, create two audio patches for TX and RX path. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 440 | if (availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) { |
| 441 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 442 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 443 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 444 | if (!availablePrimaryModuleInputDevices().contains(txDevice)) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 445 | createTxPatch = true; |
| 446 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 447 | } else { // create RX path audio patch |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 448 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevices.itemAt(0), delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 449 | createTxPatch = true; |
| 450 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 451 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 452 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 453 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 454 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 455 | return muteWaitMs; |
| 456 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 457 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 458 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 459 | bool isRx, const sp<DeviceDescriptor> &device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 460 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 461 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 462 | if (device == nullptr) { |
| 463 | return nullptr; |
| 464 | } |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 465 | if (isRx) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 466 | patchBuilder.addSink(device). |
| 467 | addSource(mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 468 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 469 | patchBuilder.addSource(device). |
| 470 | addSink(mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 471 | } |
| 472 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 473 | // @TODO: still ignoring the address, or not dealing platform with mutliple telephonydevices |
| 474 | const sp<DeviceDescriptor> outputDevice = isRx ? |
| 475 | device : mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 476 | SortedVector<audio_io_handle_t> outputs = |
| 477 | getOutputsForDevices(DeviceVector(outputDevice), mOutputs); |
| 478 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 479 | // request to reuse existing output stream if one is already opened to reach the target device |
| 480 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 481 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 482 | ALOG_ASSERT(!outputDesc->isDuplicated(), "%s() %s device output %d is duplicated", __func__, |
| 483 | outputDevice->toString().c_str(), output); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 484 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 488 | // terminate active capture if on the same HW module as the call TX source device |
| 489 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 490 | // call TX device but this information is not in the audio patch and logic here must be |
| 491 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 492 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 493 | if (activeDesc->hasSameHwModuleAs(device)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 494 | closeActiveClients(activeDesc); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 497 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 498 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 499 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 500 | status_t status = mpClientInterface->createAudioPatch( |
| 501 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 502 | ALOGW_IF(status != NO_ERROR, |
| 503 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 504 | sp<AudioPatch> audioPatch; |
| 505 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 506 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 507 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 508 | audioPatch->mUid = mUidCached; |
| 509 | } |
| 510 | return audioPatch; |
| 511 | } |
| 512 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 513 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 514 | const DeviceVector& devices, audio_devices_t device) const { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 515 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 516 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 517 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 518 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 519 | } |
| 520 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 521 | audio_devices_t AudioPolicyManager::getModuleDeviceTypes( |
| 522 | const DeviceVector& devices, const char *moduleId) const { |
| 523 | sp<HwModule> mod = mHwModules.getModuleFromName(moduleId); |
| 524 | return mod != 0 ? devices.getDeviceTypesFromHwModule(mod->getHandle()) : AUDIO_DEVICE_NONE; |
| 525 | } |
| 526 | |
| 527 | bool AudioPolicyManager::isDeviceOfModule( |
| 528 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 529 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 530 | if (module != 0) { |
| 531 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 532 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 533 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 534 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 535 | } |
| 536 | return false; |
| 537 | } |
| 538 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 539 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 | { |
| 541 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 542 | // store previous phone state for management of sonification strategy below |
| 543 | int oldState = mEngine->getPhoneState(); |
| 544 | |
| 545 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 546 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 547 | return; |
| 548 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 549 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 550 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 551 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 552 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 553 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 | } |
| 555 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 556 | /** |
| 557 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 558 | * routing command. |
| 559 | */ |
| 560 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 561 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 562 | |
| 563 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 564 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 566 | int delayMs = 0; |
| 567 | if (isStateInCall(state)) { |
| 568 | nsecs_t sysTime = systemTime(); |
| 569 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 570 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 571 | // mute media and sonification strategies and delay device switch by the largest |
| 572 | // latency of any output where either strategy is active. |
| 573 | // 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] | 574 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 575 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 576 | sysTime) || |
| 577 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 578 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 579 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 580 | (delayMs < (int)desc->latency()*2)) { |
| 581 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 583 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 584 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 585 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 586 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 587 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 588 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 589 | } |
| 590 | } |
| 591 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 592 | if (hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 593 | // Note that despite the fact that getNewOutputDevices() is called on the primary output, |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 594 | // the device returned is not necessarily reachable via this output |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 595 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 596 | // force routing command to audio hardware when ending call |
| 597 | // even if no device change is needed |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 598 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 599 | rxDevices = mPrimaryOutput->devices(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 600 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 601 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 602 | if (state == AUDIO_MODE_IN_CALL) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 603 | updateCallRouting(rxDevices, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 604 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 605 | if (mCallRxPatch != 0) { |
| 606 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 607 | mCallRxPatch.clear(); |
| 608 | } |
| 609 | if (mCallTxPatch != 0) { |
| 610 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 611 | mCallTxPatch.clear(); |
| 612 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 613 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 614 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 615 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 616 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 617 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 618 | |
| 619 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 620 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 621 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 622 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 623 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 624 | setOutputDevices(desc, newDevices, !newDevices.isEmpty(), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 628 | if (isStateInCall(state)) { |
| 629 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 630 | // force reevaluating accessibility routing when call starts |
| 631 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // 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] | 635 | if (state == AUDIO_MODE_RINGTONE && |
| 636 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | mLimitRingtoneVolume = true; |
| 638 | } else { |
| 639 | mLimitRingtoneVolume = false; |
| 640 | } |
| 641 | } |
| 642 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 643 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 644 | return mEngine->getPhoneState(); |
| 645 | } |
| 646 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 647 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 648 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 649 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 650 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 651 | if (config == mEngine->getForceUse(usage)) { |
| 652 | return; |
| 653 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 654 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 655 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 656 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 657 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 658 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 659 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 660 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 661 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 662 | |
| 663 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 664 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 665 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 666 | //FIXME: workaround for truncated touch sounds |
| 667 | // to be removed when the problem is handled by system UI |
| 668 | uint32_t delayMs = 0; |
| 669 | uint32_t waitMs = 0; |
| 670 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 671 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 672 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 673 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 674 | DeviceVector newDevices = getNewOutputDevices(mPrimaryOutput, true /*fromCache*/); |
| 675 | waitMs = updateCallRouting(newDevices, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 676 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 677 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 678 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 679 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 680 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 681 | waitMs = setOutputDevices(outputDesc, newDevices, !newDevices.isEmpty(), delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 682 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 683 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 684 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 688 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 689 | auto newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 690 | // Force new input selection if the new device can not be reached via current input |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 691 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 692 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 693 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 694 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 695 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 696 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 699 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 700 | { |
| 701 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 702 | } |
| 703 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 704 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 705 | // search to profiles for direct outputs. |
| 706 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 707 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 708 | uint32_t samplingRate, |
| 709 | audio_format_t format, |
| 710 | audio_channel_mask_t channelMask, |
| 711 | audio_output_flags_t flags, |
| 712 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 713 | { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 714 | if (directOnly) { |
| 715 | // only retain flags that will drive the direct output profile selection |
| 716 | // if explicitly requested |
| 717 | static const uint32_t kRelevantFlags = |
| 718 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 719 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
| 720 | flags = |
| 721 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 722 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 723 | |
| 724 | sp<IOProfile> profile; |
| 725 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 726 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 727 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 728 | if (!curProfile->isCompatibleProfile(devices, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 729 | samplingRate, NULL /*updatedSamplingRate*/, |
| 730 | format, NULL /*updatedFormat*/, |
| 731 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 732 | flags)) { |
| 733 | continue; |
| 734 | } |
| 735 | // reject profiles not corresponding to a device currently available |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 736 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 737 | continue; |
| 738 | } |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 739 | if (!directOnly) return curProfile; |
| 740 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 741 | // to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 742 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 743 | continue; |
| 744 | } |
| 745 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 746 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 747 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 748 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 751 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 754 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 755 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 756 | routing_strategy strategy = getStrategy(stream); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 757 | DeviceVector devices = getDevicesForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 758 | |
| 759 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 760 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 761 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 762 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 763 | // and AudioSystem::getOutputSamplingRate(). |
| 764 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 765 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
| 766 | audio_io_handle_t output = selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 767 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 768 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 769 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 770 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 771 | } |
| 772 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 773 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 774 | const audio_attributes_t *srcAttr, |
| 775 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 776 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 777 | if (srcAttr != NULL) { |
| 778 | if (!isValidAttributes(srcAttr)) { |
| 779 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 780 | __func__, |
| 781 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 782 | srcAttr->tags); |
| 783 | return BAD_VALUE; |
| 784 | } |
| 785 | *dstAttr = *srcAttr; |
| 786 | } else { |
| 787 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 788 | ALOGE("%s: invalid stream type", __func__); |
| 789 | return BAD_VALUE; |
| 790 | } |
| 791 | stream_type_to_audio_attributes(srcStream, dstAttr); |
| 792 | } |
| 793 | return NO_ERROR; |
| 794 | } |
| 795 | |
| 796 | status_t AudioPolicyManager::getOutputForAttrInt(audio_attributes_t *resultAttr, |
| 797 | audio_io_handle_t *output, |
| 798 | audio_session_t session, |
| 799 | const audio_attributes_t *attr, |
| 800 | audio_stream_type_t *stream, |
| 801 | uid_t uid, |
| 802 | const audio_config_t *config, |
| 803 | audio_output_flags_t *flags, |
| 804 | audio_port_handle_t *selectedDeviceId) |
| 805 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 806 | DeviceVector devices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 807 | routing_strategy strategy; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 808 | audio_devices_t deviceType = AUDIO_DEVICE_NONE; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 809 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 810 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 811 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 812 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 813 | if (status != NO_ERROR) { |
| 814 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 815 | } |
| 816 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 817 | ALOGV("%s usage=%d, content=%d, tag=%s flags=%08x" |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 818 | " session %d selectedDeviceId %d", |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 819 | __func__, |
| 820 | resultAttr->usage, resultAttr->content_type, resultAttr->tags, resultAttr->flags, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 821 | session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 822 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 823 | *stream = streamTypefromAttributesInt(resultAttr); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 824 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 825 | strategy = getStrategyForAttr(resultAttr); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 826 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 827 | // First check for explicit routing (eg. setPreferredDevice) |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 828 | sp<DeviceDescriptor> requestedDevice = mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 829 | if (requestedDevice != nullptr) { |
| 830 | deviceType = requestedDevice->type(); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 831 | } else { |
| 832 | // If no explict route, is there a matching dynamic policy that applies? |
| 833 | sp<SwAudioOutputDescriptor> desc; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 834 | if (mPolicyMixes.getOutputForAttr(*resultAttr, uid, desc) == NO_ERROR) { |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 835 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 836 | if (!audio_has_proportional_frames(config->format)) { |
| 837 | return BAD_VALUE; |
| 838 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 839 | *stream = streamTypefromAttributesInt(resultAttr); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 840 | *output = desc->mIoHandle; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 841 | AudioMix *mix = desc->mPolicyMix; |
| 842 | sp<DeviceDescriptor> deviceDesc = |
| 843 | mAvailableOutputDevices.getDevice(mix->mDeviceType, mix->mDeviceAddress); |
| 844 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 845 | ALOGV("%s returns output %d", __func__, *output); |
| 846 | return NO_ERROR; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | // Virtual sources must always be dynamicaly or explicitly routed |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 850 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 851 | ALOGW("%s no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE", __func__); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 852 | return BAD_VALUE; |
| 853 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 854 | deviceType = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 855 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 856 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 857 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 858 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 861 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 862 | // chosen by the engine if not. |
| 863 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 864 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 865 | // TODO: Remove check of AUDIO_STREAM_MUSIC once migration is completed on the app side. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 866 | if (deviceType == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 867 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 868 | audio_is_linear_pcm(config->format) && |
| 869 | isInCall()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 870 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 871 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 872 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 873 | // Get the devce type directly from the engine to bypass preferred route logic |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 874 | deviceType = mEngine->getDeviceForStrategy(strategy); |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 878 | ALOGV("%s device 0x%x, sampling rate %d, format %#x, channel mask %#x, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 879 | "flags %#x", |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 880 | __func__, |
| 881 | deviceType, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 882 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 883 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 884 | if (!msdDevices.isEmpty()) { |
| 885 | *output = getOutputForDevices(msdDevices, session, *stream, config, flags); |
| 886 | sp<DeviceDescriptor> deviceDesc = mAvailableOutputDevices.getDevice(deviceType); |
| 887 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(deviceDesc) == NO_ERROR) { |
| 888 | ALOGV("%s() Using MSD devices %s instead of device %s", |
| 889 | __func__, msdDevices.toString().c_str(), deviceDesc->toString().c_str()); |
| 890 | deviceType = msdDevices.types(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 891 | } else { |
| 892 | *output = AUDIO_IO_HANDLE_NONE; |
| 893 | } |
| 894 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 895 | devices = mAvailableOutputDevices.getDevicesFromTypeMask(deviceType); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 896 | if (*output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 897 | *output = getOutputForDevices(devices, session, *stream, config, flags); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 898 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 899 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 900 | return INVALID_OPERATION; |
| 901 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 902 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 903 | *selectedDeviceId = getFirstDeviceId(devices); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 904 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 905 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 906 | |
| 907 | return NO_ERROR; |
| 908 | } |
| 909 | |
| 910 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 911 | audio_io_handle_t *output, |
| 912 | audio_session_t session, |
| 913 | audio_stream_type_t *stream, |
| 914 | uid_t uid, |
| 915 | const audio_config_t *config, |
| 916 | audio_output_flags_t *flags, |
| 917 | audio_port_handle_t *selectedDeviceId, |
| 918 | audio_port_handle_t *portId) |
| 919 | { |
| 920 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 921 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 922 | return INVALID_OPERATION; |
| 923 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 924 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 925 | audio_attributes_t resultAttr; |
| 926 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
| 927 | config, flags, selectedDeviceId); |
| 928 | if (status != NO_ERROR) { |
| 929 | return status; |
| 930 | } |
| 931 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 932 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 933 | .format = config->format, |
| 934 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 935 | *portId = AudioPort::getNextUniqueId(); |
| 936 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 937 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 938 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 939 | requestedPortId, *stream, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 940 | getStrategyForAttr(&resultAttr), |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 941 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 942 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 943 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 944 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 945 | ALOGV("%s returns output %d selectedDeviceId %d for port ID %d", |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 946 | __func__, *output, requestedPortId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 947 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 948 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 949 | } |
| 950 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 951 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 952 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 953 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 954 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 955 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 956 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 957 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 958 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 959 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 960 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 961 | // open a direct output if required by specified parameters |
| 962 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 963 | // and all common behaviors are driven by checking only the direct flag |
| 964 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 965 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 966 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 967 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 968 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 969 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 970 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 971 | // only allow deep buffering for music stream type |
| 972 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 973 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 974 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 975 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 976 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 977 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 978 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 979 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 980 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 981 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 982 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 983 | audio_is_linear_pcm(config->format) && |
| 984 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 985 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 986 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 987 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 988 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 990 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 991 | sp<IOProfile> profile; |
| 992 | |
| 993 | // 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] | 994 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 995 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 996 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 997 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 998 | goto non_direct_output; |
| 999 | } |
| 1000 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1001 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1002 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1003 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1004 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1005 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1006 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1007 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1008 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1009 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1010 | profile = getProfileForOutput(devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1011 | config->sample_rate, |
| 1012 | config->format, |
| 1013 | config->channel_mask, |
| 1014 | (audio_output_flags_t)*flags, |
| 1015 | true /* directOnly */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1018 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1019 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1020 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1021 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1022 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1023 | // reuse direct output if currently open by the same client |
| 1024 | // and configured with same parameters |
| 1025 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1026 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1027 | (config->channel_mask == desc->mChannelMask) && |
| 1028 | (session == desc->mDirectClientSession)) { |
| 1029 | desc->mDirectOpenCount++; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1030 | ALOGI("%s reusing direct output %d for session %d", __func__, |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1031 | mOutputs.keyAt(i), session); |
| 1032 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1036 | |
| 1037 | if (!profile->canOpenNewIo()) { |
| 1038 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1039 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1040 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1041 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1042 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1043 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1044 | String8 address = getFirstDeviceAddress(devices); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1045 | |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 1046 | // MSD patch may be using the only output stream that can service this request. Release |
| 1047 | // MSD patch to prioritize this request over any active output on MSD. |
| 1048 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 1049 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1050 | const auto& patch = msdPatches[i]; |
| 1051 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1052 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1053 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1054 | (sink->ext.device.type & devices.types()) != AUDIO_DEVICE_NONE && |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 1055 | (address.isEmpty() || strncmp(sink->ext.device.address, address.string(), |
| 1056 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 1057 | releaseAudioPatch(patch->mHandle, mUidCached); |
| 1058 | break; |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1063 | status = outputDesc->open(config, devices, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1064 | |
| 1065 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1066 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1067 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1068 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1069 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1070 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1071 | "format %d %d, channel mask %04x %04x", __func__, output, config->sample_rate, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1072 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1073 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1074 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1075 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1076 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1077 | // 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] | 1078 | if (audio_is_linear_pcm(config->format) && |
| 1079 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1080 | goto non_direct_output; |
| 1081 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1082 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1083 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1084 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1085 | outputDesc->mDirectClientSession = session; |
| 1086 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1087 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1088 | mPreviousOutputs = mOutputs; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1089 | ALOGV("%s returns new direct output %d", __func__, output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1090 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1091 | return output; |
| 1092 | } |
| 1093 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1094 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1095 | |
| 1096 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1097 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1098 | 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] | 1099 | return AUDIO_IO_HANDLE_NONE; |
| 1100 | } |
| 1101 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1102 | // ignoring channel mask due to downmix capability in mixer |
| 1103 | |
| 1104 | // open a non direct output |
| 1105 | |
| 1106 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1107 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1108 | // get which output is suitable for the specified stream. The actual |
| 1109 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1110 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1111 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1112 | // 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] | 1113 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1114 | output = selectOutput(outputs, *flags, config->format, |
| 1115 | config->channel_mask, config->sample_rate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1116 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1117 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1118 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1119 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1120 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1121 | return output; |
| 1122 | } |
| 1123 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1124 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1125 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1126 | mAvailableInputDevices); |
| 1127 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1128 | } |
| 1129 | |
| 1130 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1131 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1132 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1133 | } |
| 1134 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1135 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1136 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1137 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1138 | if (msdModule != 0) { |
| 1139 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1140 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1141 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1142 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1143 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1144 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1145 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1146 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1147 | } |
| 1148 | } |
| 1149 | } |
| 1150 | return msdPatches; |
| 1151 | } |
| 1152 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1153 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(const sp<DeviceDescriptor> &outputDevice, |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1154 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1155 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1156 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1157 | if (msdModule == nullptr) { |
| 1158 | ALOGE("%s() unable to get MSD module", __func__); |
| 1159 | return NO_INIT; |
| 1160 | } |
| 1161 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice); |
| 1162 | if (deviceModule == nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1163 | ALOGE("%s() unable to get module for %s", __func__, outputDevice->toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1164 | return NO_INIT; |
| 1165 | } |
| 1166 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1167 | if (inputProfiles.isEmpty()) { |
| 1168 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1169 | return NO_INIT; |
| 1170 | } |
| 1171 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1172 | if (outputProfiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1173 | ALOGE("%s() no output profiles for device %s", __func__, outputDevice->toString().c_str()); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1174 | return NO_INIT; |
| 1175 | } |
| 1176 | AudioProfileVector msdProfiles; |
| 1177 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1178 | for (const auto &inProfile : inputProfiles) { |
| 1179 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1180 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1181 | } |
| 1182 | } |
| 1183 | AudioProfileVector deviceProfiles; |
| 1184 | for (const auto &outProfile : outputProfiles) { |
| 1185 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1186 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1187 | } |
| 1188 | } |
| 1189 | struct audio_config_base bestSinkConfig; |
| 1190 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1191 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1192 | &bestSinkConfig); |
| 1193 | if (result != NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1194 | ALOGD("%s() no matching profiles found for device: %s, hwAvSync: %d", |
| 1195 | __func__, outputDevice->toString().c_str(), hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1196 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1197 | } |
| 1198 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1199 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1200 | sinkConfig->format = bestSinkConfig.format; |
| 1201 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1202 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1203 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1204 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1205 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1206 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1207 | sourceConfig->format = bestSinkConfig.format; |
| 1208 | // Copy input stream directly without any processing (e.g. resampling). |
| 1209 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1210 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1211 | if (hwAvSync) { |
| 1212 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1213 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1214 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1215 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1216 | } |
| 1217 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1218 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1219 | sinkConfig->config_mask |= config_mask; |
| 1220 | sourceConfig->config_mask |= config_mask; |
| 1221 | return NO_ERROR; |
| 1222 | } |
| 1223 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1224 | PatchBuilder AudioPolicyManager::buildMsdPatch(const sp<DeviceDescriptor> &outputDevice) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1225 | { |
| 1226 | PatchBuilder patchBuilder; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1227 | patchBuilder.addSource(getMsdAudioInDevice()).addSink(outputDevice); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1228 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1229 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1230 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1231 | // For now, we just forcefully try with HwAvSync first. |
| 1232 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1233 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1234 | getBestMsdAudioProfileFor( |
| 1235 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1236 | if (res == NO_ERROR) { |
| 1237 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1238 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1239 | } |
| 1240 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1241 | " supporting PCM format conversion.", __func__); |
| 1242 | return patchBuilder; |
| 1243 | } |
| 1244 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1245 | status_t AudioPolicyManager::setMsdPatch(const sp<DeviceDescriptor> &outputDevice) { |
| 1246 | sp<DeviceDescriptor> device = outputDevice; |
| 1247 | if (device == nullptr) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1248 | // Use media strategy for unspecified output device. This should only |
| 1249 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1250 | // therefore invalidate explicit routing requests. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1251 | DeviceVector devices = getDevicesForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1252 | LOG_ALWAYS_FATAL_IF(devices.isEmpty(), "no outpudevice to set Msd Patch"); |
| 1253 | device = devices.itemAt(0); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1254 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1255 | ALOGV("%s() for device %s", __func__, device->toString().c_str()); |
| 1256 | PatchBuilder patchBuilder = buildMsdPatch(device); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1257 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1258 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1259 | if (!msdPatches.isEmpty()) { |
| 1260 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1261 | "The current MSD prototype only supports one output patch"); |
| 1262 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1263 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1264 | return NO_ERROR; |
| 1265 | } |
| 1266 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1267 | } |
| 1268 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1269 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1270 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1271 | ALOGI_IF(status == NO_ERROR, "%s() Patch created from MSD_IN to " |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1272 | "device:%s (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1273 | device->toString().c_str(), patch->sources[0].format, |
| 1274 | patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1275 | return status; |
| 1276 | } |
| 1277 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1278 | 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] | 1279 | audio_output_flags_t flags, |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1280 | audio_format_t format, |
| 1281 | audio_channel_mask_t channelMask, |
| 1282 | uint32_t samplingRate) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1283 | { |
| 1284 | // select one output among several that provide a path to a particular device or set of |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1285 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1286 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1287 | // 1: the output supporting haptic playback when requesting haptic playback |
| 1288 | // 2: the output with the highest number of requested policy flags |
| 1289 | // 3: the output with the bit depth the closest to the requested one |
| 1290 | // 4: the primary output |
| 1291 | // 5: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1292 | |
| 1293 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1294 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1295 | } |
| 1296 | if (outputs.size() == 1) { |
| 1297 | return outputs[0]; |
| 1298 | } |
| 1299 | |
| 1300 | int maxCommonFlags = 0; |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1301 | const size_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1302 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1303 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1304 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1305 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1306 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1307 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1308 | |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1309 | // Flags which must be present on both the request and the selected output |
| 1310 | static const audio_output_flags_t kMandatedFlags = (audio_output_flags_t) |
| 1311 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1312 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1313 | for (audio_io_handle_t output : outputs) { |
| 1314 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1315 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1316 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1317 | continue; |
| 1318 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1319 | // If haptic channel is specified, use the haptic output if present. |
| 1320 | // When using haptic output, same audio format and sample rate are required. |
| 1321 | if (hapticChannelCount > 0) { |
| 1322 | // If haptic channel is specified, use the first output that |
| 1323 | // support haptic playback. |
| 1324 | if (audio_channel_count_from_out_mask( |
| 1325 | outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) >= hapticChannelCount |
| 1326 | && format == outputDesc->mFormat |
| 1327 | && samplingRate == outputDesc->mSamplingRate) { |
| 1328 | return output; |
| 1329 | } |
| 1330 | } else { |
| 1331 | // When haptic channel is not specified, skip haptic output. |
| 1332 | if (outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 1333 | continue; |
| 1334 | } |
| 1335 | } |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1336 | if ((kMandatedFlags & flags) != |
| 1337 | (kMandatedFlags & outputDesc->mProfile->getFlags())) { |
| 1338 | continue; |
| 1339 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1340 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1341 | // if a valid format is specified, skip output if not compatible |
| 1342 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1343 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1344 | continue; |
| 1345 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1346 | if (AudioPort::isBetterFormatMatch( |
| 1347 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1348 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1349 | bestFormat = outputDesc->mFormat; |
| 1350 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1353 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1354 | if (commonFlags >= maxCommonFlags) { |
| 1355 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1356 | if (format != AUDIO_FORMAT_INVALID |
| 1357 | && AudioPort::isBetterFormatMatch( |
| 1358 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1359 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1360 | bestFormatForFlags = outputDesc->mFormat; |
| 1361 | } |
| 1362 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1363 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1364 | maxCommonFlags = commonFlags; |
| 1365 | bestFormatForFlags = outputDesc->mFormat; |
| 1366 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1367 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1368 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1369 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1370 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1375 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1376 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1377 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1378 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1379 | return outputForFormat; |
| 1380 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1381 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1382 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | return outputs[0]; |
| 1386 | } |
| 1387 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1388 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1389 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1390 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1391 | |
| 1392 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1393 | if (outputDesc == 0) { |
| 1394 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1395 | return BAD_VALUE; |
| 1396 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1397 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1398 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1399 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1400 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1401 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1402 | status_t status = outputDesc->start(); |
| 1403 | if (status != NO_ERROR) { |
| 1404 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1405 | } |
| 1406 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1407 | uint32_t delayMs; |
| 1408 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1409 | |
| 1410 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1411 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1412 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1413 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1414 | if (delayMs != 0) { |
| 1415 | usleep(delayMs * 1000); |
| 1416 | } |
| 1417 | |
| 1418 | return status; |
| 1419 | } |
| 1420 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1421 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1422 | const sp<TrackClientDescriptor>& client, |
| 1423 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1424 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1425 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1426 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1427 | |
| 1428 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1429 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1430 | if (stream == AUDIO_STREAM_TTS) { |
| 1431 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1432 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1433 | return INVALID_OPERATION; |
| 1434 | } else { |
| 1435 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1436 | } |
| 1437 | } else { |
| 1438 | // some playback other than beacon starts |
| 1439 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1440 | } |
| 1441 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1442 | // 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] | 1443 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1444 | bool force = !outputDesc->isActive() && |
| 1445 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1446 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1447 | DeviceVector devices; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1448 | AudioMix *policyMix = NULL; |
| 1449 | const char *address = NULL; |
| 1450 | if (outputDesc->mPolicyMix != NULL) { |
| 1451 | policyMix = outputDesc->mPolicyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1452 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1453 | address = policyMix->mDeviceAddress.string(); |
| 1454 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1455 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1456 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1457 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1458 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1459 | devices.add(mAvailableOutputDevices.getDevice(newDeviceType, String8(address))); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1462 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1463 | // It covers a common case when there is no materially active audio |
| 1464 | // and muting would result in unnecessary delay and dropped audio. |
| 1465 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1466 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1467 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1468 | // increment usage count for this stream on the requested output: |
| 1469 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1470 | // 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] | 1471 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1472 | |
| 1473 | if (client->hasPreferredDevice(true)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1474 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 1475 | if (devices != outputDesc->devices()) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1476 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1477 | } |
| 1478 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1479 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1480 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1481 | selectOutputForMusicEffects(); |
| 1482 | } |
| 1483 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1484 | if (outputDesc->streamActiveCount(stream) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1485 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1486 | if (devices.isEmpty()) { |
| 1487 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1488 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1489 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1490 | routing_strategy strategy = getStrategy(stream); |
| 1491 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1492 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1493 | (beaconMuteLatency > 0); |
| 1494 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1495 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1496 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1497 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1498 | // An output has a shared device if |
| 1499 | // - managed by the same hw module |
| 1500 | // - supports the currently selected device |
| 1501 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1502 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1503 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1504 | // force a device change if any other output is: |
| 1505 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1506 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1507 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1508 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1509 | // 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] | 1510 | // change the device currently selected by the other output. |
| 1511 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1512 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1513 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1514 | force = true; |
| 1515 | } |
| 1516 | // 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] | 1517 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1518 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1519 | const uint32_t latencyMs = desc->latency(); |
| 1520 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1521 | |
| 1522 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1523 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1524 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1525 | |
| 1526 | // Require mute check if another output is on a shared device |
| 1527 | // and currently active to have proper drain and avoid pops. |
| 1528 | // Note restoring AudioTracks onto this output needs to invoke |
| 1529 | // a volume ramp if there is no mute. |
| 1530 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1531 | } |
| 1532 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1533 | |
| 1534 | const uint32_t muteWaitMs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1535 | setOutputDevices(outputDesc, devices, force, 0, NULL, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1536 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1537 | // apply volume rules for current stream and device if necessary |
| 1538 | checkAndSetVolume(stream, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1539 | mVolumeCurves->getVolumeIndex(stream, outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1540 | outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1541 | outputDesc->devices().types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1542 | |
| 1543 | // update the outputs if starting an output with a stream that can affect notification |
| 1544 | // routing |
| 1545 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1546 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1547 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1548 | if (strategy == STRATEGY_SONIFICATION) { |
| 1549 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1550 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1551 | |
| 1552 | if (waitMs > muteWaitMs) { |
| 1553 | *delayMs = waitMs - muteWaitMs; |
| 1554 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1555 | |
| 1556 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1557 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1558 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1559 | // change occurs after the MixerThread starts and causes a stream volume |
| 1560 | // glitch. |
| 1561 | // |
| 1562 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1563 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1564 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1565 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1566 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1567 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1568 | } |
| 1569 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1570 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1571 | // of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1572 | if (audio_is_remote_submix_device(devices.types()) && policyMix != NULL && |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1573 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1574 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1575 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1576 | address, |
| 1577 | "remote-submix"); |
| 1578 | } |
| 1579 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | return NO_ERROR; |
| 1581 | } |
| 1582 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1583 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1584 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1585 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1586 | |
| 1587 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1588 | if (outputDesc == 0) { |
| 1589 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1590 | return BAD_VALUE; |
| 1591 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1592 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1593 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1594 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1595 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1596 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1597 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1598 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1599 | if (status == NO_ERROR ) { |
| 1600 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1601 | } |
| 1602 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1605 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1606 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1607 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1608 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1609 | audio_stream_type_t stream = client->stream(); |
| 1610 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1611 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1612 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1613 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1614 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1615 | // Automatically disable the remote submix input when output is stopped on a |
| 1616 | // re routing mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1617 | if (audio_is_remote_submix_device(outputDesc->devices().types()) && |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1618 | outputDesc->mPolicyMix != NULL && |
| 1619 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1620 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1621 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1622 | outputDesc->mPolicyMix->mDeviceAddress, |
| 1623 | "remote-submix"); |
| 1624 | } |
| 1625 | } |
| 1626 | bool forceDeviceUpdate = false; |
| 1627 | if (client->hasPreferredDevice(true)) { |
| 1628 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1629 | forceDeviceUpdate = true; |
| 1630 | } |
| 1631 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1632 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1633 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1634 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1635 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1636 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1637 | outputDesc->mStopTime[stream] = systemTime(); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1638 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1639 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1640 | // the track stop() command is received and at that time the audio track buffer can |
| 1641 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1642 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1643 | // audio path (audio DSP, CODEC ...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1644 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1645 | |
| 1646 | // force restoring the device selection on other active outputs if it differs from the |
| 1647 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1648 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1649 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1650 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1651 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1652 | desc->isActive() && |
| 1653 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1654 | (newDevices != desc->devices())) { |
| 1655 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 1656 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1657 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1658 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 1659 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1660 | // re-apply device specific volume if not done by setOutputDevice() |
| 1661 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1662 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1663 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1667 | handleNotificationRoutingForStream(stream); |
| 1668 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1669 | |
| 1670 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1671 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1672 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1673 | } |
| 1674 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1675 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1676 | selectOutputForMusicEffects(); |
| 1677 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | return NO_ERROR; |
| 1679 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1680 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1681 | return INVALID_OPERATION; |
| 1682 | } |
| 1683 | } |
| 1684 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1685 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1686 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1687 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1688 | |
| 1689 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1690 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1691 | // If an output descriptor is closed due to a device routing change, |
| 1692 | // then there are race conditions with releaseOutput from tracks |
| 1693 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1694 | // destroyed shortly thereafter. |
| 1695 | // |
| 1696 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1697 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1698 | return; |
| 1699 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1700 | |
| 1701 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1702 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1703 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1704 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1705 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1706 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1707 | return; |
| 1708 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1709 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1710 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1711 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1712 | } |
| 1713 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1714 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1715 | // otherwise there may be inaccurate stream reference counts. |
| 1716 | // This is checked in outputDesc->removeClient below. |
| 1717 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1720 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1721 | audio_io_handle_t *input, |
| 1722 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1723 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1724 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1725 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1726 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1727 | input_type_t *inputType, |
| 1728 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1729 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1730 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1731 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1732 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1733 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1734 | status_t status = NO_ERROR; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1735 | audio_source_t halInputSource; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1736 | audio_attributes_t attributes = *attr; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1737 | AudioMix *policyMix = NULL; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1738 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1739 | sp<AudioInputDescriptor> inputDesc; |
| 1740 | sp<RecordClientDescriptor> clientDesc; |
| 1741 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1742 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1743 | |
| 1744 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1745 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1746 | return INVALID_OPERATION; |
| 1747 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1748 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1749 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 1750 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1751 | } |
| 1752 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1753 | // Explicit routing? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1754 | sp<DeviceDescriptor> explicitRoutingDevice = |
| 1755 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1756 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1757 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1758 | // possible |
| 1759 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1760 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1761 | ssize_t index = mInputs.indexOfKey(*input); |
| 1762 | if (index < 0) { |
| 1763 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1764 | status = BAD_VALUE; |
| 1765 | goto error; |
| 1766 | } |
| 1767 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1768 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1769 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1770 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1771 | status = BAD_VALUE; |
| 1772 | goto error; |
| 1773 | } |
| 1774 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1775 | // 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] | 1776 | // corresponds to a new client and is only permitted from the same UID. |
| 1777 | // 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] | 1778 | if (clients.size() > 1) { |
| 1779 | for (const auto& client : clients) { |
| 1780 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1781 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1782 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1783 | // to an actual client |
| 1784 | if (client == *clients.cbegin()) { |
| 1785 | continue; |
| 1786 | } |
| 1787 | if (uid != client->uid() && !client->isSilenced()) { |
| 1788 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1789 | uid, client->portId(), client->uid()); |
| 1790 | status = INVALID_OPERATION; |
| 1791 | goto error; |
| 1792 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1793 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1794 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1795 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1796 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1797 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1798 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1799 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1800 | } |
| 1801 | |
| 1802 | *input = AUDIO_IO_HANDLE_NONE; |
| 1803 | *inputType = API_INPUT_INVALID; |
| 1804 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1805 | halInputSource = attributes.source; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1806 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1807 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 1808 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 1809 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1810 | if (status != NO_ERROR) { |
| 1811 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1812 | } |
| 1813 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1814 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1815 | String8(attr->tags + strlen("addr="))); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1816 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1817 | if (explicitRoutingDevice != nullptr) { |
| 1818 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1819 | } else { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1820 | device = getDeviceAndMixForAttributes(attributes, &policyMix); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1821 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1822 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1823 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1824 | status = BAD_VALUE; |
| 1825 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1826 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1827 | if (policyMix != nullptr) { |
| 1828 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 1829 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1830 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1831 | // know about it and is therefore considered "legacy" |
| 1832 | *inputType = API_INPUT_LEGACY; |
| 1833 | } else if (audio_is_remote_submix_device(device->type())) { |
| 1834 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8("0")); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1835 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1836 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1837 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1838 | } else { |
| 1839 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1840 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1841 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1842 | } |
| 1843 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1844 | *input = getInputForDevice(device, session, attributes.source, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1845 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1846 | policyMix); |
| 1847 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1848 | status = INVALID_OPERATION; |
| 1849 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1850 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1851 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1852 | exit: |
| 1853 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1854 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 1855 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1856 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1857 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1858 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1859 | *portId = AudioPort::getNextUniqueId(); |
| 1860 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1861 | clientDesc = new RecordClientDescriptor(*portId, uid, session, *attr, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 1862 | requestedDeviceId, attributes.source, flags, |
| 1863 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1864 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1865 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1866 | |
| 1867 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1868 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1869 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1870 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1871 | |
| 1872 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1873 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1874 | } |
| 1875 | |
| 1876 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1877 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1878 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1879 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1880 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1881 | audio_input_flags_t flags, |
| 1882 | AudioMix *policyMix) |
| 1883 | { |
| 1884 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1885 | audio_source_t halInputSource = inputSource; |
| 1886 | bool isSoundTrigger = false; |
| 1887 | |
| 1888 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1889 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1890 | if (index >= 0) { |
| 1891 | input = mSoundTriggerSessions.valueFor(session); |
| 1892 | isSoundTrigger = true; |
| 1893 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1894 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1895 | } else { |
| 1896 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1897 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1898 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1899 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1900 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1903 | // find a compatible input profile (not necessarily identical in parameters) |
| 1904 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1905 | // sampling rate and flags may be updated by getInputProfile |
| 1906 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1907 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1908 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1909 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1910 | audio_input_flags_t profileFlags = flags; |
| 1911 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1912 | profileFormat = config->format; // reset each time through loop, in case it is updated |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1913 | profile = getInputProfile(device, profileSamplingRate, profileFormat, profileChannelMask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1914 | profileFlags); |
| 1915 | if (profile != 0) { |
| 1916 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1917 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1918 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1919 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1920 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1921 | } else { // fail |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1922 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 1923 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 1924 | config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1925 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1926 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1927 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1928 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1929 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1930 | if (samplingRate == 0) { |
| 1931 | samplingRate = profileSamplingRate; |
| 1932 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1933 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1934 | if (profile->getModuleHandle() == 0) { |
| 1935 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1936 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1937 | } |
| 1938 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1939 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 1940 | for (size_t i = 0; i < mInputs.size(); ) { |
| 1941 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 1942 | if (desc->mProfile != profile) { |
| 1943 | continue; |
| 1944 | } |
| 1945 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 1946 | // else |
| 1947 | // reuse input if active client app is not in IDLE state |
| 1948 | // |
| 1949 | RecordClientVector clients = desc->clientsList(); |
| 1950 | bool doClose = false; |
| 1951 | for (const auto& client : clients) { |
| 1952 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 1953 | continue; |
| 1954 | } |
| 1955 | if (client->isSoundTrigger()) { |
| 1956 | if (session == client->session()) { |
| 1957 | return desc->mIoHandle; |
| 1958 | } |
| 1959 | continue; |
| 1960 | } |
| 1961 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 1962 | return desc->mIoHandle; |
| 1963 | } |
| 1964 | doClose = true; |
| 1965 | } |
| 1966 | if (doClose) { |
| 1967 | closeInput(desc->mIoHandle); |
| 1968 | } else { |
| 1969 | i++; |
| 1970 | } |
| 1971 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1972 | } |
| 1973 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1974 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1975 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1976 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 1977 | lConfig.sample_rate = profileSamplingRate; |
| 1978 | lConfig.channel_mask = profileChannelMask; |
| 1979 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1980 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1981 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1982 | |
| 1983 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1984 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1985 | (profileSamplingRate != lConfig.sample_rate) || |
| 1986 | !audio_formats_match(profileFormat, lConfig.format) || |
| 1987 | (profileChannelMask != lConfig.channel_mask)) { |
| 1988 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1989 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1990 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1991 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1992 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1993 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1994 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1997 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1998 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1999 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2000 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2001 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2002 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2005 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2006 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2007 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2008 | |
| 2009 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2010 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2011 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2012 | return BAD_VALUE; |
| 2013 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2014 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2015 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2016 | if (client->active()) { |
| 2017 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2018 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2019 | } |
| 2020 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2021 | audio_session_t session = client->session(); |
| 2022 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2023 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2024 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2025 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2026 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2027 | status_t status = inputDesc->start(); |
| 2028 | if (status != NO_ERROR) { |
| 2029 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2030 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2031 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2032 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2033 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2034 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2035 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2036 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2037 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2038 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2039 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2041 | if (inputDesc->activeCount() == 1) { |
| 2042 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2043 | if ((inputDesc->mPolicyMix != NULL) |
| 2044 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2045 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2046 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2047 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2048 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2049 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2050 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2051 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2052 | SoundTrigger::setCaptureState(true); |
| 2053 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2054 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2055 | // automatically enable the remote submix output when input is started if not |
| 2056 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2057 | // For remote submix (a virtual device), we open only one input per capture request. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2058 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2059 | String8 address = String8(""); |
| 2060 | if (inputDesc->mPolicyMix == NULL) { |
| 2061 | address = String8("0"); |
| 2062 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2063 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2064 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2065 | if (address != "") { |
| 2066 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2067 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2068 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2069 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2070 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2071 | } |
| 2072 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2073 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2074 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2075 | return NO_ERROR; |
| 2076 | } |
| 2077 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2078 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2079 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2080 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2081 | |
| 2082 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2083 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2084 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2085 | return BAD_VALUE; |
| 2086 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2087 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2088 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2089 | if (!client->active()) { |
| 2090 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2091 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2092 | } |
| 2093 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2094 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2095 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2096 | inputDesc->stop(); |
| 2097 | if (inputDesc->isActive()) { |
| 2098 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2099 | } else { |
| 2100 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2101 | if ((inputDesc->mPolicyMix != NULL) |
| 2102 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2103 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2104 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2105 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2106 | |
| 2107 | // automatically disable the remote submix output when input is stopped if not |
| 2108 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2109 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2110 | String8 address = String8(""); |
| 2111 | if (inputDesc->mPolicyMix == NULL) { |
| 2112 | address = String8("0"); |
| 2113 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2114 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2115 | } |
| 2116 | if (address != "") { |
| 2117 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2118 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2119 | address, "remote-submix"); |
| 2120 | } |
| 2121 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2122 | resetInputDevice(input); |
| 2123 | |
| 2124 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2125 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2126 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2127 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2128 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2129 | SoundTrigger::setCaptureState(false); |
| 2130 | } |
| 2131 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2132 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2133 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2136 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2138 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2139 | |
| 2140 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2141 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2142 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2143 | return; |
| 2144 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2145 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2146 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2147 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2148 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2149 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2150 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2151 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2152 | if (inputDesc->getClientCount() > 0) { |
| 2153 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2154 | return; |
| 2155 | } |
| 2156 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2157 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2158 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2159 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2160 | } |
| 2161 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2162 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2163 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2164 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2165 | |
| 2166 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2167 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2168 | } |
| 2169 | } |
| 2170 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2171 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2172 | { |
| 2173 | stopInput(portId); |
| 2174 | releaseInput(portId); |
| 2175 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2176 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2177 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2178 | bool patchRemoved = false; |
| 2179 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2180 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2181 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2182 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2183 | if (patch_index >= 0) { |
| 2184 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2185 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2186 | mAudioPatches.removeItemsAt(patch_index); |
| 2187 | patchRemoved = true; |
| 2188 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2189 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2190 | } |
| 2191 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2192 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2193 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2194 | |
| 2195 | if (patchRemoved) { |
| 2196 | mpClientInterface->onAudioPatchListUpdate(); |
| 2197 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2198 | } |
| 2199 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2200 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2201 | int indexMin, |
| 2202 | int indexMax) |
| 2203 | { |
| 2204 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2205 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2206 | |
| 2207 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2208 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2209 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2210 | continue; |
| 2211 | } |
| 2212 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2213 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2214 | } |
| 2215 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2216 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2217 | int index, |
| 2218 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2219 | { |
| 2220 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2221 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2222 | // app that has MODIFY_PHONE_STATE permission. |
| 2223 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2224 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2225 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2226 | return BAD_VALUE; |
| 2227 | } |
| 2228 | if (!audio_is_output_device(device)) { |
| 2229 | return BAD_VALUE; |
| 2230 | } |
| 2231 | |
| 2232 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2233 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2234 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2235 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2236 | stream, device, index); |
| 2237 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2238 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2239 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2240 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2241 | continue; |
| 2242 | } |
| 2243 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2244 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2245 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2246 | // update volume on all outputs and streams matching the following: |
| 2247 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2248 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2249 | // the requested device |
| 2250 | // - For non default requested device, currently selected device on the output is either the |
| 2251 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2252 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2253 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2254 | status_t status = NO_ERROR; |
| 2255 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2256 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2257 | audio_devices_t curDevice = desc->devices().types(); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2258 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2259 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2260 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2261 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2262 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2263 | continue; |
| 2264 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2265 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2266 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2267 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2268 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2269 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2270 | continue; |
| 2271 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2272 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2273 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2274 | curStreamDevice |= device; |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2275 | applyVolume = (Volume::getDeviceForVolume(curDevice) & curStreamDevice) != 0; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2276 | } else { |
| 2277 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2278 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2279 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2280 | // rescale index before applying to curStream as ranges may be different for |
| 2281 | // stream and curStream |
| 2282 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2283 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2284 | //FIXME: workaround for truncated touch sounds |
| 2285 | // delayed volume change for system stream to be removed when the problem is |
| 2286 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2287 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2288 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2289 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2290 | if (volStatus != NO_ERROR) { |
| 2291 | status = volStatus; |
| 2292 | } |
| 2293 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2294 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2295 | } |
| 2296 | return status; |
| 2297 | } |
| 2298 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2299 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2300 | int *index, |
| 2301 | audio_devices_t device) |
| 2302 | { |
| 2303 | if (index == NULL) { |
| 2304 | return BAD_VALUE; |
| 2305 | } |
| 2306 | if (!audio_is_output_device(device)) { |
| 2307 | return BAD_VALUE; |
| 2308 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2309 | // 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] | 2310 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2311 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2312 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2313 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2314 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2315 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2316 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2317 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2318 | return NO_ERROR; |
| 2319 | } |
| 2320 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2321 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2322 | { |
| 2323 | // select one output among several suitable for global effects. |
| 2324 | // The priority is as follows: |
| 2325 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2326 | // AudioFlinger will invalidate the track and the offloaded output |
| 2327 | // will be closed causing the effect to be moved to a PCM output. |
| 2328 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2329 | // 3: The primary output |
| 2330 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2331 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2332 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2333 | DeviceVector devices = getDevicesForStrategy(strategy, false /*fromCache*/); |
| 2334 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2335 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2336 | if (outputs.size() == 0) { |
| 2337 | return AUDIO_IO_HANDLE_NONE; |
| 2338 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2339 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2340 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2341 | bool activeOnly = true; |
| 2342 | |
| 2343 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2344 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2345 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2346 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2347 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2348 | for (audio_io_handle_t output : outputs) { |
| 2349 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2350 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2351 | continue; |
| 2352 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2353 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2354 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2355 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2356 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2357 | } |
| 2358 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2359 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2360 | } |
| 2361 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2362 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2363 | } |
| 2364 | } |
| 2365 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2366 | output = outputOffloaded; |
| 2367 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2368 | output = outputDeepBuffer; |
| 2369 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2370 | output = outputPrimary; |
| 2371 | } else { |
| 2372 | output = outputs[0]; |
| 2373 | } |
| 2374 | activeOnly = false; |
| 2375 | } |
| 2376 | |
| 2377 | if (output != mMusicEffectOutput) { |
| 2378 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2379 | mMusicEffectOutput = output; |
| 2380 | } |
| 2381 | |
| 2382 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2383 | return output; |
| 2384 | } |
| 2385 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2386 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2387 | { |
| 2388 | return selectOutputForMusicEffects(); |
| 2389 | } |
| 2390 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2391 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2392 | audio_io_handle_t io, |
| 2393 | uint32_t strategy, |
| 2394 | int session, |
| 2395 | int id) |
| 2396 | { |
| 2397 | ssize_t index = mOutputs.indexOfKey(io); |
| 2398 | if (index < 0) { |
| 2399 | index = mInputs.indexOfKey(io); |
| 2400 | if (index < 0) { |
| 2401 | ALOGW("registerEffect() unknown io %d", io); |
| 2402 | return INVALID_OPERATION; |
| 2403 | } |
| 2404 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2405 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2406 | } |
| 2407 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2408 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 2409 | { |
| 2410 | if (mEffects.getEffect(id) == nullptr) { |
| 2411 | return INVALID_OPERATION; |
| 2412 | } |
| 2413 | |
| 2414 | if (mEffects.isEffectEnabled(id)) { |
| 2415 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 2416 | setEffectEnabled(id, false); |
| 2417 | } |
| 2418 | return mEffects.unregisterEffect(id); |
| 2419 | } |
| 2420 | |
| 2421 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 2422 | { |
| 2423 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 2424 | if (effect == nullptr) { |
| 2425 | return INVALID_OPERATION; |
| 2426 | } |
| 2427 | |
| 2428 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 2429 | if (status == NO_ERROR) { |
| 2430 | mInputs.trackEffectEnabled(effect, enabled); |
| 2431 | } |
| 2432 | return status; |
| 2433 | } |
| 2434 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2435 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2436 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2437 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2438 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2439 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2440 | continue; |
| 2441 | } |
| 2442 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2443 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2444 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2448 | { |
| 2449 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2450 | } |
| 2451 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2452 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2453 | { |
| 2454 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2455 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2456 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2457 | return true; |
| 2458 | } |
| 2459 | } |
| 2460 | return false; |
| 2461 | } |
| 2462 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2463 | // Register a list of custom mixes with their attributes and format. |
| 2464 | // When a mix is registered, corresponding input and output profiles are |
| 2465 | // added to the remote submix hw module. The profile contains only the |
| 2466 | // parameters (sampling rate, format...) specified by the mix. |
| 2467 | // The corresponding input remote submix device is also connected. |
| 2468 | // |
| 2469 | // When a remote submix device is connected, the address is checked to select the |
| 2470 | // appropriate profile and the corresponding input or output stream is opened. |
| 2471 | // |
| 2472 | // When capture starts, getInputForAttr() will: |
| 2473 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2474 | // - 2 if none found, getDeviceForInputSource() will: |
| 2475 | // - 2.1 look for a mix matching the attributes source |
| 2476 | // - 2.2 if none found, default to device selection by policy rules |
| 2477 | // At this time, the corresponding output remote submix device is also connected |
| 2478 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2479 | // after AudioTracks are invalidated |
| 2480 | // |
| 2481 | // When playback starts, getOutputForAttr() will: |
| 2482 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2483 | // - 2 if none found, look for a mix matching the attributes usage |
| 2484 | // - 3 if none found, default to device and output selection by policy rules. |
| 2485 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2486 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2487 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2488 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2489 | status_t res = NO_ERROR; |
| 2490 | |
| 2491 | sp<HwModule> rSubmixModule; |
| 2492 | // examine each mix's route type |
| 2493 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2494 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2495 | // 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] | 2496 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2497 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2498 | break; |
| 2499 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2500 | 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] | 2501 | 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] | 2502 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2503 | rSubmixModule = mHwModules.getModuleFromName( |
| 2504 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2505 | if (rSubmixModule == 0) { |
| 2506 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2507 | i); |
| 2508 | res = INVALID_OPERATION; |
| 2509 | break; |
| 2510 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2511 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2512 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2513 | String8 address = mix.mDeviceAddress; |
| 2514 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2515 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2516 | } else { |
| 2517 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2518 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2519 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2520 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2521 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2522 | res = INVALID_OPERATION; |
| 2523 | break; |
| 2524 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2525 | audio_config_t outputConfig = mix.mFormat; |
| 2526 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2527 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2528 | // stereo and let audio flinger do the channel conversion if needed. |
| 2529 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2530 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2531 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2532 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2533 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2534 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2535 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2536 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2537 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2538 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2539 | address.string(), "remote-submix"); |
| 2540 | } else { |
| 2541 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2542 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2543 | address.string(), "remote-submix"); |
| 2544 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2545 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2546 | String8 address = mix.mDeviceAddress; |
| 2547 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2548 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2549 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2550 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2551 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2552 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2553 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2554 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2555 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2556 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2557 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2558 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2559 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2560 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2561 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2562 | } else { |
| 2563 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2564 | } |
| 2565 | break; |
| 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | if (res != NO_ERROR) { |
| 2570 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2571 | i, device, address.string()); |
| 2572 | res = INVALID_OPERATION; |
| 2573 | break; |
| 2574 | } else if (!foundOutput) { |
| 2575 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2576 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2577 | res = INVALID_OPERATION; |
| 2578 | break; |
| 2579 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2580 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2581 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2582 | if (res != NO_ERROR) { |
| 2583 | unregisterPolicyMixes(mixes); |
| 2584 | } |
| 2585 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2589 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2590 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2591 | status_t res = NO_ERROR; |
| 2592 | sp<HwModule> rSubmixModule; |
| 2593 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2594 | for (const auto& mix : mixes) { |
| 2595 | 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] | 2596 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2597 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2598 | rSubmixModule = mHwModules.getModuleFromName( |
| 2599 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2600 | if (rSubmixModule == 0) { |
| 2601 | res = INVALID_OPERATION; |
| 2602 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2603 | } |
| 2604 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2605 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2606 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2607 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2608 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2609 | res = INVALID_OPERATION; |
| 2610 | continue; |
| 2611 | } |
| 2612 | |
| 2613 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2614 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2615 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2616 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2617 | address.string(), "remote-submix"); |
| 2618 | } |
| 2619 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2620 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2621 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2622 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2623 | address.string(), "remote-submix"); |
| 2624 | } |
| 2625 | rSubmixModule->removeOutputProfile(address); |
| 2626 | rSubmixModule->removeInputProfile(address); |
| 2627 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2628 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2629 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2630 | res = INVALID_OPERATION; |
| 2631 | continue; |
| 2632 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2633 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2634 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2635 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2636 | } |
| 2637 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 2638 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 2639 | { |
| 2640 | size_t i = 0; |
| 2641 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 2642 | for (const auto& fmt : mManualSurroundFormats) { |
| 2643 | if (i++ != 0) dst->append(", "); |
| 2644 | std::string sfmt; |
| 2645 | FormatConverter::toString(fmt, sfmt); |
| 2646 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 2647 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 2648 | } |
| 2649 | } |
| 2650 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2651 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
| 2652 | const Vector<AudioDeviceTypeAddr>& devices) { |
| 2653 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
| 2654 | // uid/device affinity is only for output devices |
| 2655 | for (size_t i = 0; i < devices.size(); i++) { |
| 2656 | if (!audio_is_output_device(devices[i].mType)) { |
| 2657 | ALOGE("setUidDeviceAffinities() device=%08x is NOT an output device", |
| 2658 | devices[i].mType); |
| 2659 | return BAD_VALUE; |
| 2660 | } |
| 2661 | } |
| 2662 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
| 2663 | if (res == NO_ERROR) { |
| 2664 | // reevaluate outputs for all given devices |
| 2665 | for (size_t i = 0; i < devices.size(); i++) { |
| 2666 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
| 2667 | devices[i].mType, devices[i].mAddress, String8()); |
| 2668 | SortedVector<audio_io_handle_t> outputs; |
| 2669 | if (checkOutputsForDevice(devDesc, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2670 | outputs) != NO_ERROR) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2671 | ALOGE("setUidDeviceAffinities() error in checkOutputsForDevice for device=%08x" |
| 2672 | " addr=%s", devices[i].mType, devices[i].mAddress.string()); |
| 2673 | return INVALID_OPERATION; |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | return res; |
| 2678 | } |
| 2679 | |
| 2680 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 2681 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
| 2682 | Vector<AudioDeviceTypeAddr> devices; |
| 2683 | status_t res = mPolicyMixes.getDevicesForUid(uid, devices); |
| 2684 | if (res == NO_ERROR) { |
| 2685 | // reevaluate outputs for all found devices |
| 2686 | for (size_t i = 0; i < devices.size(); i++) { |
| 2687 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
| 2688 | devices[i].mType, devices[i].mAddress, String8()); |
| 2689 | SortedVector<audio_io_handle_t> outputs; |
| 2690 | if (checkOutputsForDevice(devDesc, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2691 | outputs) != NO_ERROR) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2692 | ALOGE("%s() error in checkOutputsForDevice for device=%08x addr=%s", |
| 2693 | __FUNCTION__, devices[i].mType, devices[i].mAddress.string()); |
| 2694 | return INVALID_OPERATION; |
| 2695 | } |
| 2696 | } |
| 2697 | } |
| 2698 | |
| 2699 | return res; |
| 2700 | } |
| 2701 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2702 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2703 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2704 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2705 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2706 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2707 | std::string stateLiteral; |
| 2708 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2709 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2710 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2711 | "communications", "media", "record", "dock", "system", |
| 2712 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2713 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2714 | i < AUDIO_POLICY_FORCE_USE_CNT; i = (audio_policy_force_use_t)((int)i + 1)) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 2715 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 2716 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 2717 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 2718 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 2719 | dst->append(" (MANUAL: "); |
| 2720 | dumpManualSurroundFormats(dst); |
| 2721 | dst->append(")"); |
| 2722 | } |
| 2723 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2724 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2725 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2726 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2727 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2728 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2729 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2730 | mHwModulesAll.dump(dst); |
| 2731 | mOutputs.dump(dst); |
| 2732 | mInputs.dump(dst); |
| 2733 | mVolumeCurves->dump(dst); |
| 2734 | mEffects.dump(dst); |
| 2735 | mAudioPatches.dump(dst); |
| 2736 | mPolicyMixes.dump(dst); |
| 2737 | mAudioSources.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | status_t AudioPolicyManager::dump(int fd) |
| 2741 | { |
| 2742 | String8 result; |
| 2743 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2744 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2745 | return NO_ERROR; |
| 2746 | } |
| 2747 | |
| 2748 | // This function checks for the parameters which can be offloaded. |
| 2749 | // This can be enhanced depending on the capability of the DSP and policy |
| 2750 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2751 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2752 | { |
| 2753 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2754 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2755 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2756 | offloadInfo.format, |
| 2757 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2758 | offloadInfo.has_video); |
| 2759 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2760 | if (mMasterMono) { |
| 2761 | return false; // no offloading if mono is set. |
| 2762 | } |
| 2763 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2764 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2765 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2766 | ALOGV("offload disabled by audio.offload.disable"); |
| 2767 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2768 | } |
| 2769 | |
| 2770 | // Check if stream type is music, then only allow offload as of now. |
| 2771 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2772 | { |
| 2773 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2774 | return false; |
| 2775 | } |
| 2776 | |
| 2777 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2778 | const bool allowOffloadWithVideo = |
| 2779 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2780 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2781 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2782 | return false; |
| 2783 | } |
| 2784 | |
| 2785 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2786 | const int min_duration_secs = property_get_int32( |
| 2787 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2788 | if (min_duration_secs >= 0) { |
| 2789 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2790 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2791 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2792 | return false; |
| 2793 | } |
| 2794 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2795 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2796 | return false; |
| 2797 | } |
| 2798 | |
| 2799 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2800 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2801 | // detects there is an active non offloadable effect. |
| 2802 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2803 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2804 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2805 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2806 | return false; |
| 2807 | } |
| 2808 | |
| 2809 | // See if there is a profile to support this. |
| 2810 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2811 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2812 | offloadInfo.sample_rate, |
| 2813 | offloadInfo.format, |
| 2814 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2815 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 2816 | true /* directOnly */); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2817 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2818 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2821 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 2822 | const audio_attributes_t& attributes) { |
| 2823 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
| 2824 | audio_attributes_flags_to_audio_output_flags(attributes.flags, output_flags); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2825 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2826 | config.sample_rate, |
| 2827 | config.format, |
| 2828 | config.channel_mask, |
| 2829 | output_flags, |
| 2830 | true /* directOnly */); |
| 2831 | ALOGV("%s() profile %sfound with name: %s, " |
| 2832 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 2833 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 2834 | (profile != 0 ? profile->getTagName().string() : "null"), |
| 2835 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 2836 | return (profile != 0); |
| 2837 | } |
| 2838 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2839 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2840 | audio_port_type_t type, |
| 2841 | unsigned int *num_ports, |
| 2842 | struct audio_port *ports, |
| 2843 | unsigned int *generation) |
| 2844 | { |
| 2845 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2846 | generation == NULL) { |
| 2847 | return BAD_VALUE; |
| 2848 | } |
| 2849 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2850 | if (ports == NULL) { |
| 2851 | *num_ports = 0; |
| 2852 | } |
| 2853 | |
| 2854 | size_t portsWritten = 0; |
| 2855 | size_t portsMax = *num_ports; |
| 2856 | *num_ports = 0; |
| 2857 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2858 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2859 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2860 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2861 | for (const auto& dev : mAvailableOutputDevices) { |
| 2862 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2863 | continue; |
| 2864 | } |
| 2865 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2866 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2867 | } |
| 2868 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2869 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2870 | } |
| 2871 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2872 | for (const auto& dev : mAvailableInputDevices) { |
| 2873 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2874 | continue; |
| 2875 | } |
| 2876 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2877 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2878 | } |
| 2879 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2880 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2881 | } |
| 2882 | } |
| 2883 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2884 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2885 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2886 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2887 | } |
| 2888 | *num_ports += mInputs.size(); |
| 2889 | } |
| 2890 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2891 | size_t numOutputs = 0; |
| 2892 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2893 | if (!mOutputs[i]->isDuplicated()) { |
| 2894 | numOutputs++; |
| 2895 | if (portsWritten < portsMax) { |
| 2896 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2897 | } |
| 2898 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2899 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2900 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2901 | } |
| 2902 | } |
| 2903 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2904 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2905 | return NO_ERROR; |
| 2906 | } |
| 2907 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2908 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2909 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2910 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 2911 | return BAD_VALUE; |
| 2912 | } |
| 2913 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 2914 | if (dev != 0) { |
| 2915 | dev->toAudioPort(port); |
| 2916 | return NO_ERROR; |
| 2917 | } |
| 2918 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 2919 | if (dev != 0) { |
| 2920 | dev->toAudioPort(port); |
| 2921 | return NO_ERROR; |
| 2922 | } |
| 2923 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 2924 | if (out != 0) { |
| 2925 | out->toAudioPort(port); |
| 2926 | return NO_ERROR; |
| 2927 | } |
| 2928 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 2929 | if (in != 0) { |
| 2930 | in->toAudioPort(port); |
| 2931 | return NO_ERROR; |
| 2932 | } |
| 2933 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2934 | } |
| 2935 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2936 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2937 | audio_patch_handle_t *handle, |
| 2938 | uid_t uid) |
| 2939 | { |
| 2940 | ALOGV("createAudioPatch()"); |
| 2941 | |
| 2942 | if (handle == NULL || patch == NULL) { |
| 2943 | return BAD_VALUE; |
| 2944 | } |
| 2945 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2946 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 2947 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2948 | return BAD_VALUE; |
| 2949 | } |
| 2950 | // only one source per audio patch supported for now |
| 2951 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2952 | return INVALID_OPERATION; |
| 2953 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2954 | |
| 2955 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2956 | return INVALID_OPERATION; |
| 2957 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2958 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2959 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2960 | return INVALID_OPERATION; |
| 2961 | } |
| 2962 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2963 | |
| 2964 | sp<AudioPatch> patchDesc; |
| 2965 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2966 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2967 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2968 | patch->sources[0].role, |
| 2969 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2970 | #if LOG_NDEBUG == 0 |
| 2971 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2972 | 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] | 2973 | patch->sinks[i].role, |
| 2974 | patch->sinks[i].type); |
| 2975 | } |
| 2976 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2977 | |
| 2978 | if (index >= 0) { |
| 2979 | patchDesc = mAudioPatches.valueAt(index); |
| 2980 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2981 | mUidCached, patchDesc->mUid, uid); |
| 2982 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2983 | return INVALID_OPERATION; |
| 2984 | } |
| 2985 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2986 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2987 | } |
| 2988 | |
| 2989 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2990 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2991 | if (outputDesc == NULL) { |
| 2992 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2993 | return BAD_VALUE; |
| 2994 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2995 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2996 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2997 | if (patchDesc != 0) { |
| 2998 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2999 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 3000 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 3001 | return BAD_VALUE; |
| 3002 | } |
| 3003 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3004 | DeviceVector devices; |
| 3005 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3006 | // Only support mix to devices connection |
| 3007 | // TODO add support for mix to mix connection |
| 3008 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3009 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 3010 | return INVALID_OPERATION; |
| 3011 | } |
| 3012 | sp<DeviceDescriptor> devDesc = |
| 3013 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3014 | if (devDesc == 0) { |
| 3015 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 3016 | return BAD_VALUE; |
| 3017 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3018 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3019 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3020 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3021 | NULL, // updatedSamplingRate |
| 3022 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3023 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3024 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3025 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3026 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3027 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 3028 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3029 | return INVALID_OPERATION; |
| 3030 | } |
| 3031 | devices.add(devDesc); |
| 3032 | } |
| 3033 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3034 | return INVALID_OPERATION; |
| 3035 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3036 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3037 | // TODO: reconfigure output format and channels here |
| 3038 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3039 | devices.types(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3040 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3041 | index = mAudioPatches.indexOfKey(*handle); |
| 3042 | if (index >= 0) { |
| 3043 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3044 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3045 | } |
| 3046 | patchDesc = mAudioPatches.valueAt(index); |
| 3047 | patchDesc->mUid = uid; |
| 3048 | ALOGV("createAudioPatch() success"); |
| 3049 | } else { |
| 3050 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3051 | return INVALID_OPERATION; |
| 3052 | } |
| 3053 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3054 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3055 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3056 | // only one sink supported when connecting an input device to a mix |
| 3057 | if (patch->num_sinks > 1) { |
| 3058 | return INVALID_OPERATION; |
| 3059 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3060 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3061 | if (inputDesc == NULL) { |
| 3062 | return BAD_VALUE; |
| 3063 | } |
| 3064 | if (patchDesc != 0) { |
| 3065 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3066 | return BAD_VALUE; |
| 3067 | } |
| 3068 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3069 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3070 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3071 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3072 | return BAD_VALUE; |
| 3073 | } |
| 3074 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3075 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3076 | patch->sinks[0].sample_rate, |
| 3077 | NULL, /*updatedSampleRate*/ |
| 3078 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3079 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3080 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3081 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3082 | // FIXME for the parameter type, |
| 3083 | // and the NONE |
| 3084 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3085 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3086 | return INVALID_OPERATION; |
| 3087 | } |
| 3088 | // TODO: reconfigure output format and channels here |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3089 | ALOGV("%s() setting device %s on output %d", __func__, |
| 3090 | device->toString().c_str(), inputDesc->mIoHandle); |
| 3091 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3092 | index = mAudioPatches.indexOfKey(*handle); |
| 3093 | if (index >= 0) { |
| 3094 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3095 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3096 | } |
| 3097 | patchDesc = mAudioPatches.valueAt(index); |
| 3098 | patchDesc->mUid = uid; |
| 3099 | ALOGV("createAudioPatch() success"); |
| 3100 | } else { |
| 3101 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3102 | return INVALID_OPERATION; |
| 3103 | } |
| 3104 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3105 | // device to device connection |
| 3106 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3107 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3108 | return BAD_VALUE; |
| 3109 | } |
| 3110 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3111 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3112 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3113 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3114 | return BAD_VALUE; |
| 3115 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3116 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3117 | //update source and sink with our own data as the data passed in the patch may |
| 3118 | // be incomplete. |
| 3119 | struct audio_patch newPatch = *patch; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3120 | srcDevice->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3121 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3122 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3123 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3124 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3125 | return INVALID_OPERATION; |
| 3126 | } |
| 3127 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3128 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3129 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3130 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3131 | return BAD_VALUE; |
| 3132 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3133 | sinkDevice->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3134 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3135 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3136 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3137 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 3138 | // - audio HAL version is >= 3.0 but no route has been declared between devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3139 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 3140 | (srcDevice->getModuleVersionMajor() < 3) || |
| 3141 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3142 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3143 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3144 | return INVALID_OPERATION; |
| 3145 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3146 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3147 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3148 | // if the sink device is reachable via an opened output stream, request to go via |
| 3149 | // this output stream by adding a second source to the patch description |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 3150 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3151 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3152 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3153 | if (outputDesc->isDuplicated()) { |
| 3154 | return INVALID_OPERATION; |
| 3155 | } |
| 3156 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3157 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3158 | newPatch.num_sources = 2; |
| 3159 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3160 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3161 | } |
| 3162 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3163 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3164 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3165 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3166 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3167 | status); |
| 3168 | return INVALID_OPERATION; |
| 3169 | } |
| 3170 | } else { |
| 3171 | return BAD_VALUE; |
| 3172 | } |
| 3173 | } else { |
| 3174 | return BAD_VALUE; |
| 3175 | } |
| 3176 | return NO_ERROR; |
| 3177 | } |
| 3178 | |
| 3179 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3180 | uid_t uid) |
| 3181 | { |
| 3182 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3183 | |
| 3184 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3185 | |
| 3186 | if (index < 0) { |
| 3187 | return BAD_VALUE; |
| 3188 | } |
| 3189 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3190 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3191 | mUidCached, patchDesc->mUid, uid); |
| 3192 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3193 | return INVALID_OPERATION; |
| 3194 | } |
| 3195 | |
| 3196 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3197 | patchDesc->mUid = mUidCached; |
| 3198 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3199 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3200 | if (outputDesc == NULL) { |
| 3201 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3202 | return BAD_VALUE; |
| 3203 | } |
| 3204 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3205 | setOutputDevices(outputDesc, |
| 3206 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 3207 | true, |
| 3208 | 0, |
| 3209 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3210 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3211 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3212 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3213 | if (inputDesc == NULL) { |
| 3214 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3215 | return BAD_VALUE; |
| 3216 | } |
| 3217 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3218 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3219 | true, |
| 3220 | NULL); |
| 3221 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3222 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3223 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3224 | status, patchDesc->mAfPatchHandle); |
| 3225 | removeAudioPatch(patchDesc->mHandle); |
| 3226 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3227 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3228 | } else { |
| 3229 | return BAD_VALUE; |
| 3230 | } |
| 3231 | } else { |
| 3232 | return BAD_VALUE; |
| 3233 | } |
| 3234 | return NO_ERROR; |
| 3235 | } |
| 3236 | |
| 3237 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3238 | struct audio_patch *patches, |
| 3239 | unsigned int *generation) |
| 3240 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3241 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3242 | return BAD_VALUE; |
| 3243 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3244 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3245 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3246 | } |
| 3247 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3248 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3249 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3250 | ALOGV("setAudioPortConfig()"); |
| 3251 | |
| 3252 | if (config == NULL) { |
| 3253 | return BAD_VALUE; |
| 3254 | } |
| 3255 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3256 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3257 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3258 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3259 | } |
| 3260 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3261 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3262 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3263 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3264 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3265 | if (outputDesc == NULL) { |
| 3266 | return BAD_VALUE; |
| 3267 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3268 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3269 | "setAudioPortConfig() called on duplicated output %d", |
| 3270 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3271 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3272 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3273 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3274 | if (inputDesc == NULL) { |
| 3275 | return BAD_VALUE; |
| 3276 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3277 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3278 | } else { |
| 3279 | return BAD_VALUE; |
| 3280 | } |
| 3281 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3282 | sp<DeviceDescriptor> deviceDesc; |
| 3283 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3284 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3285 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3286 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3287 | } else { |
| 3288 | return BAD_VALUE; |
| 3289 | } |
| 3290 | if (deviceDesc == NULL) { |
| 3291 | return BAD_VALUE; |
| 3292 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3293 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3294 | } else { |
| 3295 | return BAD_VALUE; |
| 3296 | } |
| 3297 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3298 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3299 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3300 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3301 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3302 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3303 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3304 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3305 | if (status != NO_ERROR) { |
| 3306 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3307 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3308 | |
| 3309 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3310 | } |
| 3311 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3312 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3313 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3314 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3315 | clearAudioPatches(uid); |
| 3316 | clearSessionRoutes(uid); |
| 3317 | } |
| 3318 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3319 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3320 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3321 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3322 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3323 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3324 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3329 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3330 | audio_io_handle_t ouptutToSkip) |
| 3331 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3332 | DeviceVector devices = getDevicesForStrategy(strategy, false /*fromCache*/); |
| 3333 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3334 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3335 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3336 | continue; |
| 3337 | } |
| 3338 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3339 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3340 | continue; |
| 3341 | } |
| 3342 | // If the default device for this strategy is on another output mix, |
| 3343 | // invalidate all tracks in this strategy to force re connection. |
| 3344 | // Otherwise select new device on the output mix. |
| 3345 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3346 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3347 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3348 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3349 | } |
| 3350 | } |
| 3351 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3352 | setOutputDevices( |
| 3353 | outputDesc, getNewOutputDevices(outputDesc, false /*fromCache*/), false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3354 | } |
| 3355 | } |
| 3356 | } |
| 3357 | |
| 3358 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3359 | { |
| 3360 | // remove output routes associated with this uid |
| 3361 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3362 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3363 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3364 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3365 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3366 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3367 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3368 | } |
| 3369 | } |
| 3370 | } |
| 3371 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3372 | for (const auto& strategy : affectedStrategies) { |
| 3373 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | // remove input routes associated with this uid |
| 3377 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3378 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3379 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3380 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3381 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3382 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3383 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | // reroute inputs if necessary |
| 3388 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3389 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3390 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3391 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3392 | inputsToClose.add(inputDesc->mIoHandle); |
| 3393 | } |
| 3394 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3395 | for (const auto& input : inputsToClose) { |
| 3396 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3397 | } |
| 3398 | } |
| 3399 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3400 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3401 | { |
| 3402 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3403 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3404 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3405 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3406 | } |
| 3407 | } |
| 3408 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3409 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3410 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3411 | audio_io_handle_t *ioHandle, |
| 3412 | audio_devices_t *device) |
| 3413 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3414 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3415 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 3416 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
| 3417 | *device = getDeviceAndMixForAttributes(attr)->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3418 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3419 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3420 | } |
| 3421 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3422 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3423 | const audio_attributes_t *attributes, |
| 3424 | audio_port_handle_t *portId, |
| 3425 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3426 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3427 | ALOGV("%s", __FUNCTION__); |
| 3428 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3429 | |
| 3430 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3431 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3432 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3433 | return BAD_VALUE; |
| 3434 | } |
| 3435 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3436 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3437 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3438 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3439 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3440 | return INVALID_OPERATION; |
| 3441 | } |
| 3442 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3443 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3444 | mAvailableInputDevices.getDevice(source->ext.device.type, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3445 | String8(source->ext.device.address)); |
| 3446 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3447 | 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] | 3448 | return BAD_VALUE; |
| 3449 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3450 | |
| 3451 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3452 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3453 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3454 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3455 | |
| 3456 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3457 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDevice, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3458 | streamTypefromAttributesInt(attributes), |
| 3459 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3460 | |
| 3461 | status_t status = connectAudioSource(sourceDesc); |
| 3462 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3463 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3464 | } |
| 3465 | return status; |
| 3466 | } |
| 3467 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3468 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3469 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3470 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3471 | |
| 3472 | // make sure we only have one patch per source. |
| 3473 | disconnectAudioSource(sourceDesc); |
| 3474 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3475 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3476 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3477 | audio_stream_type_t stream = sourceDesc->stream(); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3478 | sp<DeviceDescriptor> srcDevice = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3479 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3480 | DeviceVector sinkDevices = getDevicesForStrategy(strategy, true); |
| 3481 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for strategy"); |
| 3482 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
| 3483 | ALOG_ASSERT(mAvailableOutputDevices.contains(sinkDevice), "%s: Device %s not available", |
| 3484 | __FUNCTION__, sinkDevice->toString().c_str()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3485 | |
| 3486 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3487 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3488 | if (srcDevice->hasSameHwModuleAs(sinkDevice) && |
| 3489 | srcDevice->getModuleVersionMajor() >= 3 && |
| 3490 | sinkDevice->getModule()->supportsPatch(srcDevice, sinkDevice) && |
| 3491 | srcDevice->getAudioPort()->mGains.size() > 0) { |
François Gaffie | 83d789d | 2018-05-29 13:29:19 +0200 | [diff] [blame] | 3492 | ALOGV("%s Device to Device route supported by >=3.0 HAL", __FUNCTION__); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3493 | // TODO: may explicitly specify whether we should use HW or SW patch |
François Gaffie | 83d789d | 2018-05-29 13:29:19 +0200 | [diff] [blame] | 3494 | // create patch between src device and output device |
| 3495 | // create Hwoutput and add to mHwOutputs |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3496 | } else { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3497 | audio_attributes_t resultAttr; |
| 3498 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3499 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3500 | config.sample_rate = sourceDesc->config().sample_rate; |
| 3501 | config.channel_mask = sourceDesc->config().channel_mask; |
| 3502 | config.format = sourceDesc->config().format; |
| 3503 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 3504 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3505 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, |
| 3506 | &attributes, &stream, sourceDesc->uid(), &config, &flags, &selectedDeviceId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3507 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3508 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevices.types()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3509 | return INVALID_OPERATION; |
| 3510 | } |
| 3511 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3512 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3513 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevices.types()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3514 | return INVALID_OPERATION; |
| 3515 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3516 | status_t status = outputDesc->start(); |
| 3517 | if (status != NO_ERROR) { |
| 3518 | return status; |
| 3519 | } |
| 3520 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3521 | // create a special patch with no sink and two sources: |
| 3522 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3523 | // be connected as well as the stream type for volume control |
| 3524 | // - the sink is defined by whatever output device is currently selected for the output |
| 3525 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3526 | PatchBuilder patchBuilder; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3527 | patchBuilder.addSource(srcDevice).addSource(outputDesc, { .stream = stream }); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3528 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3529 | &afPatchHandle, |
| 3530 | 0); |
| 3531 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3532 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3533 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3534 | if (status != NO_ERROR) { |
| 3535 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3536 | __FUNCTION__, status); |
| 3537 | return INVALID_OPERATION; |
| 3538 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3539 | |
| 3540 | if (outputDesc->getClient(sourceDesc->portId()) != nullptr) { |
| 3541 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 3542 | return INVALID_OPERATION; |
| 3543 | } |
| 3544 | outputDesc->addClient(sourceDesc); |
| 3545 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3546 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3547 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3548 | |
| 3549 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3550 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3551 | return status; |
| 3552 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3553 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3554 | if (delayMs != 0) { |
| 3555 | usleep(delayMs * 1000); |
| 3556 | } |
| 3557 | } |
| 3558 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3559 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3560 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3561 | |
| 3562 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3565 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3566 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3567 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3568 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3569 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3570 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3571 | return BAD_VALUE; |
| 3572 | } |
| 3573 | status_t status = disconnectAudioSource(sourceDesc); |
| 3574 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3575 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3576 | return status; |
| 3577 | } |
| 3578 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3579 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3580 | { |
| 3581 | if (mMasterMono == mono) { |
| 3582 | return NO_ERROR; |
| 3583 | } |
| 3584 | mMasterMono = mono; |
| 3585 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3586 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3587 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3588 | // |
| 3589 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3590 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3591 | // play back offloaded. |
| 3592 | if (mMasterMono) { |
| 3593 | Vector<audio_io_handle_t> offloaded; |
| 3594 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3595 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3596 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3597 | offloaded.push(desc->mIoHandle); |
| 3598 | } |
| 3599 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3600 | for (const auto& handle : offloaded) { |
| 3601 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3602 | } |
| 3603 | } |
| 3604 | // update master mono for all remaining outputs |
| 3605 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3606 | updateMono(mOutputs.keyAt(i)); |
| 3607 | } |
| 3608 | return NO_ERROR; |
| 3609 | } |
| 3610 | |
| 3611 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3612 | { |
| 3613 | *mono = mMasterMono; |
| 3614 | return NO_ERROR; |
| 3615 | } |
| 3616 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3617 | float AudioPolicyManager::getStreamVolumeDB( |
| 3618 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3619 | { |
| 3620 | return computeVolume(stream, index, device); |
| 3621 | } |
| 3622 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3623 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3624 | audio_format_t *surroundFormats, |
| 3625 | bool *surroundFormatsEnabled, |
| 3626 | bool reported) |
| 3627 | { |
| 3628 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3629 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3630 | return BAD_VALUE; |
| 3631 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3632 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p reported %d", |
| 3633 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3634 | |
| 3635 | size_t formatsWritten = 0; |
| 3636 | size_t formatsMax = *numSurroundFormats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3637 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3638 | if (reported) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3639 | // Return formats from all device profiles that have already been resolved by |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3640 | // checkOutputsForDevice(). |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3641 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 3642 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 3643 | FormatVector supportedFormats = |
| 3644 | device->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3645 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
| 3646 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
| 3647 | formats.insert(supportedFormats[j]); |
| 3648 | } else { |
| 3649 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3650 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3651 | formats.insert(pair.first); |
| 3652 | break; |
| 3653 | } |
| 3654 | } |
| 3655 | } |
| 3656 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3657 | } |
| 3658 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3659 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3660 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3661 | } |
| 3662 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3663 | *numSurroundFormats = formats.size(); |
| 3664 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3665 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3666 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3667 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3668 | surroundFormats[formatsWritten] = format; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3669 | bool formatEnabled = true; |
| 3670 | switch (forceUse) { |
| 3671 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
| 3672 | formatEnabled = mManualSurroundFormats.count(format) != 0; |
| 3673 | break; |
| 3674 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 3675 | formatEnabled = false; |
| 3676 | break; |
| 3677 | default: // AUTO or ALWAYS => true |
| 3678 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3679 | } |
| 3680 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3681 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3682 | } |
| 3683 | return NO_ERROR; |
| 3684 | } |
| 3685 | |
| 3686 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3687 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3688 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3689 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3690 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3691 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3692 | return BAD_VALUE; |
| 3693 | } |
| 3694 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3695 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 3696 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3697 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3698 | return INVALID_OPERATION; |
| 3699 | } |
| 3700 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3701 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3702 | return NO_ERROR; |
| 3703 | } |
| 3704 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3705 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3706 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3707 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3708 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3709 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3710 | } |
| 3711 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3712 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3713 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3714 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3719 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3720 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3721 | AUDIO_DEVICE_OUT_HDMI); |
| 3722 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3723 | // Simulate reconnection to update enabled surround sound formats. |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3724 | String8 address = hdmiOutputDevices[i]->address(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3725 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3726 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3727 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3728 | address.c_str(), |
| 3729 | name.c_str()); |
| 3730 | if (status != NO_ERROR) { |
| 3731 | continue; |
| 3732 | } |
| 3733 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3734 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3735 | address.c_str(), |
| 3736 | name.c_str()); |
| 3737 | profileUpdated |= (status == NO_ERROR); |
| 3738 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3739 | // FIXME: Why doing this for input HDMI devices if we don't augment their reported formats? |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3740 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3741 | AUDIO_DEVICE_IN_HDMI); |
| 3742 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3743 | // Simulate reconnection to update enabled surround sound formats. |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3744 | String8 address = hdmiInputDevices[i]->address(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3745 | String8 name = hdmiInputDevices[i]->getName(); |
| 3746 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3747 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3748 | address.c_str(), |
| 3749 | name.c_str()); |
| 3750 | if (status != NO_ERROR) { |
| 3751 | continue; |
| 3752 | } |
| 3753 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3754 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3755 | address.c_str(), |
| 3756 | name.c_str()); |
| 3757 | profileUpdated |= (status == NO_ERROR); |
| 3758 | } |
| 3759 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3760 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3761 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3762 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3766 | } |
| 3767 | |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3768 | void AudioPolicyManager::setAppState(uid_t uid, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3769 | { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3770 | ALOGV("%s(uid:%d, state:%d)", __func__, uid, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3771 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 3772 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3773 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
| 3774 | RecordClientVector clients = inputDesc->clientsList(false /*activeOnly*/); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3775 | for (const auto& client : clients) { |
| 3776 | if (uid == client->uid()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3777 | client->setAppState(state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3778 | } |
| 3779 | } |
| 3780 | } |
| 3781 | } |
| 3782 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 3783 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 3784 | { |
| 3785 | for (const auto& hwModule : mHwModules) { |
| 3786 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 3787 | for (const auto &outProfile : outputProfiles) { |
| 3788 | struct audio_port audioPort; |
| 3789 | outProfile->toAudioPort(&audioPort); |
| 3790 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 3791 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 3792 | return true; |
| 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | } |
| 3797 | return false; |
| 3798 | } |
| 3799 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3800 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3801 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3802 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3803 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3804 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3805 | if (patchDesc == 0) { |
| 3806 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3807 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3808 | return BAD_VALUE; |
| 3809 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3810 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3811 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3812 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3813 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3814 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3815 | if (status == NO_ERROR) { |
| 3816 | swOutputDesc->stop(); |
| 3817 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3818 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3819 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3820 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3821 | if (hwOutputDesc != 0) { |
| 3822 | // release patch between src device and output device |
| 3823 | // close Hwoutput and remove from mHwOutputs |
| 3824 | } else { |
| 3825 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3826 | } |
| 3827 | } |
| 3828 | return NO_ERROR; |
| 3829 | } |
| 3830 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3831 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3832 | audio_io_handle_t output, routing_strategy strategy) |
| 3833 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3834 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3835 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3836 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3837 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3838 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3839 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3840 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3841 | source = sourceDesc; |
| 3842 | break; |
| 3843 | } |
| 3844 | } |
| 3845 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3846 | } |
| 3847 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3848 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3849 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3850 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3851 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3852 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3853 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3854 | } |
| 3855 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3856 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3857 | static const char *kConfigLocationList[] = |
| 3858 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3859 | static const int kConfigLocationListSize = |
| 3860 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3861 | |
| 3862 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3863 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3864 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3865 | status_t ret; |
| 3866 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3867 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3868 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3869 | // A2DP offload supported but disabled: try to use special XML file |
| 3870 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3871 | } |
| 3872 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3873 | |
| 3874 | for (const char* fileName : fileNames) { |
| 3875 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3876 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3877 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3878 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3879 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3880 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3881 | return ret; |
| 3882 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3883 | } |
| 3884 | } |
| 3885 | return ret; |
| 3886 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3887 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3888 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3889 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3890 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3891 | 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] | 3892 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3893 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3894 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3895 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3896 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3897 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3898 | mAudioPortGeneration(1), |
| 3899 | mBeaconMuteRefCount(0), |
| 3900 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3901 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3902 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3903 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3904 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3905 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3906 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3907 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3908 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 3909 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 3910 | { |
| 3911 | loadConfig(); |
| 3912 | initialize(); |
| 3913 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3914 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3915 | // This check is to catch any legacy platform updating to Q without having |
| 3916 | // switched to XML since its deprecation on O. |
| 3917 | // TODO: after Q release, remove this check and flag as XML is now the only |
| 3918 | // option and all legacy platform should have transitioned to XML. |
| 3919 | #ifndef USE_XML_AUDIO_POLICY_CONF |
| 3920 | #error Audio policy no longer supports legacy .conf configuration format |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3921 | #endif |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 3922 | |
| 3923 | void AudioPolicyManager::loadConfig() { |
| 3924 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3925 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3926 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3927 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3928 | } |
| 3929 | |
| 3930 | status_t AudioPolicyManager::initialize() { |
| 3931 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3932 | |
| 3933 | // 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] | 3934 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3935 | if (!engineInstance) { |
| 3936 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3937 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3938 | } |
| 3939 | // Retrieve the Policy Manager Interface |
| 3940 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3941 | if (mEngine == NULL) { |
| 3942 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3943 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3944 | } |
| 3945 | mEngine->setObserver(this); |
| 3946 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3947 | if (status != NO_ERROR) { |
| 3948 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 3949 | return status; |
| 3950 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3951 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3952 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3953 | // open all output streams needed to access attached devices |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3954 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3955 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3956 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 3957 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3958 | continue; |
| 3959 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3960 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3961 | // open all output streams needed to access attached devices |
| 3962 | // except for direct output streams that are only opened when they are actually |
| 3963 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3964 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 3965 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 3966 | if (!outProfile->canOpenNewIo()) { |
| 3967 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 3968 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 3969 | continue; |
| 3970 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3971 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 3972 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3973 | continue; |
| 3974 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3975 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3976 | mTtsOutputAvailable = true; |
| 3977 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3978 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3979 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3980 | continue; |
| 3981 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3982 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3983 | DeviceVector availProfileDevices = supportedDevices.filter(mAvailableOutputDevices); |
| 3984 | sp<DeviceDescriptor> supportedDevice = 0; |
| 3985 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 3986 | supportedDevice = mDefaultOutputDevice; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3987 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3988 | // choose first device present in profile's SupportedDevices also part of |
| 3989 | // mAvailableOutputDevices. |
| 3990 | if (availProfileDevices.isEmpty()) { |
| 3991 | continue; |
| 3992 | } |
| 3993 | supportedDevice = availProfileDevices.itemAt(0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3994 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3995 | if (!mAvailableOutputDevices.contains(supportedDevice)) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3996 | continue; |
| 3997 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3998 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3999 | mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4000 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4001 | status_t status = outputDesc->open(nullptr, DeviceVector(supportedDevice), |
| 4002 | AUDIO_STREAM_DEFAULT, |
| 4003 | AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4004 | if (status != NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4005 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 4006 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 4007 | continue; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4008 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4009 | for (const auto &device : availProfileDevices) { |
| 4010 | // give a valid ID to an attached device once confirmed it is reachable |
| 4011 | if (!device->isAttached()) { |
| 4012 | device->attach(hwModule); |
| 4013 | } |
| 4014 | } |
| 4015 | if (mPrimaryOutput == 0 && |
| 4016 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 4017 | mPrimaryOutput = outputDesc; |
| 4018 | } |
| 4019 | addOutput(output, outputDesc); |
| 4020 | setOutputDevices(outputDesc, |
| 4021 | DeviceVector(supportedDevice), |
| 4022 | true, |
| 4023 | 0, |
| 4024 | NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4025 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4026 | // open input streams needed to access attached devices to validate |
| 4027 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4028 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4029 | if (!inProfile->canOpenNewIo()) { |
| 4030 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4031 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4032 | continue; |
| 4033 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4034 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4035 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4036 | continue; |
| 4037 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4038 | // chose first device present in profile's SupportedDevices also part of |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4039 | // available input devices |
| 4040 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 4041 | DeviceVector availProfileDevices = supportedDevices.filter(mAvailableInputDevices); |
| 4042 | if (availProfileDevices.isEmpty()) { |
| 4043 | ALOGE("%s: Input device list is empty!", __FUNCTION__); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4044 | continue; |
| 4045 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4046 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4047 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4048 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4049 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4050 | status_t status = inputDesc->open(nullptr, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4051 | availProfileDevices.itemAt(0), |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4052 | AUDIO_SOURCE_MIC, |
| 4053 | AUDIO_INPUT_FLAG_NONE, |
| 4054 | &input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4055 | if (status != NO_ERROR) { |
| 4056 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 4057 | availProfileDevices.toString().c_str(), |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4058 | hwModule->getName()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4059 | continue; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4060 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4061 | for (const auto &device : availProfileDevices) { |
| 4062 | // give a valid ID to an attached device once confirmed it is reachable |
| 4063 | if (!device->isAttached()) { |
| 4064 | device->attach(hwModule); |
| 4065 | device->importAudioPort(inProfile, true); |
| 4066 | } |
| 4067 | } |
| 4068 | inputDesc->close(); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4069 | } |
| 4070 | } |
| 4071 | // make sure all attached devices have been allocated a unique ID |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4072 | auto checkAndSetAvailable = [this](auto& devices) { |
| 4073 | for (const auto &device : devices) { |
| 4074 | if (!device->isAttached()) { |
| 4075 | ALOGW("device %s is unreachable", device->toString().c_str()); |
| 4076 | devices.remove(device); |
| 4077 | continue; |
| 4078 | } |
| 4079 | // Device is now validated and can be appended to the available devices of the engine |
| 4080 | mEngine->setDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4081 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4082 | }; |
| 4083 | checkAndSetAvailable(mAvailableOutputDevices); |
| 4084 | checkAndSetAvailable(mAvailableInputDevices); |
| 4085 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4086 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4087 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 4088 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 4089 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4090 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4091 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4092 | // If microphones address is empty, set it according to device type |
| 4093 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 4094 | if (mAvailableInputDevices[i]->address().isEmpty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4095 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
jiabin | 20729a7 | 2019-01-04 16:01:55 -0800 | [diff] [blame] | 4096 | mAvailableInputDevices[i]->setAddress(String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS)); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4097 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
jiabin | 20729a7 | 2019-01-04 16:01:55 -0800 | [diff] [blame] | 4098 | mAvailableInputDevices[i]->setAddress(String8(AUDIO_BACK_MICROPHONE_ADDRESS)); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4099 | } |
| 4100 | } |
| 4101 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4102 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4103 | if (mPrimaryOutput == 0) { |
| 4104 | ALOGE("Failed to open primary output"); |
| 4105 | status = NO_INIT; |
| 4106 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4108 | // Silence ALOGV statements |
| 4109 | property_set("log.tag." LOG_TAG, "D"); |
| 4110 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4111 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4112 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4115 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4116 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4117 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4118 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4119 | } |
| 4120 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4121 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4122 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4123 | mAvailableOutputDevices.clear(); |
| 4124 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4125 | mOutputs.clear(); |
| 4126 | mInputs.clear(); |
| 4127 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4128 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4129 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4132 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4133 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4134 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4135 | } |
| 4136 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4137 | // --- |
| 4138 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4139 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4140 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4141 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4142 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4143 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4144 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4145 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4146 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4147 | } |
| 4148 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4149 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4150 | { |
| 4151 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4152 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4153 | } |
| 4154 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4155 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4156 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4157 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4158 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4159 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4160 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4161 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4162 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4163 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4164 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4165 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4166 | audio_devices_t deviceType = device->type(); |
| 4167 | const String8 &address = device->address(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4168 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4169 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4170 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4171 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4172 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4173 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4174 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4175 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | // first list already open outputs that can be routed to this device |
| 4177 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4178 | desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4179 | if (!desc->isDuplicated() && desc->supportsDevice(device)) { |
| 4180 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 4181 | mOutputs.keyAt(i), device->toString().c_str()); |
| 4182 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4183 | } |
| 4184 | } |
| 4185 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4186 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4187 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4188 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4189 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4190 | if (profile->supportsDevice(device)) { |
| 4191 | profiles.add(profile); |
| 4192 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4193 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4194 | } |
| 4195 | } |
| 4196 | } |
| 4197 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4198 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4199 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4200 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4201 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4202 | return BAD_VALUE; |
| 4203 | } |
| 4204 | |
| 4205 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4206 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4207 | 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] | 4208 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4209 | |
| 4210 | // nothing to do if one output is already opened for this profile |
| 4211 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4212 | for (j = 0; j < outputs.size(); j++) { |
| 4213 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4214 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4215 | // matching profile: save the sample rates, format and channel masks supported |
| 4216 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4217 | if (audio_device_is_digital(deviceType)) { |
| 4218 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4219 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4220 | break; |
| 4221 | } |
| 4222 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4223 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 | continue; |
| 4225 | } |
| 4226 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4227 | if (!profile->canOpenNewIo()) { |
| 4228 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4229 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4230 | continue; |
| 4231 | } |
| 4232 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4233 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4234 | deviceType, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4235 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4236 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4237 | status_t status = desc->open(nullptr, DeviceVector(device), |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4238 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4239 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4240 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4241 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4242 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4243 | char *param = audio_device_address_to_parameter(deviceType, address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4244 | mpClientInterface->setParameters(output, String8(param)); |
| 4245 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4246 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4247 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4248 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4249 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4250 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4251 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4252 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4253 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4254 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4255 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4256 | profile->pickAudioProfile( |
| 4257 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4258 | config.offload_info.sample_rate = config.sample_rate; |
| 4259 | config.offload_info.channel_mask = config.channel_mask; |
| 4260 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4261 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4262 | status_t status = desc->open(&config, DeviceVector(device), |
| 4263 | AUDIO_STREAM_DEFAULT, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4264 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4265 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4266 | output = AUDIO_IO_HANDLE_NONE; |
| 4267 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4268 | } |
| 4269 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4270 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4271 | addOutput(output, desc); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4272 | if (device_distinguishes_on_address(deviceType) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4273 | sp<AudioPolicyMix> policyMix; |
| 4274 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4275 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4276 | address.string()); |
| 4277 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4278 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4279 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4280 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4281 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4282 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4283 | // no duplicated output for direct outputs and |
| 4284 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4285 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4287 | //TODO: configure audio effect output stage here |
| 4288 | |
| 4289 | // open a duplicating output thread for the new output and the primary output |
Eric Laurent | 5babc4f | 2018-02-15 12:33:44 -0800 | [diff] [blame] | 4290 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4291 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4292 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4293 | &duplicatedOutput); |
| 4294 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4295 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4296 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4297 | } else { |
| 4298 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4299 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4300 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4301 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4302 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4303 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4304 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4305 | } |
| 4306 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4307 | } else { |
| 4308 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4309 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4310 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4311 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | profiles.removeAt(profile_index); |
| 4313 | profile_index--; |
| 4314 | } else { |
| 4315 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4316 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4317 | if (audio_device_is_digital(deviceType)) { |
| 4318 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4319 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4320 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4321 | if (device_distinguishes_on_address(deviceType)) { |
| 4322 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 4323 | device->toString().c_str()); |
| 4324 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 4325 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4326 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4327 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4328 | } |
| 4329 | } |
| 4330 | |
| 4331 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4332 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4333 | return BAD_VALUE; |
| 4334 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4335 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | // check if one opened output is not needed any more after disconnecting one device |
| 4337 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4338 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4339 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4340 | // exact match on device |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4341 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4342 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4343 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4344 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4345 | mOutputs.keyAt(i)); |
| 4346 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4347 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4348 | } |
| 4349 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4350 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4351 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4352 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4353 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4354 | if (profile->supportsDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4355 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4356 | "clearing direct output profile %zu on module %s", |
| 4357 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4358 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | } |
| 4362 | } |
| 4363 | return NO_ERROR; |
| 4364 | } |
| 4365 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4366 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4367 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4368 | SortedVector<audio_io_handle_t>& inputs) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4369 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4370 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4371 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4372 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4373 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4374 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4375 | } |
| 4376 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4377 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4378 | // first list already open inputs that can be routed to this device |
| 4379 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4380 | desc = mInputs.valueAt(input_index); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4381 | if (desc->mProfile->supportsDeviceTypes(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4382 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4383 | inputs.add(mInputs.keyAt(input_index)); |
| 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4388 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4389 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4390 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4391 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4392 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4393 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4394 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4395 | if (profile->supportsDevice(device)) { |
| 4396 | profiles.add(profile); |
| 4397 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4398 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4399 | } |
| 4400 | } |
| 4401 | } |
| 4402 | |
| 4403 | if (profiles.isEmpty() && inputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4404 | ALOGW("%s: No input available for device %s", __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4405 | return BAD_VALUE; |
| 4406 | } |
| 4407 | |
| 4408 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4409 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4410 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4411 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4412 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4413 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4414 | // nothing to do if one input is already opened for this profile |
| 4415 | size_t input_index; |
| 4416 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4417 | desc = mInputs.valueAt(input_index); |
| 4418 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4419 | if (audio_device_is_digital(device->type())) { |
| 4420 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4421 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4422 | break; |
| 4423 | } |
| 4424 | } |
| 4425 | if (input_index != mInputs.size()) { |
| 4426 | continue; |
| 4427 | } |
| 4428 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4429 | if (!profile->canOpenNewIo()) { |
| 4430 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4431 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4432 | continue; |
| 4433 | } |
| 4434 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4435 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4436 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4437 | status_t status = desc->open(nullptr, |
| 4438 | device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4439 | AUDIO_SOURCE_MIC, |
| 4440 | AUDIO_INPUT_FLAG_NONE, |
| 4441 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4442 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4443 | if (status == NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4444 | const String8& address = device->address(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4445 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4446 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4447 | mpClientInterface->setParameters(input, String8(param)); |
| 4448 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4449 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4450 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4451 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4452 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4453 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4454 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | if (input != 0) { |
| 4458 | addInput(input, desc); |
| 4459 | } |
| 4460 | } // endif input != 0 |
| 4461 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4462 | if (input == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4463 | ALOGW("%s could not open input for device %s", __func__, |
| 4464 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4465 | profiles.removeAt(profile_index); |
| 4466 | profile_index--; |
| 4467 | } else { |
| 4468 | inputs.add(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4469 | if (audio_device_is_digital(device->type())) { |
| 4470 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4471 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4472 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4473 | } |
| 4474 | } // end scan profiles |
| 4475 | |
| 4476 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4477 | ALOGW("%s: No input available for device %s", __func__, device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4478 | return BAD_VALUE; |
| 4479 | } |
| 4480 | } else { |
| 4481 | // Disconnect |
| 4482 | // check if one opened input is not needed any more after disconnecting one device |
| 4483 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4484 | desc = mInputs.valueAt(input_index); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4485 | if (!mAvailableInputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4486 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4487 | mInputs.keyAt(input_index)); |
| 4488 | inputs.add(mInputs.keyAt(input_index)); |
| 4489 | } |
| 4490 | } |
| 4491 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4492 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4493 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4494 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4495 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4496 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4497 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4498 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4499 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4500 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4501 | } |
| 4502 | } |
| 4503 | } |
| 4504 | } // end disconnect |
| 4505 | |
| 4506 | return NO_ERROR; |
| 4507 | } |
| 4508 | |
| 4509 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4510 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4511 | { |
| 4512 | ALOGV("closeOutput(%d)", output); |
| 4513 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4514 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4515 | if (outputDesc == NULL) { |
| 4516 | ALOGW("closeOutput() unknown output %d", output); |
| 4517 | return; |
| 4518 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4519 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4520 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4521 | // look for duplicated outputs connected to the output being removed. |
| 4522 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4523 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4524 | if (dupOutputDesc->isDuplicated() && |
| 4525 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4526 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4527 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4528 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4529 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4530 | } else { |
| 4531 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4532 | } |
| 4533 | // As all active tracks on duplicated output will be deleted, |
| 4534 | // and as they were also referenced on the other output, the reference |
| 4535 | // count for their stream type must be adjusted accordingly on |
| 4536 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4537 | const bool wasActive = outputDesc2->isActive(); |
| 4538 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4539 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4540 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4541 | // stop() will be a no op if the output is still active but is needed in case all |
| 4542 | // active streams refcounts where cleared above |
| 4543 | if (wasActive) { |
| 4544 | outputDesc2->stop(); |
| 4545 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4546 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4547 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4548 | |
| 4549 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4550 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4551 | } |
| 4552 | } |
| 4553 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4554 | nextAudioPortGeneration(); |
| 4555 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4556 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4557 | if (index >= 0) { |
| 4558 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4559 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4560 | mAudioPatches.removeItemsAt(index); |
| 4561 | mpClientInterface->onAudioPatchListUpdate(); |
| 4562 | } |
| 4563 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4564 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4565 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4566 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4567 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4568 | |
| 4569 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4570 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4571 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4572 | bool directOutputOpen = false; |
| 4573 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4574 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4575 | directOutputOpen = true; |
| 4576 | break; |
| 4577 | } |
| 4578 | } |
| 4579 | if (!directOutputOpen) { |
| 4580 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4581 | setMsdPatch(); |
| 4582 | } |
| 4583 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4584 | } |
| 4585 | |
| 4586 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4587 | { |
| 4588 | ALOGV("closeInput(%d)", input); |
| 4589 | |
| 4590 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4591 | if (inputDesc == NULL) { |
| 4592 | ALOGW("closeInput() unknown input %d", input); |
| 4593 | return; |
| 4594 | } |
| 4595 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4596 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4597 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4598 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4599 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4600 | if (index >= 0) { |
| 4601 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4602 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4603 | mAudioPatches.removeItemsAt(index); |
| 4604 | mpClientInterface->onAudioPatchListUpdate(); |
| 4605 | } |
| 4606 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4607 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4608 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4609 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4610 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 4611 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4612 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4613 | SoundTrigger::setCaptureState(false); |
| 4614 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4615 | } |
| 4616 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4617 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 4618 | const DeviceVector &devices, |
| 4619 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4620 | { |
| 4621 | SortedVector<audio_io_handle_t> outputs; |
| 4622 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4623 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4625 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4626 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4627 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4628 | if (openOutputs.valueAt(i)->supportsAllDevices(devices)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4629 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4630 | outputs.add(openOutputs.keyAt(i)); |
| 4631 | } |
| 4632 | } |
| 4633 | return outputs; |
| 4634 | } |
| 4635 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4636 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4637 | { |
| 4638 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4639 | // output is suspended before any tracks are moved to it |
| 4640 | checkA2dpSuspend(); |
| 4641 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4642 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4643 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 4644 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4645 | setMsdPatch(); |
| 4646 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4649 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4650 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4651 | DeviceVector oldDevices = getDevicesForStrategy(strategy, true /*fromCache*/); |
| 4652 | DeviceVector newDevices = getDevicesForStrategy(strategy, false /*fromCache*/); |
| 4653 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 4654 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4655 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4656 | // also take into account external policy-related changes: add all outputs which are |
| 4657 | // associated with policies in the "before" and "after" output vectors |
| 4658 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4659 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4660 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4661 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4662 | srcOutputs.add(desc->mIoHandle); |
| 4663 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4664 | } |
| 4665 | } |
| 4666 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4667 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4668 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4669 | dstOutputs.add(desc->mIoHandle); |
| 4670 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4671 | } |
| 4672 | } |
| 4673 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4674 | if (!dstOutputs.isEmpty() && srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4675 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4676 | // audio from invalidated tracks will be rendered when unmuting |
| 4677 | uint32_t maxLatency = 0; |
| 4678 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4679 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4680 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4681 | maxLatency = desc->latency(); |
| 4682 | } |
| 4683 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4684 | ALOGV("%s: strategy %d, moving from output %s to output %s", __func__, strategy, |
| 4685 | (srcOutputs.isEmpty()? "none" : std::to_string(srcOutputs[0]).c_str()), |
| 4686 | (dstOutputs.isEmpty()? "none" : std::to_string(dstOutputs[0]).c_str())); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4687 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4688 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4689 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4690 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4691 | setStrategyMute(strategy, true, desc); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4692 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 4693 | newDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4694 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4695 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4696 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4697 | if (source != 0){ |
| 4698 | connectAudioSource(source); |
| 4699 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4700 | } |
| 4701 | |
| 4702 | // Move effects associated to this strategy from previous output to new output |
| 4703 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4704 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4705 | } |
| 4706 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4707 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4708 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4709 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | } |
| 4713 | } |
| 4714 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4715 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4716 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4717 | 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] | 4718 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4719 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4720 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4721 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4722 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4723 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4724 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4725 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4726 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4727 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 | } |
| 4729 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4730 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4731 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4732 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4733 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4734 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4735 | return; |
| 4736 | } |
| 4737 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4738 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4739 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4740 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4741 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4742 | |
| 4743 | // if suspended, restore A2DP output if: |
| 4744 | // ((SCO device is NOT connected) || |
| 4745 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4746 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4747 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4748 | // if not suspended, suspend A2DP output if: |
| 4749 | // (SCO device is connected) && |
| 4750 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4751 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4752 | // |
| 4753 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4754 | if (!isScoConnected || |
| 4755 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4756 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4757 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4758 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4759 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4760 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4761 | |
| 4762 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4763 | mA2dpSuspended = false; |
| 4764 | } |
| 4765 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4766 | if (isScoConnected && |
| 4767 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4768 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4769 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4770 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4771 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4772 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4773 | |
| 4774 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4775 | mA2dpSuspended = true; |
| 4776 | } |
| 4777 | } |
| 4778 | } |
| 4779 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4780 | template <class IoDescriptor, class Filter> |
| 4781 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4782 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4783 | { |
| 4784 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4785 | auto activeClientsWithRoute = |
| 4786 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4787 | active = activeClients.size() > 0; |
| 4788 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4789 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4790 | } |
| 4791 | return nullptr; |
| 4792 | } |
| 4793 | |
| 4794 | template <class IoCollection, class Filter> |
| 4795 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4796 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4797 | { |
| 4798 | sp<DeviceDescriptor> device; |
| 4799 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4800 | auto desc = ioCollection.valueAt(i); |
| 4801 | bool active; |
| 4802 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4803 | if (active && curDevice == nullptr) { |
| 4804 | return nullptr; |
| 4805 | } else if (curDevice != nullptr) { |
| 4806 | device = curDevice; |
| 4807 | } |
| 4808 | } |
| 4809 | return device; |
| 4810 | } |
| 4811 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4812 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 4813 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4814 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4815 | DeviceVector devices; |
| 4816 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4817 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4818 | if (index >= 0) { |
| 4819 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4820 | if (patchDesc->mUid != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4821 | ALOGV("%s device %s forced by patch %d", __func__, |
| 4822 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 4823 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4824 | } |
| 4825 | } |
| 4826 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4827 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4828 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4829 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4830 | sp<DeviceDescriptor> device = |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4831 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4832 | if (device != nullptr) { |
| 4833 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4834 | } |
| 4835 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4836 | // 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] | 4837 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4838 | // use device for strategy enforced audible |
| 4839 | // 2: we are in call or the strategy phone is active on the output: |
| 4840 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4841 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4842 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4843 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4844 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4845 | // 5: the strategy accessibility is active on the output: |
| 4846 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4847 | // 6: the strategy "respectful" sonification is active on the output: |
| 4848 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4849 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4850 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4851 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4852 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4853 | // 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] | 4854 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4855 | |
| 4856 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4857 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4858 | // as opposed to all streams on the same audio HAL module. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4859 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4860 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4861 | devices = getDevicesForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4863 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4864 | devices = getDevicesForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4865 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4866 | devices = getDevicesForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4867 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4868 | devices = getDevicesForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4869 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4870 | devices = getDevicesForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4871 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4872 | devices = getDevicesForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4873 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4874 | devices = getDevicesForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4875 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4876 | devices = getDevicesForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4877 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4878 | devices = getDevicesForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4879 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4880 | devices = getDevicesForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4881 | } |
| 4882 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4883 | ALOGV("getNewOutputDevice() selected devices %s", devices.toString().c_str()); |
| 4884 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4885 | } |
| 4886 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4887 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 4888 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4889 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4890 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4891 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4892 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4893 | if (index >= 0) { |
| 4894 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4895 | if (patchDesc->mUid != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4896 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 4897 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 4898 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4899 | } |
| 4900 | } |
| 4901 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4902 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4903 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4904 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4905 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 4906 | if (device != nullptr) { |
| 4907 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4908 | } |
| 4909 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4910 | // If we are not in call and no client is active on this input, this methods returns |
| 4911 | // AUDIO_DEVICE_NONE, causing the patch on the input stream to be released. |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4912 | audio_attributes_t attributes = inputDesc->getHighestPriorityAttributes(); |
| 4913 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 4914 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 4915 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 4916 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
| 4917 | device = getDeviceAndMixForAttributes(attributes); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4918 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4919 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4920 | return device; |
| 4921 | } |
| 4922 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4923 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4924 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4925 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4926 | } |
| 4927 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4928 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4929 | return (uint32_t)getStrategy(stream); |
| 4930 | } |
| 4931 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4932 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4933 | // By checking the range of stream before calling getStrategy, we avoid |
| 4934 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4935 | // 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] | 4936 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4937 | return AUDIO_DEVICE_NONE; |
| 4938 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4939 | DeviceVector activeDevices; |
| 4940 | DeviceVector devices; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4941 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4942 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4943 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4944 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4945 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4946 | DeviceVector curDevices = |
| 4947 | getDevicesForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
| 4948 | devices.merge(curDevices); |
| 4949 | for (audio_io_handle_t output : getOutputsForDevices(curDevices, mOutputs)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4950 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4951 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4952 | activeDevices.merge(outputDesc->devices()); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4953 | } |
| 4954 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4955 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4956 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4957 | // Favor devices selected on active streams if any to report correct device in case of |
| 4958 | // explicit device selection |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4959 | if (!activeDevices.isEmpty()) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 4960 | devices = activeDevices; |
| 4961 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4962 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4963 | and doesn't really need to.*/ |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4964 | DeviceVector speakerSafeDevices = devices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 4965 | if (!speakerSafeDevices.isEmpty()) { |
| 4966 | devices.merge(mAvailableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER)); |
| 4967 | devices.remove(speakerSafeDevices); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4968 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4969 | return devices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4970 | } |
| 4971 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4972 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4973 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4974 | 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] | 4975 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | } |
| 4977 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4978 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4979 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4980 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4981 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4982 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4983 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4984 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4985 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4986 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4987 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4988 | } |
| 4989 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4990 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4991 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4992 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4993 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4994 | updateDevicesAndOutputs(); |
| 4995 | break; |
| 4996 | default: |
| 4997 | break; |
| 4998 | } |
| 4999 | } |
| 5000 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5001 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5002 | |
| 5003 | // skip beacon mute management if a dedicated TTS output is available |
| 5004 | if (mTtsOutputAvailable) { |
| 5005 | return 0; |
| 5006 | } |
| 5007 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5008 | switch(event) { |
| 5009 | case STARTING_OUTPUT: |
| 5010 | mBeaconMuteRefCount++; |
| 5011 | break; |
| 5012 | case STOPPING_OUTPUT: |
| 5013 | if (mBeaconMuteRefCount > 0) { |
| 5014 | mBeaconMuteRefCount--; |
| 5015 | } |
| 5016 | break; |
| 5017 | case STARTING_BEACON: |
| 5018 | mBeaconPlayingRefCount++; |
| 5019 | break; |
| 5020 | case STOPPING_BEACON: |
| 5021 | if (mBeaconPlayingRefCount > 0) { |
| 5022 | mBeaconPlayingRefCount--; |
| 5023 | } |
| 5024 | break; |
| 5025 | } |
| 5026 | |
| 5027 | if (mBeaconMuteRefCount > 0) { |
| 5028 | // any playback causes beacon to be muted |
| 5029 | return setBeaconMute(true); |
| 5030 | } else { |
| 5031 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5032 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5033 | } |
| 5034 | } |
| 5035 | |
| 5036 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5037 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5038 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5039 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5040 | if (mBeaconMuted != mute) { |
| 5041 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5042 | ALOGV("\t muting %d", mute); |
| 5043 | uint32_t maxLatency = 0; |
| 5044 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5045 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5046 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5047 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5048 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5049 | const uint32_t latency = desc->latency() * 2; |
| 5050 | if (latency > maxLatency) { |
| 5051 | maxLatency = latency; |
| 5052 | } |
| 5053 | } |
| 5054 | mBeaconMuted = mute; |
| 5055 | return maxLatency; |
| 5056 | } |
| 5057 | return 0; |
| 5058 | } |
| 5059 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5060 | DeviceVector AudioPolicyManager::getDevicesForStrategy(routing_strategy strategy, bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5061 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5062 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5063 | // case the last active client route is used |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5064 | sp<DeviceDescriptor> device = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5065 | if (device != nullptr) { |
| 5066 | return DeviceVector(device); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5067 | } |
| 5068 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5069 | if (fromCache) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5070 | ALOGVV("%s from cache strategy %d, device %s", __func__, strategy, |
| 5071 | mDevicesForStrategy[strategy].toString().c_str()); |
| 5072 | return mDevicesForStrategy[strategy]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5073 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5074 | return mAvailableOutputDevices.getDevicesFromTypeMask(mEngine->getDeviceForStrategy(strategy)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5075 | } |
| 5076 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5077 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5078 | { |
| 5079 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5080 | mDevicesForStrategy[i] = getDevicesForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5081 | } |
| 5082 | mPreviousOutputs = mOutputs; |
| 5083 | } |
| 5084 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5085 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5086 | audio_devices_t prevDeviceType, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5087 | uint32_t delayMs) |
| 5088 | { |
| 5089 | // mute/unmute strategies using an incompatible device combination |
| 5090 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5091 | // if unmuting, unmute only after the specified delay |
| 5092 | if (outputDesc->isDuplicated()) { |
| 5093 | return 0; |
| 5094 | } |
| 5095 | |
| 5096 | uint32_t muteWaitMs = 0; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5097 | audio_devices_t deviceType = outputDesc->devices().types(); |
| 5098 | bool shouldMute = outputDesc->isActive() && (popcount(deviceType) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5099 | |
| 5100 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5101 | audio_devices_t curDeviceType = |
| 5102 | getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5103 | curDeviceType = curDeviceType & outputDesc->supportedDevices().types(); |
| 5104 | bool mute = shouldMute && (curDeviceType & deviceType) && (curDeviceType != deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5105 | bool doMute = false; |
| 5106 | |
| 5107 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5108 | doMute = true; |
| 5109 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5110 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5111 | doMute = true; |
| 5112 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5113 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5114 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5115 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5116 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5117 | // skip output if it does not share any device with current output |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5118 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5119 | continue; |
| 5120 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5121 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5122 | mute ? "muting" : "unmuting", i, curDeviceType); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5123 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5124 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5125 | if (mute) { |
| 5126 | // FIXME: should not need to double latency if volume could be applied |
| 5127 | // immediately by the audioflinger mixer. We must account for the delay |
| 5128 | // between now and the next time the audioflinger thread for this output |
| 5129 | // will process a buffer (which corresponds to one buffer size, |
| 5130 | // usually 1/2 or 1/4 of the latency). |
| 5131 | if (muteWaitMs < desc->latency() * 2) { |
| 5132 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5133 | } |
| 5134 | } |
| 5135 | } |
| 5136 | } |
| 5137 | } |
| 5138 | } |
| 5139 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5140 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5141 | // different per device volumes |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5142 | if (outputDesc->isActive() && (deviceType != prevDeviceType)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5143 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5144 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5145 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5146 | if (muteWaitMs < tempMuteWaitMs) { |
| 5147 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5148 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5149 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5150 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5151 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5152 | // make sure that we do not start the temporary mute period too early in case of |
| 5153 | // delayed device change |
| 5154 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5155 | setStrategyMute((routing_strategy)i, false, outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5156 | delayMs + tempMuteDurationMs, deviceType); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5157 | } |
| 5158 | } |
| 5159 | } |
| 5160 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5161 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5162 | if (muteWaitMs > delayMs) { |
| 5163 | muteWaitMs -= delayMs; |
| 5164 | usleep(muteWaitMs * 1000); |
| 5165 | return muteWaitMs; |
| 5166 | } |
| 5167 | return 0; |
| 5168 | } |
| 5169 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5170 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 5171 | const DeviceVector &devices, |
| 5172 | bool force, |
| 5173 | int delayMs, |
| 5174 | audio_patch_handle_t *patchHandle, |
| 5175 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5176 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5177 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5178 | uint32_t muteWaitMs; |
| 5179 | |
| 5180 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5181 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 5182 | nullptr /* patchHandle */, requiresMuteCheck); |
| 5183 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 5184 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5185 | return muteWaitMs; |
| 5186 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5187 | |
| 5188 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5189 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5190 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5191 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5192 | // output profile |
| 5193 | if (!devices.isEmpty() && filteredDevices.isEmpty()) { |
| 5194 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 5195 | return 0; |
| 5196 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5197 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5198 | DeviceVector prevDevices = outputDesc->devices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5199 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5200 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 5201 | |
| 5202 | if (!filteredDevices.isEmpty()) { |
| 5203 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5205 | |
| 5206 | // if the outputs are not materially active, there is no need to mute. |
| 5207 | if (requiresMuteCheck) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5208 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices.types(), delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5209 | } else { |
| 5210 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5211 | muteWaitMs = 0; |
| 5212 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5213 | |
| 5214 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5215 | // the requested device is AUDIO_DEVICE_NONE |
| 5216 | // OR the requested device is the same as current device |
| 5217 | // AND force is not specified |
| 5218 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5219 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
| 5220 | if ((!filteredDevices.isEmpty() || filteredDevices == prevDevices) && |
| 5221 | !force && outputDesc->getPatchHandle() != 0) { |
| 5222 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 5223 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5224 | return muteWaitMs; |
| 5225 | } |
| 5226 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5227 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5228 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5229 | // do the routing |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5230 | if (filteredDevices.isEmpty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5231 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5232 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5233 | PatchBuilder patchBuilder; |
| 5234 | patchBuilder.addSource(outputDesc); |
| 5235 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 5236 | for (const auto &filteredDevice : filteredDevices) { |
| 5237 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5238 | } |
| 5239 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5240 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5241 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5242 | |
| 5243 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5244 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5245 | |
| 5246 | return muteWaitMs; |
| 5247 | } |
| 5248 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5249 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5250 | int delayMs, |
| 5251 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5252 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5253 | ssize_t index; |
| 5254 | if (patchHandle) { |
| 5255 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5256 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5257 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5258 | } |
| 5259 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5260 | return INVALID_OPERATION; |
| 5261 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5262 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5263 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5264 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5265 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5266 | removeAudioPatch(patchDesc->mHandle); |
| 5267 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5268 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5269 | return status; |
| 5270 | } |
| 5271 | |
| 5272 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5273 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5274 | bool force, |
| 5275 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5276 | { |
| 5277 | status_t status = NO_ERROR; |
| 5278 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5279 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5280 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 5281 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5282 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5283 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5284 | PatchBuilder patchBuilder; |
| 5285 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5286 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5287 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5288 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5289 | auto result = usecase; |
| 5290 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5291 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5292 | } |
| 5293 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5294 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5295 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5296 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5297 | } |
| 5298 | } |
| 5299 | return status; |
| 5300 | } |
| 5301 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5302 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5303 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5304 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5305 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5306 | ssize_t index; |
| 5307 | if (patchHandle) { |
| 5308 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5309 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5310 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5311 | } |
| 5312 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5313 | return INVALID_OPERATION; |
| 5314 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5315 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5316 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5317 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5318 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5319 | removeAudioPatch(patchDesc->mHandle); |
| 5320 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5321 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5322 | return status; |
| 5323 | } |
| 5324 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5325 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5326 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5327 | audio_format_t& format, |
| 5328 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5329 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5330 | { |
| 5331 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5332 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5333 | // |
| 5334 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5335 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5336 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5337 | sp<IOProfile> firstInexact; |
| 5338 | uint32_t updatedSamplingRate = 0; |
| 5339 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5340 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5341 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5342 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5343 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5344 | //updatedFormat = format; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5345 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5346 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5347 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5348 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5349 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5350 | &channelMask /*updatedChannelMask*/, |
| 5351 | // FIXME ugly cast |
| 5352 | (audio_output_flags_t) flags, |
| 5353 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5354 | return profile; |
| 5355 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5356 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5357 | samplingRate, |
| 5358 | &updatedSamplingRate, |
| 5359 | format, |
| 5360 | &updatedFormat, |
| 5361 | channelMask, |
| 5362 | &updatedChannelMask, |
| 5363 | // FIXME ugly cast |
| 5364 | (audio_output_flags_t) flags, |
| 5365 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5366 | firstInexact = profile; |
| 5367 | } |
| 5368 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5369 | } |
| 5370 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5371 | if (firstInexact != nullptr) { |
| 5372 | samplingRate = updatedSamplingRate; |
| 5373 | format = updatedFormat; |
| 5374 | channelMask = updatedChannelMask; |
| 5375 | return firstInexact; |
| 5376 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5377 | return NULL; |
| 5378 | } |
| 5379 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5380 | sp<DeviceDescriptor> AudioPolicyManager::getDeviceAndMixForAttributes( |
| 5381 | const audio_attributes_t &attributes, AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5382 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5383 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5384 | // case the last active client route is used |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5385 | sp<DeviceDescriptor> device = |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5386 | findPreferredDevice(mInputs, attributes.source, mAvailableInputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5387 | if (device != nullptr) { |
| 5388 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5389 | } |
| 5390 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5391 | sp<DeviceDescriptor> selectedDeviceFromMix = |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5392 | mPolicyMixes.getDeviceAndMixForInputSource(attributes.source, mAvailableInputDevices, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5393 | policyMix); |
| 5394 | return (selectedDeviceFromMix != nullptr) ? |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5395 | selectedDeviceFromMix : getDeviceForAttributes(attributes); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5396 | } |
| 5397 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5398 | sp<DeviceDescriptor> AudioPolicyManager::getDeviceForAttributes(const audio_attributes_t &attributes) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5399 | { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5400 | audio_devices_t device = mEngine->getDeviceForInputSource(attributes.source); |
| 5401 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 5402 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 5403 | return mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 5404 | String8(attributes.tags + strlen("addr="))); |
| 5405 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5406 | return mAvailableInputDevices.getDevice(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5407 | } |
| 5408 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5409 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5410 | int index, |
| 5411 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5412 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5413 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5414 | |
| 5415 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5416 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5417 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5418 | // the ringtone volume |
| 5419 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5420 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5421 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5422 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5423 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5424 | } |
| 5425 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5426 | // in-call: always cap volume by voice volume + some low headroom |
| 5427 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5428 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5429 | switch (stream) { |
| 5430 | case AUDIO_STREAM_SYSTEM: |
| 5431 | case AUDIO_STREAM_RING: |
| 5432 | case AUDIO_STREAM_MUSIC: |
| 5433 | case AUDIO_STREAM_ALARM: |
| 5434 | case AUDIO_STREAM_NOTIFICATION: |
| 5435 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5436 | case AUDIO_STREAM_DTMF: |
| 5437 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5438 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5439 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5440 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5441 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5442 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5443 | if (volumeDB > maxVoiceVolDb) { |
| 5444 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5445 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5446 | volumeDB = maxVoiceVolDb; |
| 5447 | } |
| 5448 | } break; |
| 5449 | default: |
| 5450 | break; |
| 5451 | } |
| 5452 | } |
| 5453 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5454 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5455 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5456 | // - always attenuate notifications volume by 6dB |
| 5457 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5458 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5459 | // - if music is playing, always limit the volume to current music volume, |
| 5460 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5461 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5462 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5463 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5464 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5465 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5466 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5467 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5468 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5469 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5470 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5471 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5472 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5473 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5474 | // when the phone is ringing we must consider that music could have been paused just before |
| 5475 | // by the music application and behave as if music was active if the last music track was |
| 5476 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5477 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5478 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5479 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5480 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5481 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5482 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5483 | musicDevice), |
| 5484 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5485 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5486 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5487 | if (volumeDB > minVolDB) { |
| 5488 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5489 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5490 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5491 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5492 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5493 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5494 | // intended to be played |
| 5495 | if ((volumeDB > -96.0f) && |
| 5496 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5497 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5498 | stream, device, |
| 5499 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5500 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5501 | } |
| 5502 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5503 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5504 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5505 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5506 | } |
| 5507 | } |
| 5508 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5509 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5510 | } |
| 5511 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5512 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5513 | audio_stream_type_t srcStream, |
| 5514 | audio_stream_type_t dstStream) |
| 5515 | { |
| 5516 | if (srcStream == dstStream) { |
| 5517 | return srcIndex; |
| 5518 | } |
| 5519 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5520 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5521 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5522 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5523 | |
| 5524 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5525 | } |
| 5526 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5527 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5528 | int index, |
| 5529 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5530 | audio_devices_t device, |
| 5531 | int delayMs, |
| 5532 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5533 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5534 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5535 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5536 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5537 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5538 | return NO_ERROR; |
| 5539 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5540 | audio_policy_forced_cfg_t forceUseForComm = |
| 5541 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5542 | // 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] | 5543 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5544 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5545 | 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] | 5546 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5547 | return INVALID_OPERATION; |
| 5548 | } |
| 5549 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5550 | if (device == AUDIO_DEVICE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5551 | device = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5552 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5553 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5554 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5555 | if (outputDesc->isFixedVolume(device) || |
| 5556 | // Force VoIP volume to max for bluetooth SCO |
| 5557 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5558 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5559 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5560 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5561 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5562 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5563 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5564 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5565 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5566 | float voiceVolume; |
| 5567 | // 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] | 5568 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5569 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5570 | } else { |
| 5571 | voiceVolume = 1.0; |
| 5572 | } |
| 5573 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5574 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5575 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5576 | mLastVoiceVolume = voiceVolume; |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | return NO_ERROR; |
| 5581 | } |
| 5582 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5583 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5584 | audio_devices_t device, |
| 5585 | int delayMs, |
| 5586 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5587 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5588 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5589 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5590 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5591 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5592 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5593 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5594 | device, |
| 5595 | delayMs, |
| 5596 | force); |
| 5597 | } |
| 5598 | } |
| 5599 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5600 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5601 | bool on, |
| 5602 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5603 | int delayMs, |
| 5604 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5605 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5606 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5607 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5608 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5609 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5610 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5611 | } |
| 5612 | } |
| 5613 | } |
| 5614 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5615 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5616 | bool on, |
| 5617 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5618 | int delayMs, |
| 5619 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5620 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5621 | if (device == AUDIO_DEVICE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5622 | device = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5623 | } |
| 5624 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5625 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5626 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5627 | |
| 5628 | if (on) { |
| 5629 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5630 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5631 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5632 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5633 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5634 | } |
| 5635 | } |
| 5636 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5637 | outputDesc->mMuteCount[stream]++; |
| 5638 | } else { |
| 5639 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5640 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5641 | return; |
| 5642 | } |
| 5643 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5644 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5645 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5646 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5647 | device, |
| 5648 | delayMs); |
| 5649 | } |
| 5650 | } |
| 5651 | } |
| 5652 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5653 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5654 | { |
| 5655 | // flags to stream type mapping |
| 5656 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5657 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5658 | } |
| 5659 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5660 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5661 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5662 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5663 | return AUDIO_STREAM_TTS; |
| 5664 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5665 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5666 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5667 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5668 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5669 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5670 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5671 | // has flags that map to a strategy? |
| 5672 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5673 | return true; |
| 5674 | } |
| 5675 | |
| 5676 | // has known usage? |
| 5677 | switch (paa->usage) { |
| 5678 | case AUDIO_USAGE_UNKNOWN: |
| 5679 | case AUDIO_USAGE_MEDIA: |
| 5680 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5681 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5682 | case AUDIO_USAGE_ALARM: |
| 5683 | case AUDIO_USAGE_NOTIFICATION: |
| 5684 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5685 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5686 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5687 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5688 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5689 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5690 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5691 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5692 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5693 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5694 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5695 | break; |
| 5696 | default: |
| 5697 | return false; |
| 5698 | } |
| 5699 | return true; |
| 5700 | } |
| 5701 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5702 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5703 | routing_strategy strategy, uint32_t inPastMs, |
| 5704 | nsecs_t sysTime) const |
| 5705 | { |
| 5706 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5707 | sysTime = systemTime(); |
| 5708 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5709 | 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] | 5710 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5711 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5712 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5713 | return true; |
| 5714 | } |
| 5715 | } |
| 5716 | return false; |
| 5717 | } |
| 5718 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5719 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 5720 | routing_strategy strategy, uint32_t inPastMs, |
| 5721 | nsecs_t sysTime) const |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5722 | { |
| 5723 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5724 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5725 | if (outputDesc->sharesHwModuleWith(desc) |
| 5726 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5727 | return true; |
| 5728 | } |
| 5729 | } |
| 5730 | return false; |
| 5731 | } |
| 5732 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5733 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5734 | { |
| 5735 | return mEngine->getForceUse(usage); |
| 5736 | } |
| 5737 | |
| 5738 | bool AudioPolicyManager::isInCall() |
| 5739 | { |
| 5740 | return isStateInCall(mEngine->getPhoneState()); |
| 5741 | } |
| 5742 | |
| 5743 | bool AudioPolicyManager::isStateInCall(int state) |
| 5744 | { |
| 5745 | return is_state_in_call(state); |
| 5746 | } |
| 5747 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5748 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5749 | { |
| 5750 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5751 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5752 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5753 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5754 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5755 | } |
| 5756 | } |
| 5757 | |
| 5758 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5759 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5760 | bool release = false; |
| 5761 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5762 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5763 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5764 | source->ext.device.type == deviceDesc->type()) { |
| 5765 | release = true; |
| 5766 | } |
| 5767 | } |
| 5768 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5769 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5770 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5771 | sink->ext.device.type == deviceDesc->type()) { |
| 5772 | release = true; |
| 5773 | } |
| 5774 | } |
| 5775 | if (release) { |
| 5776 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5777 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5778 | } |
| 5779 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame^] | 5780 | |
| 5781 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5782 | } |
| 5783 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5784 | void AudioPolicyManager::modifySurroundFormats( |
| 5785 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5786 | std::unordered_set<audio_format_t> enforcedSurround( |
| 5787 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5788 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 5789 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5790 | allSurround.insert(pair.first); |
| 5791 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 5792 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5793 | |
| 5794 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5795 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5796 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5797 | // This is the resulting set of formats depending on the surround mode: |
| 5798 | // 'all surround' = allSurround |
| 5799 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 5800 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 5801 | // 'manual surround' = mManualSurroundFormats |
| 5802 | // AUTO: formats v 'enforced surround' |
| 5803 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 5804 | // NEVER: formats ^ 'non-surround' |
| 5805 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5806 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5807 | std::unordered_set<audio_format_t> formatSet; |
| 5808 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 5809 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5810 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5811 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5812 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5813 | formatSet.insert(*formatIter); |
| 5814 | } |
| 5815 | } |
| 5816 | } else { |
| 5817 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 5818 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5819 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5820 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5821 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5822 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5823 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 5824 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 5825 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5826 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5827 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 5828 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5829 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5830 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5831 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5832 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5833 | for (const auto& format : formatSet) { |
| 5834 | formatsPtr->push(format); |
| 5835 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5836 | } |
| 5837 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5838 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5839 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5840 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5841 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5842 | |
| 5843 | // If NEVER, then remove support for channelMasks > stereo. |
| 5844 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5845 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5846 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5847 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5848 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5849 | channelMasks.removeAt(maskIndex); |
| 5850 | } else { |
| 5851 | maskIndex++; |
| 5852 | } |
| 5853 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5854 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5855 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5856 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5857 | bool supports5dot1 = false; |
| 5858 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5859 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5860 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5861 | supports5dot1 = true; |
| 5862 | break; |
| 5863 | } |
| 5864 | } |
| 5865 | // If not then add 5.1 support. |
| 5866 | if (!supports5dot1) { |
| 5867 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5868 | ALOGI("%s: force MANUAL or ALWAYS, so adding channelMask for 5.1 surround", __func__); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5869 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5870 | } |
| 5871 | } |
| 5872 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5873 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5874 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5875 | AudioProfileVector &profiles) |
| 5876 | { |
| 5877 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5878 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5879 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5880 | // Format MUST be checked first to update the list of AudioProfile |
| 5881 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5882 | reply = mpClientInterface->getParameters( |
| 5883 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5884 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5885 | AudioParameter repliedParameters(reply); |
| 5886 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5887 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5888 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5889 | return; |
| 5890 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5891 | FormatVector formats = formatsFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5892 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5893 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5894 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5895 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5896 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5897 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5898 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5899 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5900 | ChannelsVector channelMasks; |
| 5901 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5902 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5903 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5904 | |
| 5905 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5906 | reply = mpClientInterface->getParameters( |
| 5907 | ioHandle, |
| 5908 | requestedParameters.toString() + ";" + |
| 5909 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5910 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5911 | AudioParameter repliedParameters(reply); |
| 5912 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5913 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5914 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5915 | } |
| 5916 | } |
| 5917 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5918 | reply = mpClientInterface->getParameters(ioHandle, |
| 5919 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5920 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5921 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5922 | AudioParameter repliedParameters(reply); |
| 5923 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5924 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5925 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5926 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5927 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5928 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5929 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5930 | } |
| 5931 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5932 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5933 | } |
| 5934 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5935 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5936 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5937 | audio_patch_handle_t *patchHandle, |
| 5938 | AudioIODescriptorInterface *ioDescriptor, |
| 5939 | const struct audio_patch *patch, |
| 5940 | int delayMs) |
| 5941 | { |
| 5942 | ssize_t index = mAudioPatches.indexOfKey( |
| 5943 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 5944 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 5945 | sp<AudioPatch> patchDesc; |
| 5946 | status_t status = installPatch( |
| 5947 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 5948 | if (status == NO_ERROR) { |
| 5949 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 5950 | } |
| 5951 | return status; |
| 5952 | } |
| 5953 | |
| 5954 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 5955 | ssize_t index, |
| 5956 | audio_patch_handle_t *patchHandle, |
| 5957 | const struct audio_patch *patch, |
| 5958 | int delayMs, |
| 5959 | uid_t uid, |
| 5960 | sp<AudioPatch> *patchDescPtr) |
| 5961 | { |
| 5962 | sp<AudioPatch> patchDesc; |
| 5963 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5964 | if (index >= 0) { |
| 5965 | patchDesc = mAudioPatches.valueAt(index); |
| 5966 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5967 | } |
| 5968 | |
| 5969 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 5970 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 5971 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 5972 | if (status == NO_ERROR) { |
| 5973 | if (index < 0) { |
| 5974 | patchDesc = new AudioPatch(patch, uid); |
| 5975 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5976 | } else { |
| 5977 | patchDesc->mPatch = *patch; |
| 5978 | } |
| 5979 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 5980 | if (patchHandle) { |
| 5981 | *patchHandle = patchDesc->mHandle; |
| 5982 | } |
| 5983 | nextAudioPortGeneration(); |
| 5984 | mpClientInterface->onAudioPatchListUpdate(); |
| 5985 | } |
| 5986 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 5987 | return status; |
| 5988 | } |
| 5989 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 5990 | } // namespace android |