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, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 84 | const char *device_name, |
| 85 | audio_format_t encodedFormat) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 86 | { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 87 | status_t status = setDeviceConnectionStateInt(device, state, device_address, |
| 88 | device_name, encodedFormat); |
jiabin | a7b4379 | 2018-02-15 16:04:46 -0800 | [diff] [blame] | 89 | nextAudioPortGeneration(); |
| 90 | return status; |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 91 | } |
| 92 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 93 | void AudioPolicyManager::broadcastDeviceConnectionState(const sp<DeviceDescriptor> &device, |
| 94 | audio_policy_dev_state_t state) |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 95 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 96 | AudioParameter param(device->address()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 97 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 98 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 99 | param.addInt(key, device->type()); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 100 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 101 | } |
| 102 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 103 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t deviceType, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 104 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 105 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 106 | const char *device_name, |
| 107 | audio_format_t encodedFormat) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 108 | { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 109 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s format 0x%X", |
| 110 | deviceType, state, device_address, device_name, encodedFormat); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 111 | |
| 112 | // connect/disconnect only 1 device at a time |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 113 | 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] | 114 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 115 | sp<DeviceDescriptor> device = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 116 | mHwModules.getDeviceDescriptor(deviceType, device_address, device_name, encodedFormat, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 117 | state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
| 118 | if (device == 0) { |
| 119 | return INVALID_OPERATION; |
| 120 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 121 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 122 | // handle output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 123 | if (audio_is_output_device(deviceType)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 124 | SortedVector <audio_io_handle_t> outputs; |
| 125 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 126 | ssize_t index = mAvailableOutputDevices.indexOf(device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 127 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 128 | // save a copy of the opened output descriptors before any output is opened or closed |
| 129 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 130 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 131 | switch (state) |
| 132 | { |
| 133 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 134 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 135 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 136 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 137 | return INVALID_OPERATION; |
| 138 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 139 | ALOGV("%s() connecting device %s format %x", |
| 140 | __func__, device->toString().c_str(), encodedFormat); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 141 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 142 | // register new device as available |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 143 | index = mAvailableOutputDevices.add(device); |
| 144 | if (index >= 0) { |
| 145 | sp<HwModule> module = mHwModules.getModuleForDevice(device, encodedFormat); |
| 146 | if (module == 0) { |
| 147 | ALOGD("setDeviceConnectionState() could not find HW module for device %s", |
| 148 | device->toString().c_str()); |
| 149 | mAvailableOutputDevices.remove(device); |
| 150 | return INVALID_OPERATION; |
| 151 | } |
| 152 | ALOGV("setDeviceConnectionState() module name=%s", module->getName()); |
| 153 | mAvailableOutputDevices[index]->attach(module); |
| 154 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 155 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 156 | } |
| 157 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 158 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 159 | // parameters on newly connected devices (instead of opening the outputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 160 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 161 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 162 | if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) { |
| 163 | mAvailableOutputDevices.remove(device); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 164 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 165 | mHwModules.cleanUpForDevice(device); |
| 166 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 167 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 168 | return INVALID_OPERATION; |
| 169 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 170 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 171 | mEngine->setDeviceConnectionState(device, state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 172 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 173 | // outputs should never be empty here |
| 174 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 175 | "checkOutputsForDevice() returned no outputs but status OK"); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 176 | ALOGV("%s() checkOutputsForDevice() returned %zu outputs", __func__, outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 177 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 178 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 179 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 180 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 181 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 182 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | return INVALID_OPERATION; |
| 184 | } |
| 185 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 186 | ALOGV("%s() disconnecting output device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 187 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 188 | // Send Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 189 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 190 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | // remove device from available output devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 192 | mAvailableOutputDevices.remove(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 193 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 194 | checkOutputsForDevice(device, state, outputs); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 195 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 196 | // Reset active device codec |
| 197 | device->setEncodedFormat(AUDIO_FORMAT_DEFAULT); |
| 198 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 199 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 200 | mEngine->setDeviceConnectionState(device, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 201 | } break; |
| 202 | |
| 203 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 204 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 205 | return BAD_VALUE; |
| 206 | } |
| 207 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 208 | checkForDeviceAndOutputChanges([&]() { |
| 209 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 210 | if (!outputs.isEmpty()) { |
| 211 | for (audio_io_handle_t output : outputs) { |
| 212 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 213 | // close unused outputs after device disconnection or direct outputs that have |
| 214 | // been opened by checkOutputsForDevice() to query dynamic parameters |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 215 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
| 216 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 217 | (desc->mDirectOpenCount == 0))) { |
| 218 | closeOutput(output); |
| 219 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 220 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 221 | // check A2DP again after closing A2DP output to reset mA2dpSuspended if needed |
| 222 | return true; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 224 | return false; |
| 225 | }); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 226 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 227 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 228 | DeviceVector newDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 229 | updateCallRouting(newDevices); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 230 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 231 | const DeviceVector msdOutDevices = getMsdAudioOutDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 232 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 233 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 234 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 235 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 236 | // do not force device change on duplicated output because if device is 0, it will |
| 237 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 238 | // a valid device selection on those outputs. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 239 | bool force = (msdOutDevices.isEmpty() || msdOutDevices != desc->devices()) |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 240 | && !desc->isDuplicated() |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 241 | && (!device_distinguishes_on_address(deviceType) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 242 | // always force when disconnecting (a non-duplicated device) |
| 243 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 244 | setOutputDevices(desc, newDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 245 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 248 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 249 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 252 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 253 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 254 | } // end if is output device |
| 255 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 256 | // handle input devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 257 | if (audio_is_input_device(deviceType)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 258 | SortedVector <audio_io_handle_t> inputs; |
| 259 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 260 | ssize_t index = mAvailableInputDevices.indexOf(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 261 | switch (state) |
| 262 | { |
| 263 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 264 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 265 | if (index >= 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 266 | ALOGW("%s() device already connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 | return INVALID_OPERATION; |
| 268 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 269 | sp<HwModule> module = mHwModules.getModuleForDevice(device, AUDIO_FORMAT_DEFAULT); |
| 270 | if (module == NULL) { |
| 271 | ALOGW("setDeviceConnectionState(): could not find HW module for device %s", |
| 272 | device->toString().c_str()); |
| 273 | return INVALID_OPERATION; |
| 274 | } |
| 275 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 276 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 277 | // parameters on newly connected devices (instead of opening the inputs...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 278 | broadcastDeviceConnectionState(device, state); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 279 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 280 | if (checkInputsForDevice(device, state, inputs) != NO_ERROR) { |
| 281 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 282 | |
| 283 | mHwModules.cleanUpForDevice(device); |
| 284 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 285 | return INVALID_OPERATION; |
| 286 | } |
| 287 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 288 | if (mAvailableInputDevices.add(device) < 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 289 | return NO_MEMORY; |
| 290 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 291 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 292 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 293 | mEngine->setDeviceConnectionState(device, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 294 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 295 | |
| 296 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 297 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 298 | if (index < 0) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 299 | ALOGW("%s() device not connected: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 300 | return INVALID_OPERATION; |
| 301 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 302 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 303 | ALOGV("%s() disconnecting input device %s", __func__, device->toString().c_str()); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 304 | |
| 305 | // Set Disconnect to HALs |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 306 | broadcastDeviceConnectionState(device, state); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 307 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 308 | checkInputsForDevice(device, state, inputs); |
| 309 | mAvailableInputDevices.remove(device); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 310 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 311 | // Propagate device availability to Engine |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 312 | mEngine->setDeviceConnectionState(device, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 313 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 314 | |
| 315 | default: |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 316 | ALOGE("%s() invalid state: %x", __func__, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 317 | return BAD_VALUE; |
| 318 | } |
| 319 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 320 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 321 | // As the input device list can impact the output device selection, update |
| 322 | // getDeviceForStrategy() cache |
| 323 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 324 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 325 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 326 | DeviceVector newDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
| 327 | updateCallRouting(newDevices); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 330 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 331 | cleanUpForDevice(device); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 334 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 335 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 336 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 337 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 338 | ALOGW("%s() invalid device: %s", __func__, device->toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 339 | return BAD_VALUE; |
| 340 | } |
| 341 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 342 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 343 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 344 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 345 | sp<DeviceDescriptor> devDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 346 | mHwModules.getDeviceDescriptor(device, device_address, "", AUDIO_FORMAT_DEFAULT, |
| 347 | false /* allowToCreate */, |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 348 | (strlen(device_address) != 0)/*matchAddress*/); |
| 349 | |
| 350 | if (devDesc == 0) { |
| 351 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 352 | device, device_address); |
| 353 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 354 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 355 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 356 | DeviceVector *deviceVector; |
| 357 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 358 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 359 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 360 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 361 | deviceVector = &mAvailableInputDevices; |
| 362 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 363 | ALOGW("%s() invalid device type %08x", __func__, device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 364 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 365 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 366 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 367 | return (deviceVector->getDevice( |
| 368 | device, String8(device_address), AUDIO_FORMAT_DEFAULT) != 0) ? |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 369 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 372 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 373 | const char *device_address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 374 | const char *device_name, |
| 375 | audio_format_t encodedFormat) |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 376 | { |
| 377 | status_t status; |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 378 | String8 reply; |
| 379 | AudioParameter param; |
| 380 | int isReconfigA2dpSupported = 0; |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 381 | |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 382 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s encodedFormat: 0x%X", |
| 383 | device, device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 384 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 385 | // connect/disconnect only 1 device at a time |
| 386 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 387 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 388 | // Check if the device is currently connected |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 389 | DeviceVector availableDevices = getAvailableOutputDevices(); |
| 390 | DeviceVector deviceList = availableDevices.getDevicesFromTypeMask(device); |
| 391 | if (deviceList.empty()) { |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 392 | // Nothing to do: device is not connected |
| 393 | return NO_ERROR; |
| 394 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 395 | sp<DeviceDescriptor> devDesc = deviceList.itemAt(0); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 396 | |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 397 | // For offloaded A2DP, Hw modules may have the capability to |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 398 | // configure codecs. |
| 399 | // Handle two specific cases by sending a set parameter to |
| 400 | // configure A2DP codecs. No need to toggle device state. |
| 401 | // Case 1: A2DP active device switches from primary to primary |
| 402 | // module |
| 403 | // Case 2: A2DP device config changes on primary module. |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 404 | if (device & AUDIO_DEVICE_OUT_ALL_A2DP) { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 405 | sp<HwModule> module = mHwModules.getModuleForDeviceTypes(device, encodedFormat); |
| 406 | audio_module_handle_t primaryHandle = mPrimaryOutput->getModuleHandle(); |
| 407 | if (availablePrimaryOutputDevices().contains(devDesc) && |
| 408 | (module != 0 && module->getHandle() == primaryHandle)) { |
| 409 | reply = mpClientInterface->getParameters( |
| 410 | AUDIO_IO_HANDLE_NONE, |
| 411 | String8(AudioParameter::keyReconfigA2dpSupported)); |
| 412 | AudioParameter repliedParameters(reply); |
| 413 | repliedParameters.getInt( |
| 414 | String8(AudioParameter::keyReconfigA2dpSupported), isReconfigA2dpSupported); |
| 415 | if (isReconfigA2dpSupported) { |
| 416 | const String8 key(AudioParameter::keyReconfigA2dp); |
| 417 | param.add(key, String8("true")); |
| 418 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 419 | devDesc->setEncodedFormat(encodedFormat); |
| 420 | return NO_ERROR; |
| 421 | } |
Aniket Kumar Lata | 3432e04 | 2018-04-06 14:22:15 -0700 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 425 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 426 | // This will force reading again the device configuration |
| 427 | status = setDeviceConnectionState(device, |
| 428 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 429 | device_address, device_name, |
| 430 | devDesc->getEncodedFormat()); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 431 | if (status != NO_ERROR) { |
| 432 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 433 | status); |
| 434 | return status; |
| 435 | } |
| 436 | |
| 437 | status = setDeviceConnectionState(device, |
| 438 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 439 | device_address, device_name, encodedFormat); |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 440 | if (status != NO_ERROR) { |
| 441 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 442 | status); |
| 443 | return status; |
| 444 | } |
| 445 | |
| 446 | return NO_ERROR; |
| 447 | } |
| 448 | |
Arun Mirpuri | 11029ad | 2018-12-19 20:45:19 -0800 | [diff] [blame^] | 449 | status_t AudioPolicyManager::getHwOffloadEncodingFormatsSupportedForA2DP( |
| 450 | std::vector<audio_format_t> *formats) |
| 451 | { |
| 452 | ALOGV("getHwOffloadEncodingFormatsSupportedForA2DP()"); |
| 453 | char *tok = NULL, *saveptr; |
| 454 | status_t status = NO_ERROR; |
| 455 | char encoding_formats_list[PROPERTY_VALUE_MAX]; |
| 456 | audio_format_t format = AUDIO_FORMAT_DEFAULT; |
| 457 | // FIXME This list should not come from a property but the supported encoded |
| 458 | // formats of declared A2DP devices in primary module |
| 459 | property_get("persist.bluetooth.a2dp_offload.cap", encoding_formats_list, ""); |
| 460 | tok = strtok_r(encoding_formats_list, "-", &saveptr); |
| 461 | for (;tok != NULL; tok = strtok_r(NULL, "-", &saveptr)) { |
| 462 | if (strcmp(tok, "sbc") == 0) { |
| 463 | ALOGV("%s: SBC offload supported\n",__func__); |
| 464 | format = AUDIO_FORMAT_SBC; |
| 465 | } else if (strcmp(tok, "aptx") == 0) { |
| 466 | ALOGV("%s: APTX offload supported\n",__func__); |
| 467 | format = AUDIO_FORMAT_APTX; |
| 468 | } else if (strcmp(tok, "aptxhd") == 0) { |
| 469 | ALOGV("%s: APTX HD offload supported\n",__func__); |
| 470 | format = AUDIO_FORMAT_APTX_HD; |
| 471 | } else if (strcmp(tok, "ldac") == 0) { |
| 472 | ALOGV("%s: LDAC offload supported\n",__func__); |
| 473 | format = AUDIO_FORMAT_LDAC; |
| 474 | } else if (strcmp(tok, "aac") == 0) { |
| 475 | ALOGV("%s: AAC offload supported\n",__func__); |
| 476 | format = AUDIO_FORMAT_AAC; |
| 477 | } else { |
| 478 | ALOGE("%s: undefined token - %s\n",__func__, tok); |
| 479 | continue; |
| 480 | } |
| 481 | formats->push_back(format); |
| 482 | } |
| 483 | return status; |
| 484 | } |
| 485 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 486 | uint32_t AudioPolicyManager::updateCallRouting(const DeviceVector &rxDevices, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 487 | { |
| 488 | bool createTxPatch = false; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 489 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 490 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 491 | if(!hasPrimaryOutput() || mPrimaryOutput->devices().types() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 492 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 493 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 494 | ALOG_ASSERT(!rxDevices.isEmpty(), "updateCallRouting() no selected output device"); |
| 495 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 496 | audio_attributes_t attr = { .source = AUDIO_SOURCE_VOICE_COMMUNICATION }; |
| 497 | auto txDevice = getDeviceAndMixForAttributes(attr); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 498 | ALOGV("updateCallRouting device rxDevice %s txDevice %s", |
| 499 | rxDevices.toString().c_str(), txDevice->toString().c_str()); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 500 | |
| 501 | // release existing RX patch if any |
| 502 | if (mCallRxPatch != 0) { |
| 503 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 504 | mCallRxPatch.clear(); |
| 505 | } |
| 506 | // release TX patch if any |
| 507 | if (mCallTxPatch != 0) { |
| 508 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 509 | mCallTxPatch.clear(); |
| 510 | } |
| 511 | |
| 512 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 513 | // via setOutputDevice() on primary output. |
| 514 | // Otherwise, create two audio patches for TX and RX path. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 515 | if (availablePrimaryOutputDevices().contains(rxDevices.itemAt(0))) { |
| 516 | muteWaitMs = setOutputDevices(mPrimaryOutput, rxDevices, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 517 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 518 | // of it due to legacy implementation. If not, create a patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 519 | if (!availablePrimaryModuleInputDevices().contains(txDevice)) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 520 | createTxPatch = true; |
| 521 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 522 | } else { // create RX path audio patch |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 523 | mCallRxPatch = createTelephonyPatch(true /*isRx*/, rxDevices.itemAt(0), delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 524 | createTxPatch = true; |
| 525 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 526 | if (createTxPatch) { // create TX path audio patch |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 527 | mCallTxPatch = createTelephonyPatch(false /*isRx*/, txDevice, delayMs); |
| 528 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 529 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 530 | return muteWaitMs; |
| 531 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 532 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 533 | sp<AudioPatch> AudioPolicyManager::createTelephonyPatch( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 534 | bool isRx, const sp<DeviceDescriptor> &device, uint32_t delayMs) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 535 | PatchBuilder patchBuilder; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 536 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 537 | if (device == nullptr) { |
| 538 | return nullptr; |
| 539 | } |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 540 | if (isRx) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 541 | patchBuilder.addSink(device). |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 542 | addSource(mAvailableInputDevices.getDevice( |
| 543 | AUDIO_DEVICE_IN_TELEPHONY_RX, String8(), AUDIO_FORMAT_DEFAULT)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 544 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 545 | patchBuilder.addSource(device). |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 546 | addSink(mAvailableOutputDevices.getDevice( |
| 547 | AUDIO_DEVICE_OUT_TELEPHONY_TX, String8(), AUDIO_FORMAT_DEFAULT)); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 548 | } |
| 549 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 550 | // @TODO: still ignoring the address, or not dealing platform with mutliple telephonydevices |
| 551 | const sp<DeviceDescriptor> outputDevice = isRx ? |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 552 | device : mAvailableOutputDevices.getDevice( |
| 553 | AUDIO_DEVICE_OUT_TELEPHONY_TX, String8(), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 554 | SortedVector<audio_io_handle_t> outputs = |
| 555 | getOutputsForDevices(DeviceVector(outputDevice), mOutputs); |
| 556 | 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] | 557 | // request to reuse existing output stream if one is already opened to reach the target device |
| 558 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 559 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 560 | ALOG_ASSERT(!outputDesc->isDuplicated(), "%s() %s device output %d is duplicated", __func__, |
| 561 | outputDevice->toString().c_str(), output); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 562 | patchBuilder.addSource(outputDesc, { .stream = AUDIO_STREAM_PATCH }); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | if (!isRx) { |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 566 | // terminate active capture if on the same HW module as the call TX source device |
| 567 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 568 | // call TX device but this information is not in the audio patch and logic here must be |
| 569 | // symmetric to the one in startInput() |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 570 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 571 | if (activeDesc->hasSameHwModuleAs(device)) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 572 | closeActiveClients(activeDesc); |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 573 | } |
| 574 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 575 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 576 | |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 577 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 578 | status_t status = mpClientInterface->createAudioPatch( |
| 579 | patchBuilder.patch(), &afPatchHandle, delayMs); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 580 | ALOGW_IF(status != NO_ERROR, |
| 581 | "%s() error %d creating %s audio patch", __func__, status, isRx ? "RX" : "TX"); |
| 582 | sp<AudioPatch> audioPatch; |
| 583 | if (status == NO_ERROR) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 584 | audioPatch = new AudioPatch(patchBuilder.patch(), mUidCached); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 585 | audioPatch->mAfPatchHandle = afPatchHandle; |
| 586 | audioPatch->mUid = mUidCached; |
| 587 | } |
| 588 | return audioPatch; |
| 589 | } |
| 590 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 591 | sp<DeviceDescriptor> AudioPolicyManager::findDevice( |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 592 | const DeviceVector& devices, audio_devices_t device) const { |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 593 | DeviceVector deviceList = devices.getDevicesFromTypeMask(device); |
Mikhail Naganov | b567ba0 | 2017-12-08 11:16:27 -0800 | [diff] [blame] | 594 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 595 | "%s() selected device type %#x is not in devices list", __func__, device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 596 | return deviceList.itemAt(0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 599 | audio_devices_t AudioPolicyManager::getModuleDeviceTypes( |
| 600 | const DeviceVector& devices, const char *moduleId) const { |
| 601 | sp<HwModule> mod = mHwModules.getModuleFromName(moduleId); |
| 602 | return mod != 0 ? devices.getDeviceTypesFromHwModule(mod->getHandle()) : AUDIO_DEVICE_NONE; |
| 603 | } |
| 604 | |
| 605 | bool AudioPolicyManager::isDeviceOfModule( |
| 606 | const sp<DeviceDescriptor>& devDesc, const char *moduleId) const { |
| 607 | sp<HwModule> module = mHwModules.getModuleFromName(moduleId); |
| 608 | if (module != 0) { |
| 609 | return mAvailableOutputDevices.getDevicesFromHwModule(module->getHandle()) |
| 610 | .indexOf(devDesc) != NAME_NOT_FOUND |
| 611 | || mAvailableInputDevices.getDevicesFromHwModule(module->getHandle()) |
| 612 | .indexOf(devDesc) != NAME_NOT_FOUND; |
| 613 | } |
| 614 | return false; |
| 615 | } |
| 616 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 617 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 618 | { |
| 619 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 620 | // store previous phone state for management of sonification strategy below |
| 621 | int oldState = mEngine->getPhoneState(); |
| 622 | |
| 623 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 624 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 625 | return; |
| 626 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 627 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 628 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 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 stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 631 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | } |
| 633 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 634 | /** |
| 635 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 636 | * routing command. |
| 637 | */ |
| 638 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 639 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 640 | |
| 641 | // check for device and output changes triggered by new phone state |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 642 | checkForDeviceAndOutputChanges(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 643 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 644 | int delayMs = 0; |
| 645 | if (isStateInCall(state)) { |
| 646 | nsecs_t sysTime = systemTime(); |
| 647 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 648 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 649 | // mute media and sonification strategies and delay device switch by the largest |
| 650 | // latency of any output where either strategy is active. |
| 651 | // 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] | 652 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 653 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 654 | sysTime) || |
| 655 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 656 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 657 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 658 | (delayMs < (int)desc->latency()*2)) { |
| 659 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 660 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 661 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 662 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 663 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 664 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 665 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 667 | } |
| 668 | } |
| 669 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 670 | if (hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 671 | // 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] | 672 | // the device returned is not necessarily reachable via this output |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 673 | DeviceVector rxDevices = getNewOutputDevices(mPrimaryOutput, false /*fromCache*/); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 674 | // force routing command to audio hardware when ending call |
| 675 | // even if no device change is needed |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 676 | if (isStateInCall(oldState) && rxDevices.isEmpty()) { |
| 677 | rxDevices = mPrimaryOutput->devices(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 678 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 679 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 680 | if (state == AUDIO_MODE_IN_CALL) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 681 | updateCallRouting(rxDevices, delayMs); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 682 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 683 | if (mCallRxPatch != 0) { |
| 684 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 685 | mCallRxPatch.clear(); |
| 686 | } |
| 687 | if (mCallTxPatch != 0) { |
| 688 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 689 | mCallTxPatch.clear(); |
| 690 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 691 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 692 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 693 | setOutputDevices(mPrimaryOutput, rxDevices, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 694 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 695 | } |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 696 | |
| 697 | // reevaluate routing on all outputs in case tracks have been started during the call |
| 698 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 699 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 700 | DeviceVector newDevices = getNewOutputDevices(desc, true /*fromCache*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 701 | if (state != AUDIO_MODE_IN_CALL || desc != mPrimaryOutput) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 702 | setOutputDevices(desc, newDevices, !newDevices.isEmpty(), 0 /*delayMs*/); |
Eric Laurent | 2e2a8a9 | 2018-04-20 16:21:33 -0700 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 706 | if (isStateInCall(state)) { |
| 707 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 708 | // force reevaluating accessibility routing when call starts |
| 709 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // 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] | 713 | if (state == AUDIO_MODE_RINGTONE && |
| 714 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 715 | mLimitRingtoneVolume = true; |
| 716 | } else { |
| 717 | mLimitRingtoneVolume = false; |
| 718 | } |
| 719 | } |
| 720 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 721 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 722 | return mEngine->getPhoneState(); |
| 723 | } |
| 724 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 725 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 726 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 727 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 728 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 729 | if (config == mEngine->getForceUse(usage)) { |
| 730 | return; |
| 731 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 732 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 733 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 734 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 735 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 736 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 737 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 738 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 739 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 740 | |
| 741 | // check for device and output changes triggered by new force usage |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 742 | checkForDeviceAndOutputChanges(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 743 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 744 | //FIXME: workaround for truncated touch sounds |
| 745 | // to be removed when the problem is handled by system UI |
| 746 | uint32_t delayMs = 0; |
| 747 | uint32_t waitMs = 0; |
| 748 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 749 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 750 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 751 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 752 | DeviceVector newDevices = getNewOutputDevices(mPrimaryOutput, true /*fromCache*/); |
| 753 | waitMs = updateCallRouting(newDevices, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 754 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 755 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 756 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 757 | DeviceVector newDevices = getNewOutputDevices(outputDesc, true /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 758 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 759 | waitMs = setOutputDevices(outputDesc, newDevices, !newDevices.isEmpty(), delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 760 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 761 | if (forceVolumeReeval && !newDevices.isEmpty()) { |
| 762 | applyStreamVolumes(outputDesc, newDevices.types(), waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 763 | } |
| 764 | } |
| 765 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 766 | for (const auto& activeDesc : mInputs.getActiveInputs()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 767 | auto newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 768 | // 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] | 769 | if (activeDesc->mProfile->getSupportedDevices().contains(newDevice)) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 770 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 771 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 772 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 773 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 774 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 775 | } |
| 776 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 777 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 778 | { |
| 779 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 780 | } |
| 781 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 782 | // Find an output profile compatible with the parameters passed. When "directOnly" is set, restrict |
| 783 | // search to profiles for direct outputs. |
| 784 | sp<IOProfile> AudioPolicyManager::getProfileForOutput( |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 785 | const DeviceVector& devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 786 | uint32_t samplingRate, |
| 787 | audio_format_t format, |
| 788 | audio_channel_mask_t channelMask, |
| 789 | audio_output_flags_t flags, |
| 790 | bool directOnly) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 791 | { |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 792 | if (directOnly) { |
| 793 | // only retain flags that will drive the direct output profile selection |
| 794 | // if explicitly requested |
| 795 | static const uint32_t kRelevantFlags = |
| 796 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 797 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
| 798 | flags = |
| 799 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 800 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 801 | |
| 802 | sp<IOProfile> profile; |
| 803 | |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 804 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 805 | for (const auto& curProfile : hwModule->getOutputProfiles()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 806 | if (!curProfile->isCompatibleProfile(devices, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 807 | samplingRate, NULL /*updatedSamplingRate*/, |
| 808 | format, NULL /*updatedFormat*/, |
| 809 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 810 | flags)) { |
| 811 | continue; |
| 812 | } |
| 813 | // reject profiles not corresponding to a device currently available |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 814 | if (!mAvailableOutputDevices.containsAtLeastOne(curProfile->getSupportedDevices())) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 815 | continue; |
| 816 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 817 | // reject profiles if connected device does not support codec |
| 818 | if (!curProfile->deviceSupportsEncodedFormats(devices.types())) { |
| 819 | continue; |
| 820 | } |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 821 | if (!directOnly) return curProfile; |
| 822 | // when searching for direct outputs, if several profiles are compatible, give priority |
| 823 | // to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 824 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 825 | continue; |
| 826 | } |
| 827 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 828 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 829 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 830 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 831 | } |
| 832 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 833 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 836 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 837 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 838 | routing_strategy strategy = getStrategy(stream); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 839 | DeviceVector devices = getDevicesForStrategy(strategy, false /*fromCache*/); |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 840 | |
| 841 | // Note that related method getOutputForAttr() uses getOutputForDevice() not selectOutput(). |
| 842 | // We use selectOutput() here since we don't have the desired AudioTrack sample rate, |
| 843 | // format, flags, etc. This may result in some discrepancy for functions that utilize |
| 844 | // getOutput() solely on audio_stream_type such as AudioSystem::getOutputFrameCount() |
| 845 | // and AudioSystem::getOutputSamplingRate(). |
| 846 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 847 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
| 848 | 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] | 849 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 850 | ALOGV("getOutput() stream %d selected devices %s, output %d", stream, |
| 851 | devices.toString().c_str(), output); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame] | 852 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 853 | } |
| 854 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 855 | status_t AudioPolicyManager::getAudioAttributes(audio_attributes_t *dstAttr, |
| 856 | const audio_attributes_t *srcAttr, |
| 857 | audio_stream_type_t srcStream) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 858 | { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 859 | if (srcAttr != NULL) { |
| 860 | if (!isValidAttributes(srcAttr)) { |
| 861 | ALOGE("%s invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 862 | __func__, |
| 863 | srcAttr->usage, srcAttr->content_type, srcAttr->flags, |
| 864 | srcAttr->tags); |
| 865 | return BAD_VALUE; |
| 866 | } |
| 867 | *dstAttr = *srcAttr; |
| 868 | } else { |
| 869 | if (srcStream < AUDIO_STREAM_MIN || srcStream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 870 | ALOGE("%s: invalid stream type", __func__); |
| 871 | return BAD_VALUE; |
| 872 | } |
| 873 | stream_type_to_audio_attributes(srcStream, dstAttr); |
| 874 | } |
| 875 | return NO_ERROR; |
| 876 | } |
| 877 | |
| 878 | status_t AudioPolicyManager::getOutputForAttrInt(audio_attributes_t *resultAttr, |
| 879 | audio_io_handle_t *output, |
| 880 | audio_session_t session, |
| 881 | const audio_attributes_t *attr, |
| 882 | audio_stream_type_t *stream, |
| 883 | uid_t uid, |
| 884 | const audio_config_t *config, |
| 885 | audio_output_flags_t *flags, |
| 886 | audio_port_handle_t *selectedDeviceId) |
| 887 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 888 | DeviceVector devices; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 889 | routing_strategy strategy; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 890 | audio_devices_t deviceType = AUDIO_DEVICE_NONE; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 891 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 892 | DeviceVector msdDevices = getMsdAudioOutDevices(); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 893 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 894 | status_t status = getAudioAttributes(resultAttr, attr, *stream); |
| 895 | if (status != NO_ERROR) { |
| 896 | return status; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 899 | ALOGV("%s usage=%d, content=%d, tag=%s flags=%08x" |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 900 | " session %d selectedDeviceId %d", |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 901 | __func__, |
| 902 | resultAttr->usage, resultAttr->content_type, resultAttr->tags, resultAttr->flags, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 903 | session, requestedPortId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 904 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 905 | *stream = streamTypefromAttributesInt(resultAttr); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 906 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 907 | strategy = getStrategyForAttr(resultAttr); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 908 | |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 909 | // First check for explicit routing (eg. setPreferredDevice) |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 910 | sp<DeviceDescriptor> requestedDevice = mAvailableOutputDevices.getDeviceFromId(requestedPortId); |
| 911 | if (requestedDevice != nullptr) { |
| 912 | deviceType = requestedDevice->type(); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 913 | } else { |
| 914 | // If no explict route, is there a matching dynamic policy that applies? |
| 915 | sp<SwAudioOutputDescriptor> desc; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 916 | if (mPolicyMixes.getOutputForAttr(*resultAttr, uid, desc) == NO_ERROR) { |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 917 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 918 | if (!audio_has_proportional_frames(config->format)) { |
| 919 | return BAD_VALUE; |
| 920 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 921 | *stream = streamTypefromAttributesInt(resultAttr); |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 922 | *output = desc->mIoHandle; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 923 | AudioMix *mix = desc->mPolicyMix; |
| 924 | sp<DeviceDescriptor> deviceDesc = |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 925 | mAvailableOutputDevices.getDevice( |
| 926 | mix->mDeviceType, mix->mDeviceAddress, AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 927 | *selectedDeviceId = deviceDesc != 0 ? deviceDesc->getId() : AUDIO_PORT_HANDLE_NONE; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 928 | ALOGV("%s returns output %d", __func__, *output); |
| 929 | return NO_ERROR; |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | // Virtual sources must always be dynamicaly or explicitly routed |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 933 | if (resultAttr->usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 934 | 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] | 935 | return BAD_VALUE; |
| 936 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 937 | deviceType = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 938 | } |
Scott Randolph | 7b1fd23 | 2018-06-18 15:33:03 -0700 | [diff] [blame] | 939 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 940 | if ((resultAttr->flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 941 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 942 | } |
| 943 | |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 944 | // Set incall music only if device was explicitly set, and fallback to the device which is |
| 945 | // chosen by the engine if not. |
| 946 | // FIXME: provide a more generic approach which is not device specific and move this back |
| 947 | // to getOutputForDevice. |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 948 | // 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] | 949 | if (deviceType == AUDIO_DEVICE_OUT_TELEPHONY_TX && |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 950 | (*stream == AUDIO_STREAM_MUSIC || resultAttr->usage == AUDIO_USAGE_VOICE_COMMUNICATION) && |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 951 | audio_is_linear_pcm(config->format) && |
| 952 | isInCall()) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 953 | if (requestedPortId != AUDIO_PORT_HANDLE_NONE) { |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 954 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_INCALL_MUSIC; |
| 955 | } else { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 956 | // 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] | 957 | deviceType = mEngine->getDeviceForStrategy(strategy); |
Nadav Bar | b2f1816 | 2018-07-18 13:01:53 +0300 | [diff] [blame] | 958 | } |
| 959 | } |
| 960 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 961 | 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] | 962 | "flags %#x", |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 963 | __func__, |
| 964 | deviceType, config->sample_rate, config->format, config->channel_mask, *flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 965 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 966 | *output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 967 | if (!msdDevices.isEmpty()) { |
| 968 | *output = getOutputForDevices(msdDevices, session, *stream, config, flags); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 969 | sp<DeviceDescriptor> deviceDesc = |
| 970 | mAvailableOutputDevices.getDevice(deviceType, String8(), AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 971 | if (*output != AUDIO_IO_HANDLE_NONE && setMsdPatch(deviceDesc) == NO_ERROR) { |
| 972 | ALOGV("%s() Using MSD devices %s instead of device %s", |
| 973 | __func__, msdDevices.toString().c_str(), deviceDesc->toString().c_str()); |
| 974 | deviceType = msdDevices.types(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 975 | } else { |
| 976 | *output = AUDIO_IO_HANDLE_NONE; |
| 977 | } |
| 978 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 979 | devices = mAvailableOutputDevices.getDevicesFromTypeMask(deviceType); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 980 | if (*output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 981 | *output = getOutputForDevices(devices, session, *stream, config, flags); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 982 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 983 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 984 | return INVALID_OPERATION; |
| 985 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 986 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 987 | *selectedDeviceId = getFirstDeviceId(devices); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 988 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 989 | ALOGV("%s returns output %d selectedDeviceId %d", __func__, *output, *selectedDeviceId); |
| 990 | |
| 991 | return NO_ERROR; |
| 992 | } |
| 993 | |
| 994 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 995 | audio_io_handle_t *output, |
| 996 | audio_session_t session, |
| 997 | audio_stream_type_t *stream, |
| 998 | uid_t uid, |
| 999 | const audio_config_t *config, |
| 1000 | audio_output_flags_t *flags, |
| 1001 | audio_port_handle_t *selectedDeviceId, |
| 1002 | audio_port_handle_t *portId) |
| 1003 | { |
| 1004 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1005 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1006 | return INVALID_OPERATION; |
| 1007 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1008 | const audio_port_handle_t requestedPortId = *selectedDeviceId; |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1009 | audio_attributes_t resultAttr; |
| 1010 | status_t status = getOutputForAttrInt(&resultAttr, output, session, attr, stream, uid, |
| 1011 | config, flags, selectedDeviceId); |
| 1012 | if (status != NO_ERROR) { |
| 1013 | return status; |
| 1014 | } |
| 1015 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1016 | audio_config_base_t clientConfig = {.sample_rate = config->sample_rate, |
| 1017 | .format = config->format, |
| 1018 | .channel_mask = config->channel_mask }; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1019 | *portId = AudioPort::getNextUniqueId(); |
| 1020 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1021 | sp<TrackClientDescriptor> clientDesc = |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1022 | new TrackClientDescriptor(*portId, uid, session, resultAttr, clientConfig, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1023 | requestedPortId, *stream, |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1024 | getStrategyForAttr(&resultAttr), |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1025 | *flags); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1026 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(*output); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1027 | outputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1028 | |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 1029 | ALOGV("%s returns output %d selectedDeviceId %d for port ID %d", |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1030 | __func__, *output, requestedPortId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1031 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1032 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1035 | audio_io_handle_t AudioPolicyManager::getOutputForDevices( |
| 1036 | const DeviceVector &devices, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1037 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1038 | audio_stream_type_t stream, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1039 | const audio_config_t *config, |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1040 | audio_output_flags_t *flags) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1041 | { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1042 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1043 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 1044 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1045 | // open a direct output if required by specified parameters |
| 1046 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 1047 | // and all common behaviors are driven by checking only the direct flag |
| 1048 | // this should normally be set appropriately in the policy configuration file |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1049 | if ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1050 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1051 | } |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1052 | if ((*flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1053 | *flags = (audio_output_flags_t)(*flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 1054 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1055 | // only allow deep buffering for music stream type |
| 1056 | if (stream != AUDIO_STREAM_MUSIC) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1057 | *flags = (audio_output_flags_t)(*flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1058 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1059 | *flags == AUDIO_OUTPUT_FLAG_NONE && |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 1060 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 1061 | // use DEEP_BUFFER as default output for music stream type |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1062 | *flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1063 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1064 | if (stream == AUDIO_STREAM_TTS) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1065 | *flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1066 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Nadav Bar | 2091949 | 2018-11-20 10:20:51 +0200 | [diff] [blame] | 1067 | audio_is_linear_pcm(config->format) && |
| 1068 | (*flags & AUDIO_OUTPUT_FLAG_INCALL_MUSIC) == 0) { |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1069 | *flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 1070 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1071 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 1072 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1073 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1074 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1075 | sp<IOProfile> profile; |
| 1076 | |
| 1077 | // 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] | 1078 | // and not explicitly requested |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1079 | if (((*flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1080 | audio_is_linear_pcm(config->format) && config->sample_rate <= SAMPLE_RATE_HZ_MAX && |
| 1081 | audio_channel_count_from_out_mask(config->channel_mask) <= 2) { |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1082 | goto non_direct_output; |
| 1083 | } |
| 1084 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1085 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 1086 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 1087 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1088 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 1089 | // This may prevent offloading in rare situations where effects are left active by apps |
| 1090 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1091 | |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1092 | if (((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 1093 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1094 | profile = getProfileForOutput(devices, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 1095 | config->sample_rate, |
| 1096 | config->format, |
| 1097 | config->channel_mask, |
| 1098 | (audio_output_flags_t)*flags, |
| 1099 | true /* directOnly */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1102 | if (profile != 0) { |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1103 | // exclusive outputs for MMAP and Offload are enforced by different session ids. |
| 1104 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1105 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1106 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 1107 | // reuse direct output if currently open by the same client |
| 1108 | // and configured with same parameters |
| 1109 | if ((config->sample_rate == desc->mSamplingRate) && |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1110 | (config->format == desc->mFormat) && |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1111 | (config->channel_mask == desc->mChannelMask) && |
| 1112 | (session == desc->mDirectClientSession)) { |
| 1113 | desc->mDirectOpenCount++; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1114 | ALOGI("%s reusing direct output %d for session %d", __func__, |
Andy Hung | c88b064 | 2018-04-27 15:42:35 -0700 | [diff] [blame] | 1115 | mOutputs.keyAt(i), session); |
| 1116 | return mOutputs.keyAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1117 | } |
| 1118 | } |
| 1119 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1120 | |
| 1121 | if (!profile->canOpenNewIo()) { |
| 1122 | goto non_direct_output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1123 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1124 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1125 | sp<SwAudioOutputDescriptor> outputDesc = |
| 1126 | new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1127 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1128 | String8 address = getFirstDeviceAddress(devices); |
Eric Laurent | 53b810e | 2017-12-10 17:25:10 -0800 | [diff] [blame] | 1129 | |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 1130 | // MSD patch may be using the only output stream that can service this request. Release |
| 1131 | // MSD patch to prioritize this request over any active output on MSD. |
| 1132 | AudioPatchCollection msdPatches = getMsdPatches(); |
| 1133 | for (size_t i = 0; i < msdPatches.size(); i++) { |
| 1134 | const auto& patch = msdPatches[i]; |
| 1135 | for (size_t j = 0; j < patch->mPatch.num_sinks; ++j) { |
| 1136 | const struct audio_port_config *sink = &patch->mPatch.sinks[j]; |
| 1137 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1138 | (sink->ext.device.type & devices.types()) != AUDIO_DEVICE_NONE && |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 1139 | (address.isEmpty() || strncmp(sink->ext.device.address, address.string(), |
| 1140 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
| 1141 | releaseAudioPatch(patch->mHandle, mUidCached); |
| 1142 | break; |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1147 | status = outputDesc->open(config, devices, stream, *flags, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1148 | |
| 1149 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1150 | if (status != NO_ERROR || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1151 | (config->sample_rate != 0 && config->sample_rate != outputDesc->mSamplingRate) || |
Andy Hung | 5659ed5 | 2018-04-30 10:31:26 -0700 | [diff] [blame] | 1152 | (config->format != AUDIO_FORMAT_DEFAULT && config->format != outputDesc->mFormat) || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1153 | (config->channel_mask != 0 && config->channel_mask != outputDesc->mChannelMask)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1154 | ALOGV("%s failed opening direct output: output %d sample rate %d %d," |
| 1155 | "format %d %d, channel mask %04x %04x", __func__, output, config->sample_rate, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1156 | outputDesc->mSamplingRate, config->format, outputDesc->mFormat, |
| 1157 | config->channel_mask, outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1158 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1159 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1160 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1161 | // 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] | 1162 | if (audio_is_linear_pcm(config->format) && |
| 1163 | config->sample_rate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1164 | goto non_direct_output; |
| 1165 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1166 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1167 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1168 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1169 | outputDesc->mDirectClientSession = session; |
| 1170 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1171 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1172 | mPreviousOutputs = mOutputs; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1173 | ALOGV("%s returns new direct output %d", __func__, output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1174 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1175 | return output; |
| 1176 | } |
| 1177 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1178 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1179 | |
| 1180 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1181 | // stamps are embedded in audio data |
Phil Burk | 2d05993 | 2018-02-15 15:55:11 -0800 | [diff] [blame] | 1182 | 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] | 1183 | return AUDIO_IO_HANDLE_NONE; |
| 1184 | } |
| 1185 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1186 | // ignoring channel mask due to downmix capability in mixer |
| 1187 | |
| 1188 | // open a non direct output |
| 1189 | |
| 1190 | // for non direct outputs, only PCM is supported |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1191 | if (audio_is_linear_pcm(config->format)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1192 | // get which output is suitable for the specified stream. The actual |
| 1193 | // routing change will happen when startOutput() will be called |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1194 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1195 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1196 | // 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] | 1197 | *flags = (audio_output_flags_t)(*flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1198 | output = selectOutput(outputs, *flags, config->format, |
| 1199 | config->channel_mask, config->sample_rate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1200 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1201 | ALOGW_IF((output == 0), "getOutputForDevices() could not find output for stream %d, " |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1202 | "sampling rate %d, format %#x, channels %#x, flags %#x", |
Nadav Bar | 766fb02 | 2018-01-07 12:18:03 +0200 | [diff] [blame] | 1203 | stream, config->sample_rate, config->format, config->channel_mask, *flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1204 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1205 | return output; |
| 1206 | } |
| 1207 | |
Mikhail Naganov | f02f367 | 2018-11-09 12:44:16 -0800 | [diff] [blame] | 1208 | sp<DeviceDescriptor> AudioPolicyManager::getMsdAudioInDevice() const { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1209 | auto msdInDevices = mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1210 | mAvailableInputDevices); |
| 1211 | return msdInDevices.isEmpty()? nullptr : msdInDevices.itemAt(0); |
| 1212 | } |
| 1213 | |
| 1214 | DeviceVector AudioPolicyManager::getMsdAudioOutDevices() const { |
| 1215 | return mHwModules.getAvailableDevicesFromModuleName(AUDIO_HARDWARE_MODULE_ID_MSD, |
| 1216 | mAvailableOutputDevices); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1217 | } |
| 1218 | |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1219 | const AudioPatchCollection AudioPolicyManager::getMsdPatches() const { |
| 1220 | AudioPatchCollection msdPatches; |
Mikhail Naganov | 8611235 | 2018-10-04 09:02:49 -0700 | [diff] [blame] | 1221 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
| 1222 | if (msdModule != 0) { |
| 1223 | for (size_t i = 0; i < mAudioPatches.size(); ++i) { |
| 1224 | sp<AudioPatch> patch = mAudioPatches.valueAt(i); |
| 1225 | for (size_t j = 0; j < patch->mPatch.num_sources; ++j) { |
| 1226 | const struct audio_port_config *source = &patch->mPatch.sources[j]; |
| 1227 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 1228 | source->ext.device.hw_module == msdModule->getHandle()) { |
| 1229 | msdPatches.addAudioPatch(patch->mHandle, patch); |
| 1230 | } |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | return msdPatches; |
| 1235 | } |
| 1236 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1237 | status_t AudioPolicyManager::getBestMsdAudioProfileFor(const sp<DeviceDescriptor> &outputDevice, |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1238 | bool hwAvSync, audio_port_config *sourceConfig, audio_port_config *sinkConfig) const |
| 1239 | { |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 1240 | sp<HwModule> msdModule = mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1241 | if (msdModule == nullptr) { |
| 1242 | ALOGE("%s() unable to get MSD module", __func__); |
| 1243 | return NO_INIT; |
| 1244 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1245 | sp<HwModule> deviceModule = mHwModules.getModuleForDevice(outputDevice, AUDIO_FORMAT_DEFAULT); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1246 | if (deviceModule == nullptr) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1247 | 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] | 1248 | return NO_INIT; |
| 1249 | } |
| 1250 | const InputProfileCollection &inputProfiles = msdModule->getInputProfiles(); |
| 1251 | if (inputProfiles.isEmpty()) { |
| 1252 | ALOGE("%s() no input profiles for MSD module", __func__); |
| 1253 | return NO_INIT; |
| 1254 | } |
| 1255 | const OutputProfileCollection &outputProfiles = deviceModule->getOutputProfiles(); |
| 1256 | if (outputProfiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1257 | 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] | 1258 | return NO_INIT; |
| 1259 | } |
| 1260 | AudioProfileVector msdProfiles; |
| 1261 | // Each IOProfile represents a MixPort from audio_policy_configuration.xml |
| 1262 | for (const auto &inProfile : inputProfiles) { |
| 1263 | if (hwAvSync == ((inProfile->getFlags() & AUDIO_INPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1264 | msdProfiles.appendVector(inProfile->getAudioProfiles()); |
| 1265 | } |
| 1266 | } |
| 1267 | AudioProfileVector deviceProfiles; |
| 1268 | for (const auto &outProfile : outputProfiles) { |
| 1269 | if (hwAvSync == ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0)) { |
| 1270 | deviceProfiles.appendVector(outProfile->getAudioProfiles()); |
| 1271 | } |
| 1272 | } |
| 1273 | struct audio_config_base bestSinkConfig; |
| 1274 | status_t result = msdProfiles.findBestMatchingOutputConfig(deviceProfiles, |
| 1275 | compressedFormatsOrder, surroundChannelMasksOrder, true /*preferHigherSamplingRates*/, |
| 1276 | &bestSinkConfig); |
| 1277 | if (result != NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1278 | ALOGD("%s() no matching profiles found for device: %s, hwAvSync: %d", |
| 1279 | __func__, outputDevice->toString().c_str(), hwAvSync); |
Greg Kaiser | 8328965 | 2018-07-30 06:13:57 -0700 | [diff] [blame] | 1280 | return result; |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1281 | } |
| 1282 | sinkConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1283 | sinkConfig->channel_mask = bestSinkConfig.channel_mask; |
| 1284 | sinkConfig->format = bestSinkConfig.format; |
| 1285 | // For encoded streams force direct flag to prevent downstream mixing. |
| 1286 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1287 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_DIRECT); |
| 1288 | sourceConfig->sample_rate = bestSinkConfig.sample_rate; |
| 1289 | // Specify exact channel mask to prevent guessing by bit count in PatchPanel. |
| 1290 | sourceConfig->channel_mask = audio_channel_mask_out_to_in(bestSinkConfig.channel_mask); |
| 1291 | sourceConfig->format = bestSinkConfig.format; |
| 1292 | // Copy input stream directly without any processing (e.g. resampling). |
| 1293 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1294 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_DIRECT); |
| 1295 | if (hwAvSync) { |
| 1296 | sinkConfig->flags.output = static_cast<audio_output_flags_t>( |
| 1297 | sinkConfig->flags.output | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 1298 | sourceConfig->flags.input = static_cast<audio_input_flags_t>( |
| 1299 | sourceConfig->flags.input | AUDIO_INPUT_FLAG_HW_AV_SYNC); |
| 1300 | } |
| 1301 | const unsigned int config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE | |
| 1302 | AUDIO_PORT_CONFIG_CHANNEL_MASK | AUDIO_PORT_CONFIG_FORMAT | AUDIO_PORT_CONFIG_FLAGS; |
| 1303 | sinkConfig->config_mask |= config_mask; |
| 1304 | sourceConfig->config_mask |= config_mask; |
| 1305 | return NO_ERROR; |
| 1306 | } |
| 1307 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1308 | PatchBuilder AudioPolicyManager::buildMsdPatch(const sp<DeviceDescriptor> &outputDevice) const |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1309 | { |
| 1310 | PatchBuilder patchBuilder; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1311 | patchBuilder.addSource(getMsdAudioInDevice()).addSink(outputDevice); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1312 | audio_port_config sourceConfig = patchBuilder.patch()->sources[0]; |
| 1313 | audio_port_config sinkConfig = patchBuilder.patch()->sinks[0]; |
| 1314 | // TODO: Figure out whether MSD module has HW_AV_SYNC flag set in the AP config file. |
| 1315 | // For now, we just forcefully try with HwAvSync first. |
| 1316 | status_t res = getBestMsdAudioProfileFor(outputDevice, true /*hwAvSync*/, |
| 1317 | &sourceConfig, &sinkConfig) == NO_ERROR ? NO_ERROR : |
| 1318 | getBestMsdAudioProfileFor( |
| 1319 | outputDevice, false /*hwAvSync*/, &sourceConfig, &sinkConfig); |
| 1320 | if (res == NO_ERROR) { |
| 1321 | // Found a matching profile for encoded audio. Re-create PatchBuilder with this config. |
| 1322 | return (PatchBuilder()).addSource(sourceConfig).addSink(sinkConfig); |
| 1323 | } |
| 1324 | ALOGV("%s() no matching profile found. Fall through to default PCM patch" |
| 1325 | " supporting PCM format conversion.", __func__); |
| 1326 | return patchBuilder; |
| 1327 | } |
| 1328 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1329 | status_t AudioPolicyManager::setMsdPatch(const sp<DeviceDescriptor> &outputDevice) { |
| 1330 | sp<DeviceDescriptor> device = outputDevice; |
| 1331 | if (device == nullptr) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1332 | // Use media strategy for unspecified output device. This should only |
| 1333 | // occur on checkForDeviceAndOutputChanges(). Device connection events may |
| 1334 | // therefore invalidate explicit routing requests. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1335 | DeviceVector devices = getDevicesForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 1336 | LOG_ALWAYS_FATAL_IF(devices.isEmpty(), "no outpudevice to set Msd Patch"); |
| 1337 | device = devices.itemAt(0); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1338 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1339 | ALOGV("%s() for device %s", __func__, device->toString().c_str()); |
| 1340 | PatchBuilder patchBuilder = buildMsdPatch(device); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1341 | const struct audio_patch* patch = patchBuilder.patch(); |
| 1342 | const AudioPatchCollection msdPatches = getMsdPatches(); |
| 1343 | if (!msdPatches.isEmpty()) { |
| 1344 | LOG_ALWAYS_FATAL_IF(msdPatches.size() > 1, |
| 1345 | "The current MSD prototype only supports one output patch"); |
| 1346 | sp<AudioPatch> currentPatch = msdPatches.valueAt(0); |
| 1347 | if (audio_patches_are_equal(¤tPatch->mPatch, patch)) { |
| 1348 | return NO_ERROR; |
| 1349 | } |
| 1350 | releaseAudioPatch(currentPatch->mHandle, mUidCached); |
| 1351 | } |
| 1352 | status_t status = installPatch(__func__, -1 /*index*/, nullptr /*patchHandle*/, |
| 1353 | patch, 0 /*delayMs*/, mUidCached, nullptr /*patchDescPtr*/); |
| 1354 | ALOGE_IF(status != NO_ERROR, "%s() error %d creating MSD audio patch", __func__, status); |
| 1355 | 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] | 1356 | "device:%s (format:%#x channels:%#x samplerate:%d)", __func__, |
| 1357 | device->toString().c_str(), patch->sources[0].format, |
| 1358 | patch->sources[0].channel_mask, patch->sources[0].sample_rate); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 1359 | return status; |
| 1360 | } |
| 1361 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1362 | 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] | 1363 | audio_output_flags_t flags, |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1364 | audio_format_t format, |
| 1365 | audio_channel_mask_t channelMask, |
| 1366 | uint32_t samplingRate) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1367 | { |
| 1368 | // 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] | 1369 | // devices (the list was previously build by getOutputsForDevices()). |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1370 | // The priority is as follows: |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1371 | // 1: the output supporting haptic playback when requesting haptic playback |
| 1372 | // 2: the output with the highest number of requested policy flags |
| 1373 | // 3: the output with the bit depth the closest to the requested one |
| 1374 | // 4: the primary output |
| 1375 | // 5: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1376 | |
| 1377 | if (outputs.size() == 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1378 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1379 | } |
| 1380 | if (outputs.size() == 1) { |
| 1381 | return outputs[0]; |
| 1382 | } |
| 1383 | |
| 1384 | int maxCommonFlags = 0; |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1385 | const size_t hapticChannelCount = audio_channel_count_from_out_mask( |
| 1386 | channelMask & AUDIO_CHANNEL_HAPTIC_ALL); |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1387 | audio_io_handle_t outputForFlags = AUDIO_IO_HANDLE_NONE; |
| 1388 | audio_io_handle_t outputForPrimary = AUDIO_IO_HANDLE_NONE; |
| 1389 | audio_io_handle_t outputForFormat = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1390 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1391 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1392 | |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1393 | // Flags which must be present on both the request and the selected output |
| 1394 | static const audio_output_flags_t kMandatedFlags = (audio_output_flags_t) |
| 1395 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_MMAP_NOIRQ); |
| 1396 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1397 | for (audio_io_handle_t output : outputs) { |
| 1398 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1399 | if (!outputDesc->isDuplicated()) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1400 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1401 | continue; |
| 1402 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1403 | // If haptic channel is specified, use the haptic output if present. |
| 1404 | // When using haptic output, same audio format and sample rate are required. |
| 1405 | if (hapticChannelCount > 0) { |
| 1406 | // If haptic channel is specified, use the first output that |
| 1407 | // support haptic playback. |
| 1408 | if (audio_channel_count_from_out_mask( |
| 1409 | outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) >= hapticChannelCount |
| 1410 | && format == outputDesc->mFormat |
| 1411 | && samplingRate == outputDesc->mSamplingRate) { |
| 1412 | return output; |
| 1413 | } |
| 1414 | } else { |
| 1415 | // When haptic channel is not specified, skip haptic output. |
| 1416 | if (outputDesc->mChannelMask & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 1417 | continue; |
| 1418 | } |
| 1419 | } |
Eric Laurent | e78b676 | 2018-12-19 16:29:01 -0800 | [diff] [blame] | 1420 | if ((kMandatedFlags & flags) != |
| 1421 | (kMandatedFlags & outputDesc->mProfile->getFlags())) { |
| 1422 | continue; |
| 1423 | } |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 1424 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1425 | // if a valid format is specified, skip output if not compatible |
| 1426 | if (format != AUDIO_FORMAT_INVALID) { |
chenhg | 1794d71 | 2018-10-09 15:20:37 -0700 | [diff] [blame] | 1427 | if (!audio_is_linear_pcm(format)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1428 | continue; |
| 1429 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1430 | if (AudioPort::isBetterFormatMatch( |
| 1431 | outputDesc->mFormat, bestFormat, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1432 | outputForFormat = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1433 | bestFormat = outputDesc->mFormat; |
| 1434 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1437 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1438 | if (commonFlags >= maxCommonFlags) { |
| 1439 | if (commonFlags == maxCommonFlags) { |
Andy Hung | c990152 | 2017-11-10 20:07:54 -0800 | [diff] [blame] | 1440 | if (format != AUDIO_FORMAT_INVALID |
| 1441 | && AudioPort::isBetterFormatMatch( |
| 1442 | outputDesc->mFormat, bestFormatForFlags, format)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1443 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1444 | bestFormatForFlags = outputDesc->mFormat; |
| 1445 | } |
| 1446 | } else { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1447 | outputForFlags = output; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1448 | maxCommonFlags = commonFlags; |
| 1449 | bestFormatForFlags = outputDesc->mFormat; |
| 1450 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1451 | ALOGV("selectOutput() commonFlags for output %d, %04x", output, commonFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1452 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1453 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1454 | outputForPrimary = output; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | } |
| 1458 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1459 | if (outputForFlags != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1460 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1461 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1462 | if (outputForFormat != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1463 | return outputForFormat; |
| 1464 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 1465 | if (outputForPrimary != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1466 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | return outputs[0]; |
| 1470 | } |
| 1471 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1472 | status_t AudioPolicyManager::startOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1473 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1474 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1475 | |
| 1476 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1477 | if (outputDesc == 0) { |
| 1478 | ALOGW("startOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1479 | return BAD_VALUE; |
| 1480 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1481 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1482 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1483 | ALOGV("startOutput() output %d, stream %d, session %d", |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1484 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1485 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1486 | status_t status = outputDesc->start(); |
| 1487 | if (status != NO_ERROR) { |
| 1488 | return status; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1489 | } |
| 1490 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1491 | uint32_t delayMs; |
| 1492 | status = startSource(outputDesc, client, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1493 | |
| 1494 | if (status != NO_ERROR) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1495 | outputDesc->stop(); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1496 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1497 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1498 | if (delayMs != 0) { |
| 1499 | usleep(delayMs * 1000); |
| 1500 | } |
| 1501 | |
| 1502 | return status; |
| 1503 | } |
| 1504 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1505 | status_t AudioPolicyManager::startSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1506 | const sp<TrackClientDescriptor>& client, |
| 1507 | uint32_t *delayMs) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1508 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1509 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1510 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1511 | |
| 1512 | *delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1513 | audio_stream_type_t stream = client->stream(); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1514 | if (stream == AUDIO_STREAM_TTS) { |
| 1515 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1516 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1517 | return INVALID_OPERATION; |
| 1518 | } else { |
| 1519 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1520 | } |
| 1521 | } else { |
| 1522 | // some playback other than beacon starts |
| 1523 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1524 | } |
| 1525 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1526 | // 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] | 1527 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1528 | bool force = !outputDesc->isActive() && |
| 1529 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1530 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1531 | DeviceVector devices; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1532 | AudioMix *policyMix = NULL; |
| 1533 | const char *address = NULL; |
| 1534 | if (outputDesc->mPolicyMix != NULL) { |
| 1535 | policyMix = outputDesc->mPolicyMix; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1536 | audio_devices_t newDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1537 | address = policyMix->mDeviceAddress.string(); |
| 1538 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1539 | newDeviceType = policyMix->mDeviceType; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1540 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1541 | newDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1542 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1543 | devices.add(mAvailableOutputDevices.getDevice(newDeviceType, |
| 1544 | String8(address), AUDIO_FORMAT_DEFAULT)); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1545 | } |
| 1546 | |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1547 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1548 | // It covers a common case when there is no materially active audio |
| 1549 | // and muting would result in unnecessary delay and dropped audio. |
| 1550 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1551 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1552 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1553 | // increment usage count for this stream on the requested output: |
| 1554 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1555 | // 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] | 1556 | outputDesc->setClientActive(client, true); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1557 | |
| 1558 | if (client->hasPreferredDevice(true)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1559 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
| 1560 | if (devices != outputDesc->devices()) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1561 | checkStrategyRoute(getStrategy(stream), outputDesc->mIoHandle); |
| 1562 | } |
| 1563 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1564 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1565 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1566 | selectOutputForMusicEffects(); |
| 1567 | } |
| 1568 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1569 | if (outputDesc->streamActiveCount(stream) == 1 || !devices.isEmpty()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1570 | // starting an output being rerouted? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1571 | if (devices.isEmpty()) { |
| 1572 | devices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1573 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1574 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1575 | routing_strategy strategy = getStrategy(stream); |
| 1576 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1577 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1578 | (beaconMuteLatency > 0); |
| 1579 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1581 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1582 | if (desc != outputDesc) { |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1583 | // An output has a shared device if |
| 1584 | // - managed by the same hw module |
| 1585 | // - supports the currently selected device |
| 1586 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1587 | && (!desc->filterSupportedDevices(devices).isEmpty()); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1588 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1589 | // force a device change if any other output is: |
| 1590 | // - managed by the same hw module |
Jean-Michel Trivi | 4a5b481 | 2018-02-08 17:22:32 +0000 | [diff] [blame] | 1591 | // - supports currently selected device |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1592 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1593 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1594 | // 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] | 1595 | // change the device currently selected by the other output. |
| 1596 | if (sharedDevice && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1597 | desc->devices() != devices && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1598 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1599 | force = true; |
| 1600 | } |
| 1601 | // 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] | 1602 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1603 | // event occurred for beacon |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1604 | const uint32_t latencyMs = desc->latency(); |
| 1605 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1606 | |
| 1607 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1608 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1609 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1610 | |
| 1611 | // Require mute check if another output is on a shared device |
| 1612 | // and currently active to have proper drain and avoid pops. |
| 1613 | // Note restoring AudioTracks onto this output needs to invoke |
| 1614 | // a volume ramp if there is no mute. |
| 1615 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1616 | } |
| 1617 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1618 | |
| 1619 | const uint32_t muteWaitMs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1620 | setOutputDevices(outputDesc, devices, force, 0, NULL, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1621 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1622 | // apply volume rules for current stream and device if necessary |
| 1623 | checkAndSetVolume(stream, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1624 | mVolumeCurves->getVolumeIndex(stream, outputDesc->devices().types()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1625 | outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1626 | outputDesc->devices().types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1627 | |
| 1628 | // update the outputs if starting an output with a stream that can affect notification |
| 1629 | // routing |
| 1630 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1631 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1632 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1633 | if (strategy == STRATEGY_SONIFICATION) { |
| 1634 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1635 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1636 | |
| 1637 | if (waitMs > muteWaitMs) { |
| 1638 | *delayMs = waitMs - muteWaitMs; |
| 1639 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 1640 | |
| 1641 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1642 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1643 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1644 | // change occurs after the MixerThread starts and causes a stream volume |
| 1645 | // glitch. |
| 1646 | // |
| 1647 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1648 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1649 | |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1650 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1651 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1652 | setStrategyMute(STRATEGY_SONIFICATION, true, outputDesc); |
| 1653 | } |
| 1654 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1655 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1656 | // of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1657 | if (audio_is_remote_submix_device(devices.types()) && policyMix != NULL && |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1658 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1659 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1660 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1661 | address, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1662 | "remote-submix", |
| 1663 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1666 | return NO_ERROR; |
| 1667 | } |
| 1668 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1669 | status_t AudioPolicyManager::stopOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1670 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1671 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1672 | |
| 1673 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1674 | if (outputDesc == 0) { |
| 1675 | ALOGW("stopOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1676 | return BAD_VALUE; |
| 1677 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1678 | sp<TrackClientDescriptor> client = outputDesc->getClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1679 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1680 | ALOGV("stopOutput() output %d, stream %d, session %d", |
| 1681 | outputDesc->mIoHandle, client->stream(), client->session()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1682 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1683 | status_t status = stopSource(outputDesc, client); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1684 | |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 1685 | if (status == NO_ERROR ) { |
| 1686 | outputDesc->stop(); |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 1687 | } |
| 1688 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1689 | } |
| 1690 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1691 | status_t AudioPolicyManager::stopSource(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 1692 | const sp<TrackClientDescriptor>& client) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1693 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1694 | // always handle stream stop, check which stream type is stopping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1695 | audio_stream_type_t stream = client->stream(); |
| 1696 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1697 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1698 | |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1699 | if (outputDesc->streamActiveCount(stream) > 0) { |
| 1700 | if (outputDesc->streamActiveCount(stream) == 1) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1701 | // Automatically disable the remote submix input when output is stopped on a |
| 1702 | // re routing mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1703 | if (audio_is_remote_submix_device(outputDesc->devices().types()) && |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1704 | outputDesc->mPolicyMix != NULL && |
| 1705 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1706 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1707 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1708 | outputDesc->mPolicyMix->mDeviceAddress, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1709 | "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1710 | } |
| 1711 | } |
| 1712 | bool forceDeviceUpdate = false; |
| 1713 | if (client->hasPreferredDevice(true)) { |
| 1714 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1715 | forceDeviceUpdate = true; |
| 1716 | } |
| 1717 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1718 | // decrement usage count of this stream on the output |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1719 | outputDesc->setClientActive(client, false); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1720 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1721 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 592dd7b | 2018-08-05 18:58:48 -0700 | [diff] [blame] | 1722 | if (outputDesc->streamActiveCount(stream) == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1723 | outputDesc->mStopTime[stream] = systemTime(); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1724 | DeviceVector newDevices = getNewOutputDevices(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1725 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1726 | // the track stop() command is received and at that time the audio track buffer can |
| 1727 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1728 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1729 | // audio path (audio DSP, CODEC ...) |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1730 | setOutputDevices(outputDesc, newDevices, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1731 | |
| 1732 | // force restoring the device selection on other active outputs if it differs from the |
| 1733 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1734 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1735 | for (size_t i = 0; i < mOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1736 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1737 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1738 | desc->isActive() && |
| 1739 | outputDesc->sharesHwModuleWith(desc) && |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1740 | (newDevices != desc->devices())) { |
| 1741 | DeviceVector newDevices2 = getNewOutputDevices(desc, false /*fromCache*/); |
| 1742 | bool force = desc->devices() != newDevices2; |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 1743 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1744 | setOutputDevices(desc, newDevices2, force, delayMs); |
| 1745 | |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1746 | // re-apply device specific volume if not done by setOutputDevice() |
| 1747 | if (!force) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1748 | applyStreamVolumes(desc, newDevices2.types(), delayMs); |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1749 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1750 | } |
| 1751 | } |
| 1752 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1753 | handleNotificationRoutingForStream(stream); |
| 1754 | } |
Tomoharu Kasahara | b62d78b | 2018-01-18 20:55:02 +0900 | [diff] [blame] | 1755 | |
| 1756 | if (stream == AUDIO_STREAM_ENFORCED_AUDIBLE && |
| 1757 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
| 1758 | setStrategyMute(STRATEGY_SONIFICATION, false, outputDesc); |
| 1759 | } |
| 1760 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1761 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1762 | selectOutputForMusicEffects(); |
| 1763 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1764 | return NO_ERROR; |
| 1765 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1766 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1767 | return INVALID_OPERATION; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1771 | void AudioPolicyManager::releaseOutput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1772 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1773 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 1774 | |
| 1775 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputForClient(portId); |
| 1776 | if (outputDesc == 0) { |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1777 | // If an output descriptor is closed due to a device routing change, |
| 1778 | // then there are race conditions with releaseOutput from tracks |
| 1779 | // that may be destroyed (with no PlaybackThread) or a PlaybackThread |
| 1780 | // destroyed shortly thereafter. |
| 1781 | // |
| 1782 | // Here we just log a warning, instead of a fatal error. |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1783 | ALOGW("releaseOutput() no output for client %d", portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1784 | return; |
| 1785 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1786 | |
| 1787 | ALOGV("releaseOutput() %d", outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1788 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1789 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1790 | if (outputDesc->mDirectOpenCount <= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1791 | ALOGW("releaseOutput() invalid open count %d for output %d", |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1792 | outputDesc->mDirectOpenCount, outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1793 | return; |
| 1794 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1795 | if (--outputDesc->mDirectOpenCount == 0) { |
| 1796 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1797 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1798 | } |
| 1799 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1800 | // stopOutput() needs to be successfully called before releaseOutput() |
| 1801 | // otherwise there may be inaccurate stream reference counts. |
| 1802 | // This is checked in outputDesc->removeClient below. |
| 1803 | outputDesc->removeClient(portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1804 | } |
| 1805 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1806 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1807 | audio_io_handle_t *input, |
| 1808 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1809 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1810 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1811 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1812 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1813 | input_type_t *inputType, |
| 1814 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1815 | { |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 1816 | ALOGV("getInputForAttr() source %d, sampling rate %d, format %#x, channel mask %#x," |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1817 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1818 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1819 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1820 | status_t status = NO_ERROR; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1821 | audio_source_t halInputSource; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1822 | audio_attributes_t attributes = *attr; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1823 | AudioMix *policyMix = NULL; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1824 | sp<DeviceDescriptor> device; |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1825 | sp<AudioInputDescriptor> inputDesc; |
| 1826 | sp<RecordClientDescriptor> clientDesc; |
| 1827 | audio_port_handle_t requestedDeviceId = *selectedDeviceId; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1828 | bool isSoundTrigger; |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1829 | |
| 1830 | // The supplied portId must be AUDIO_PORT_HANDLE_NONE |
| 1831 | if (*portId != AUDIO_PORT_HANDLE_NONE) { |
| 1832 | return INVALID_OPERATION; |
| 1833 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1834 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1835 | if (attr->source == AUDIO_SOURCE_DEFAULT) { |
| 1836 | attributes.source = AUDIO_SOURCE_MIC; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1837 | } |
| 1838 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1839 | // Explicit routing? |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1840 | sp<DeviceDescriptor> explicitRoutingDevice = |
| 1841 | mAvailableInputDevices.getDeviceFromId(*selectedDeviceId); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1842 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1843 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1844 | // possible |
| 1845 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1846 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1847 | ssize_t index = mInputs.indexOfKey(*input); |
| 1848 | if (index < 0) { |
| 1849 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1850 | status = BAD_VALUE; |
| 1851 | goto error; |
| 1852 | } |
| 1853 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1854 | RecordClientVector clients = inputDesc->getClientsForSession(session); |
| 1855 | if (clients.size() == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1856 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1857 | status = BAD_VALUE; |
| 1858 | goto error; |
| 1859 | } |
| 1860 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1861 | // 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] | 1862 | // corresponds to a new client and is only permitted from the same UID. |
| 1863 | // 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] | 1864 | if (clients.size() > 1) { |
| 1865 | for (const auto& client : clients) { |
| 1866 | // The client map is ordered by key values (portId) and portIds are allocated |
| 1867 | // incrementaly. So the first client in this list is the one opened by audio flinger |
| 1868 | // when the mmap stream is created and should be ignored as it does not correspond |
| 1869 | // to an actual client |
| 1870 | if (client == *clients.cbegin()) { |
| 1871 | continue; |
| 1872 | } |
| 1873 | if (uid != client->uid() && !client->isSilenced()) { |
| 1874 | ALOGW("getInputForAttr() bad uid %d for client %d uid %d", |
| 1875 | uid, client->portId(), client->uid()); |
| 1876 | status = INVALID_OPERATION; |
| 1877 | goto error; |
| 1878 | } |
Eric Laurent | 331679c | 2018-04-16 17:03:16 -0700 | [diff] [blame] | 1879 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1880 | } |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1881 | *inputType = API_INPUT_LEGACY; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1882 | device = inputDesc->getDevice(); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1883 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1884 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1885 | goto exit; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | *input = AUDIO_IO_HANDLE_NONE; |
| 1889 | *inputType = API_INPUT_INVALID; |
| 1890 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1891 | halInputSource = attributes.source; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1892 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1893 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 1894 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 1895 | status = mPolicyMixes.getInputMixForAttr(attributes, &policyMix); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1896 | if (status != NO_ERROR) { |
| 1897 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1898 | } |
| 1899 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1900 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1901 | String8(attr->tags + strlen("addr=")), |
| 1902 | AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1903 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1904 | if (explicitRoutingDevice != nullptr) { |
| 1905 | device = explicitRoutingDevice; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1906 | } else { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1907 | device = getDeviceAndMixForAttributes(attributes, &policyMix); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 1908 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1909 | if (device == nullptr) { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1910 | ALOGW("getInputForAttr() could not find device for source %d", attributes.source); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1911 | status = BAD_VALUE; |
| 1912 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1913 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1914 | if (policyMix != nullptr) { |
| 1915 | ALOG_ASSERT(policyMix->mMixType == MIX_TYPE_RECORDERS, "Invalid Mix Type"); |
| 1916 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1917 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1918 | // know about it and is therefore considered "legacy" |
| 1919 | *inputType = API_INPUT_LEGACY; |
| 1920 | } else if (audio_is_remote_submix_device(device->type())) { |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 1921 | device = mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8("0"), |
| 1922 | AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1923 | *inputType = API_INPUT_MIX_CAPTURE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1924 | } else if (device->type() == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1925 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1926 | } else { |
| 1927 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1928 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1929 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1930 | } |
| 1931 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1932 | *input = getInputForDevice(device, session, attributes.source, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1933 | config, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1934 | policyMix); |
| 1935 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1936 | status = INVALID_OPERATION; |
| 1937 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1938 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1939 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1940 | exit: |
| 1941 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1942 | *selectedDeviceId = mAvailableInputDevices.contains(device) ? |
| 1943 | device->getId() : AUDIO_PORT_HANDLE_NONE; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1944 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1945 | isSoundTrigger = attributes.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 1946 | mSoundTriggerSessions.indexOfKey(session) > 0; |
| 1947 | *portId = AudioPort::getNextUniqueId(); |
| 1948 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1949 | clientDesc = new RecordClientDescriptor(*portId, uid, session, *attr, *config, |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 1950 | requestedDeviceId, attributes.source, flags, |
| 1951 | isSoundTrigger); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1952 | inputDesc = mInputs.valueFor(*input); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 1953 | inputDesc->addClient(clientDesc); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 1954 | |
| 1955 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d for port ID %d", |
| 1956 | *input, *inputType, *selectedDeviceId, *portId); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1957 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1958 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1959 | |
| 1960 | error: |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1961 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 1965 | audio_io_handle_t AudioPolicyManager::getInputForDevice(const sp<DeviceDescriptor> &device, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1966 | audio_session_t session, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1967 | audio_source_t inputSource, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1968 | const audio_config_base_t *config, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1969 | audio_input_flags_t flags, |
| 1970 | AudioMix *policyMix) |
| 1971 | { |
| 1972 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1973 | audio_source_t halInputSource = inputSource; |
| 1974 | bool isSoundTrigger = false; |
| 1975 | |
| 1976 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1977 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1978 | if (index >= 0) { |
| 1979 | input = mSoundTriggerSessions.valueFor(session); |
| 1980 | isSoundTrigger = true; |
| 1981 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1982 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1983 | } else { |
| 1984 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1985 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1986 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1987 | audio_is_linear_pcm(config->format)) { |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1988 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1991 | // find a compatible input profile (not necessarily identical in parameters) |
| 1992 | sp<IOProfile> profile; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1993 | // sampling rate and flags may be updated by getInputProfile |
| 1994 | uint32_t profileSamplingRate = (config->sample_rate == 0) ? |
| 1995 | SAMPLE_RATE_HZ_DEFAULT : config->sample_rate; |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 1996 | audio_format_t profileFormat; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 1997 | audio_channel_mask_t profileChannelMask = config->channel_mask; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1998 | audio_input_flags_t profileFlags = flags; |
| 1999 | for (;;) { |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 2000 | 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] | 2001 | profile = getInputProfile(device, profileSamplingRate, profileFormat, profileChannelMask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2002 | profileFlags); |
| 2003 | if (profile != 0) { |
| 2004 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 2005 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 2006 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2007 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 2008 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 2009 | } else { // fail |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2010 | ALOGW("%s could not find profile for device %s, sampling rate %u, format %#x, " |
| 2011 | "channel mask 0x%X, flags %#x", __func__, device->toString().c_str(), |
| 2012 | config->sample_rate, config->format, config->channel_mask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2013 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 2014 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2015 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2016 | // Pick input sampling rate if not specified by client |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2017 | uint32_t samplingRate = config->sample_rate; |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 2018 | if (samplingRate == 0) { |
| 2019 | samplingRate = profileSamplingRate; |
| 2020 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2021 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2022 | if (profile->getModuleHandle() == 0) { |
| 2023 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2024 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2027 | if (!profile->canOpenNewIo()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2028 | for (size_t i = 0; i < mInputs.size(); ) { |
| 2029 | sp <AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 2030 | if (desc->mProfile != profile) { |
| 2031 | continue; |
| 2032 | } |
| 2033 | // if sound trigger, reuse input if used by other sound trigger on same session |
| 2034 | // else |
| 2035 | // reuse input if active client app is not in IDLE state |
| 2036 | // |
| 2037 | RecordClientVector clients = desc->clientsList(); |
| 2038 | bool doClose = false; |
| 2039 | for (const auto& client : clients) { |
| 2040 | if (isSoundTrigger != client->isSoundTrigger()) { |
| 2041 | continue; |
| 2042 | } |
| 2043 | if (client->isSoundTrigger()) { |
| 2044 | if (session == client->session()) { |
| 2045 | return desc->mIoHandle; |
| 2046 | } |
| 2047 | continue; |
| 2048 | } |
| 2049 | if (client->active() && client->appState() != APP_STATE_IDLE) { |
| 2050 | return desc->mIoHandle; |
| 2051 | } |
| 2052 | doClose = true; |
| 2053 | } |
| 2054 | if (doClose) { |
| 2055 | closeInput(desc->mIoHandle); |
| 2056 | } else { |
| 2057 | i++; |
| 2058 | } |
| 2059 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2060 | } |
| 2061 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2062 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2063 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2064 | audio_config_t lConfig = AUDIO_CONFIG_INITIALIZER; |
| 2065 | lConfig.sample_rate = profileSamplingRate; |
| 2066 | lConfig.channel_mask = profileChannelMask; |
| 2067 | lConfig.format = profileFormat; |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 2068 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2069 | status_t status = inputDesc->open(&lConfig, device, halInputSource, profileFlags, &input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2070 | |
| 2071 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2072 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2073 | (profileSamplingRate != lConfig.sample_rate) || |
| 2074 | !audio_formats_match(profileFormat, lConfig.format) || |
| 2075 | (profileChannelMask != lConfig.channel_mask)) { |
| 2076 | ALOGW("getInputForAttr() failed opening input: sampling rate %d" |
Glenn Kasten | 49f36ba | 2017-12-06 13:02:02 -0800 | [diff] [blame] | 2077 | ", format %#x, channel mask %#x", |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2078 | profileSamplingRate, profileFormat, profileChannelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2079 | if (input != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2080 | inputDesc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 2081 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2082 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2085 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2086 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2087 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2088 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2089 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2090 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2093 | status_t AudioPolicyManager::startInput(audio_port_handle_t portId) |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 2094 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2095 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2096 | |
| 2097 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2098 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2099 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2100 | return BAD_VALUE; |
| 2101 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2102 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2103 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2104 | if (client->active()) { |
| 2105 | ALOGW("%s input %d client %d already started", __FUNCTION__, input, client->portId()); |
| 2106 | return INVALID_OPERATION; |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2109 | audio_session_t session = client->session(); |
| 2110 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2111 | ALOGV("%s input:%d, session:%d)", __FUNCTION__, input, session); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2112 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2113 | Vector<sp<AudioInputDescriptor>> activeInputs = mInputs.getActiveInputs(); |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2114 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2115 | status_t status = inputDesc->start(); |
| 2116 | if (status != NO_ERROR) { |
| 2117 | return status; |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 2118 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2119 | |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 2120 | // increment activity count before calling getNewInputDevice() below as only active sessions |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2121 | // are considered for device selection |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2122 | inputDesc->setClientActive(client, true); |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 2123 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2124 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 2125 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2126 | sp<DeviceDescriptor> device = getNewInputDevice(inputDesc); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2127 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2128 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2129 | if (inputDesc->activeCount() == 1) { |
| 2130 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2131 | if ((inputDesc->mPolicyMix != NULL) |
| 2132 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2133 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2134 | MIX_STATE_MIXING); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 2135 | } |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 2136 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2137 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2138 | if (primaryInputDevices.contains(device) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2139 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
| 2140 | SoundTrigger::setCaptureState(true); |
| 2141 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2142 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2143 | // automatically enable the remote submix output when input is started if not |
| 2144 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2145 | // 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] | 2146 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2147 | String8 address = String8(""); |
| 2148 | if (inputDesc->mPolicyMix == NULL) { |
| 2149 | address = String8("0"); |
| 2150 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2151 | address = inputDesc->mPolicyMix->mDeviceAddress; |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2152 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2153 | if (address != "") { |
| 2154 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2155 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2156 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2157 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2158 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2161 | ALOGV("%s input %d source = %d exit", __FUNCTION__, input, client->source()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2162 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2163 | return NO_ERROR; |
| 2164 | } |
| 2165 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2166 | status_t AudioPolicyManager::stopInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2167 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2168 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2169 | |
| 2170 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2171 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2172 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2173 | return BAD_VALUE; |
| 2174 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2175 | audio_io_handle_t input = inputDesc->mIoHandle; |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2176 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2177 | if (!client->active()) { |
| 2178 | ALOGW("%s input %d client %d already stopped", __FUNCTION__, input, client->portId()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2179 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2182 | inputDesc->setClientActive(client, false); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2183 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2184 | inputDesc->stop(); |
| 2185 | if (inputDesc->isActive()) { |
| 2186 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2187 | } else { |
| 2188 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2189 | if ((inputDesc->mPolicyMix != NULL) |
| 2190 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2191 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2192 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2193 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2194 | |
| 2195 | // automatically disable the remote submix output when input is stopped if not |
| 2196 | // used by a policy mix of type MIX_TYPE_RECORDERS |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2197 | if (audio_is_remote_submix_device(inputDesc->getDeviceType())) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2198 | String8 address = String8(""); |
| 2199 | if (inputDesc->mPolicyMix == NULL) { |
| 2200 | address = String8("0"); |
| 2201 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2202 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2203 | } |
| 2204 | if (address != "") { |
| 2205 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2206 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2207 | address, "remote-submix", AUDIO_FORMAT_DEFAULT); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2208 | } |
| 2209 | } |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2210 | resetInputDevice(input); |
| 2211 | |
| 2212 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2213 | // primary HW module |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2214 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 2215 | if (primaryInputDevices.contains(inputDesc->getDevice()) && |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2216 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 2217 | SoundTrigger::setCaptureState(false); |
| 2218 | } |
| 2219 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2220 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2221 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2224 | void AudioPolicyManager::releaseInput(audio_port_handle_t portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2225 | { |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2226 | ALOGV("%s portId %d", __FUNCTION__, portId); |
| 2227 | |
| 2228 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputForClient(portId); |
| 2229 | if (inputDesc == 0) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2230 | ALOGW("%s no input for client %d", __FUNCTION__, portId); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2231 | return; |
| 2232 | } |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2233 | sp<RecordClientDescriptor> client = inputDesc->getClient(portId); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2234 | audio_io_handle_t input = inputDesc->mIoHandle; |
| 2235 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2236 | ALOGV("%s %d", __FUNCTION__, input); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2237 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2238 | inputDesc->removeClient(portId); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2239 | |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 2240 | if (inputDesc->getClientCount() > 0) { |
| 2241 | ALOGV("%s(%d) %zu clients remaining", __func__, portId, inputDesc->getClientCount()); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2242 | return; |
| 2243 | } |
| 2244 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2245 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2246 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2247 | ALOGV("%s exit", __FUNCTION__); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2250 | void AudioPolicyManager::closeActiveClients(const sp<AudioInputDescriptor>& input) |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2251 | { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2252 | RecordClientVector clients = input->clientsList(true); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2253 | |
| 2254 | for (const auto& client : clients) { |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2255 | closeClient(client->portId()); |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2256 | } |
| 2257 | } |
| 2258 | |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 2259 | void AudioPolicyManager::closeClient(audio_port_handle_t portId) |
| 2260 | { |
| 2261 | stopInput(portId); |
| 2262 | releaseInput(portId); |
| 2263 | } |
Eric Laurent | 8fc147b | 2018-07-22 19:13:55 -0700 | [diff] [blame] | 2264 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2265 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2266 | bool patchRemoved = false; |
| 2267 | |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 2268 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2269 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2270 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2271 | if (patch_index >= 0) { |
| 2272 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2273 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2274 | mAudioPatches.removeItemsAt(patch_index); |
| 2275 | patchRemoved = true; |
| 2276 | } |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 2277 | inputDesc->close(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2278 | } |
| 2279 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2280 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2281 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2282 | |
| 2283 | if (patchRemoved) { |
| 2284 | mpClientInterface->onAudioPatchListUpdate(); |
| 2285 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2288 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2289 | int indexMin, |
| 2290 | int indexMax) |
| 2291 | { |
| 2292 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2293 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2294 | |
| 2295 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2296 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2297 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2298 | continue; |
| 2299 | } |
| 2300 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2301 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2304 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2305 | int index, |
| 2306 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2307 | { |
| 2308 | |
Nadav Bar | 7c605f6 | 2017-12-25 00:01:52 +0200 | [diff] [blame] | 2309 | // VOICE_CALL stream has minVolumeIndex > 0 but can be muted directly by an |
| 2310 | // app that has MODIFY_PHONE_STATE permission. |
| 2311 | if (((index < mVolumeCurves->getVolumeIndexMin(stream)) && |
| 2312 | !(stream == AUDIO_STREAM_VOICE_CALL && index == 0)) || |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2313 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2314 | return BAD_VALUE; |
| 2315 | } |
| 2316 | if (!audio_is_output_device(device)) { |
| 2317 | return BAD_VALUE; |
| 2318 | } |
| 2319 | |
| 2320 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2321 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2322 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2323 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2324 | stream, device, index); |
| 2325 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2326 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2327 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2328 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2329 | continue; |
| 2330 | } |
| 2331 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2332 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2333 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2334 | // update volume on all outputs and streams matching the following: |
| 2335 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2336 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2337 | // the requested device |
| 2338 | // - For non default requested device, currently selected device on the output is either the |
| 2339 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2340 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2341 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2342 | status_t status = NO_ERROR; |
| 2343 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2344 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2345 | audio_devices_t curDevice = desc->devices().types(); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2346 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2347 | if (!(streamsMatchForvolume(stream, (audio_stream_type_t)curStream))) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2348 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2349 | } |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 2350 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || isInCall())) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2351 | continue; |
| 2352 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2353 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2354 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2355 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2356 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2357 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2358 | continue; |
| 2359 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2360 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2361 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2362 | curStreamDevice |= device; |
nobuaki tanaka | 985d15a | 2017-07-19 13:38:51 +0900 | [diff] [blame] | 2363 | applyVolume = (Volume::getDeviceForVolume(curDevice) & curStreamDevice) != 0; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2364 | } else { |
| 2365 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2366 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2367 | } |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2368 | // rescale index before applying to curStream as ranges may be different for |
| 2369 | // stream and curStream |
| 2370 | int idx = rescaleVolumeIndex(index, stream, (audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2371 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2372 | //FIXME: workaround for truncated touch sounds |
| 2373 | // delayed volume change for system stream to be removed when the problem is |
| 2374 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2375 | status_t volStatus = |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 2376 | checkAndSetVolume((audio_stream_type_t)curStream, idx, desc, curDevice, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2377 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2378 | if (volStatus != NO_ERROR) { |
| 2379 | status = volStatus; |
| 2380 | } |
| 2381 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2382 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2383 | } |
| 2384 | return status; |
| 2385 | } |
| 2386 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2387 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2388 | int *index, |
| 2389 | audio_devices_t device) |
| 2390 | { |
| 2391 | if (index == NULL) { |
| 2392 | return BAD_VALUE; |
| 2393 | } |
| 2394 | if (!audio_is_output_device(device)) { |
| 2395 | return BAD_VALUE; |
| 2396 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2397 | // 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] | 2398 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2399 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2400 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2401 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2402 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2403 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2404 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2405 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2406 | return NO_ERROR; |
| 2407 | } |
| 2408 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2409 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2410 | { |
| 2411 | // select one output among several suitable for global effects. |
| 2412 | // The priority is as follows: |
| 2413 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2414 | // AudioFlinger will invalidate the track and the offloaded output |
| 2415 | // will be closed causing the effect to be moved to a PCM output. |
| 2416 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2417 | // 3: The primary output |
| 2418 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2419 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2420 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2421 | DeviceVector devices = getDevicesForStrategy(strategy, false /*fromCache*/); |
| 2422 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2423 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2424 | if (outputs.size() == 0) { |
| 2425 | return AUDIO_IO_HANDLE_NONE; |
| 2426 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2427 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2428 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2429 | bool activeOnly = true; |
| 2430 | |
| 2431 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2432 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2433 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2434 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2435 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2436 | for (audio_io_handle_t output : outputs) { |
| 2437 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2438 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2439 | continue; |
| 2440 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2441 | ALOGV("selectOutputForMusicEffects activeOnly %d output %d flags 0x%08x", |
| 2442 | activeOnly, output, desc->mFlags); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2443 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2444 | outputOffloaded = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2445 | } |
| 2446 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2447 | outputDeepBuffer = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2448 | } |
| 2449 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2450 | outputPrimary = output; |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2454 | output = outputOffloaded; |
| 2455 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2456 | output = outputDeepBuffer; |
| 2457 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2458 | output = outputPrimary; |
| 2459 | } else { |
| 2460 | output = outputs[0]; |
| 2461 | } |
| 2462 | activeOnly = false; |
| 2463 | } |
| 2464 | |
| 2465 | if (output != mMusicEffectOutput) { |
| 2466 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2467 | mMusicEffectOutput = output; |
| 2468 | } |
| 2469 | |
| 2470 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2471 | return output; |
| 2472 | } |
| 2473 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2474 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2475 | { |
| 2476 | return selectOutputForMusicEffects(); |
| 2477 | } |
| 2478 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2479 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2480 | audio_io_handle_t io, |
| 2481 | uint32_t strategy, |
| 2482 | int session, |
| 2483 | int id) |
| 2484 | { |
| 2485 | ssize_t index = mOutputs.indexOfKey(io); |
| 2486 | if (index < 0) { |
| 2487 | index = mInputs.indexOfKey(io); |
| 2488 | if (index < 0) { |
| 2489 | ALOGW("registerEffect() unknown io %d", io); |
| 2490 | return INVALID_OPERATION; |
| 2491 | } |
| 2492 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2493 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2494 | } |
| 2495 | |
Eric Laurent | c241b0d | 2018-11-28 09:08:49 -0800 | [diff] [blame] | 2496 | status_t AudioPolicyManager::unregisterEffect(int id) |
| 2497 | { |
| 2498 | if (mEffects.getEffect(id) == nullptr) { |
| 2499 | return INVALID_OPERATION; |
| 2500 | } |
| 2501 | |
| 2502 | if (mEffects.isEffectEnabled(id)) { |
| 2503 | ALOGW("%s effect %d enabled", __FUNCTION__, id); |
| 2504 | setEffectEnabled(id, false); |
| 2505 | } |
| 2506 | return mEffects.unregisterEffect(id); |
| 2507 | } |
| 2508 | |
| 2509 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
| 2510 | { |
| 2511 | sp<EffectDescriptor> effect = mEffects.getEffect(id); |
| 2512 | if (effect == nullptr) { |
| 2513 | return INVALID_OPERATION; |
| 2514 | } |
| 2515 | |
| 2516 | status_t status = mEffects.setEffectEnabled(id, enabled); |
| 2517 | if (status == NO_ERROR) { |
| 2518 | mInputs.trackEffectEnabled(effect, enabled); |
| 2519 | } |
| 2520 | return status; |
| 2521 | } |
| 2522 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2523 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2524 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2525 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2526 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2527 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2528 | continue; |
| 2529 | } |
| 2530 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2531 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2532 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2536 | { |
| 2537 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2538 | } |
| 2539 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2540 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2541 | { |
| 2542 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2543 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2544 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2545 | return true; |
| 2546 | } |
| 2547 | } |
| 2548 | return false; |
| 2549 | } |
| 2550 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2551 | // Register a list of custom mixes with their attributes and format. |
| 2552 | // When a mix is registered, corresponding input and output profiles are |
| 2553 | // added to the remote submix hw module. The profile contains only the |
| 2554 | // parameters (sampling rate, format...) specified by the mix. |
| 2555 | // The corresponding input remote submix device is also connected. |
| 2556 | // |
| 2557 | // When a remote submix device is connected, the address is checked to select the |
| 2558 | // appropriate profile and the corresponding input or output stream is opened. |
| 2559 | // |
| 2560 | // When capture starts, getInputForAttr() will: |
| 2561 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2562 | // - 2 if none found, getDeviceForInputSource() will: |
| 2563 | // - 2.1 look for a mix matching the attributes source |
| 2564 | // - 2.2 if none found, default to device selection by policy rules |
| 2565 | // At this time, the corresponding output remote submix device is also connected |
| 2566 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2567 | // after AudioTracks are invalidated |
| 2568 | // |
| 2569 | // When playback starts, getOutputForAttr() will: |
| 2570 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2571 | // - 2 if none found, look for a mix matching the attributes usage |
| 2572 | // - 3 if none found, default to device and output selection by policy rules. |
| 2573 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2574 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2575 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2576 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2577 | status_t res = NO_ERROR; |
| 2578 | |
| 2579 | sp<HwModule> rSubmixModule; |
| 2580 | // examine each mix's route type |
| 2581 | for (size_t i = 0; i < mixes.size(); i++) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2582 | AudioMix mix = mixes[i]; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2583 | // 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] | 2584 | if ((mix.mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2585 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2586 | break; |
| 2587 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2588 | 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] | 2589 | 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] | 2590 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2591 | rSubmixModule = mHwModules.getModuleFromName( |
| 2592 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2593 | if (rSubmixModule == 0) { |
| 2594 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", |
| 2595 | i); |
| 2596 | res = INVALID_OPERATION; |
| 2597 | break; |
| 2598 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2599 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2600 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2601 | String8 address = mix.mDeviceAddress; |
| 2602 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
| 2603 | mix.mDeviceType = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 2604 | } else { |
| 2605 | mix.mDeviceType = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 2606 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2607 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2608 | if (mPolicyMixes.registerMix(address, mix, 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2609 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2610 | res = INVALID_OPERATION; |
| 2611 | break; |
| 2612 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2613 | audio_config_t outputConfig = mix.mFormat; |
| 2614 | audio_config_t inputConfig = mix.mFormat; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2615 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2616 | // stereo and let audio flinger do the channel conversion if needed. |
| 2617 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2618 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2619 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2620 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2621 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2622 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2623 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2624 | if (mix.mMixType == MIX_TYPE_PLAYERS) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2625 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2626 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2627 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2628 | } else { |
| 2629 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2630 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2631 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2632 | } |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2633 | } else if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2634 | String8 address = mix.mDeviceAddress; |
| 2635 | audio_devices_t device = mix.mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2636 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2637 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2638 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2639 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2640 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2641 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2642 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2643 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2644 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2645 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2646 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2647 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 2648 | if (mPolicyMixes.registerMix(address, mix, desc) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2649 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2650 | } else { |
| 2651 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2652 | } |
| 2653 | break; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | if (res != NO_ERROR) { |
| 2658 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2659 | i, device, address.string()); |
| 2660 | res = INVALID_OPERATION; |
| 2661 | break; |
| 2662 | } else if (!foundOutput) { |
| 2663 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2664 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2665 | res = INVALID_OPERATION; |
| 2666 | break; |
| 2667 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2668 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2669 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2670 | if (res != NO_ERROR) { |
| 2671 | unregisterPolicyMixes(mixes); |
| 2672 | } |
| 2673 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2674 | } |
| 2675 | |
| 2676 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2677 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2678 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2679 | status_t res = NO_ERROR; |
| 2680 | sp<HwModule> rSubmixModule; |
| 2681 | // examine each mix's route type |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2682 | for (const auto& mix : mixes) { |
| 2683 | 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] | 2684 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2685 | if (rSubmixModule == 0) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 2686 | rSubmixModule = mHwModules.getModuleFromName( |
| 2687 | AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX); |
| 2688 | if (rSubmixModule == 0) { |
| 2689 | res = INVALID_OPERATION; |
| 2690 | continue; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2691 | } |
| 2692 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2693 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2694 | String8 address = mix.mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2695 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2696 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2697 | res = INVALID_OPERATION; |
| 2698 | continue; |
| 2699 | } |
| 2700 | |
| 2701 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2702 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2703 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2704 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2705 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2706 | } |
| 2707 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2708 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2709 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2710 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2711 | address.string(), "remote-submix", AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2712 | } |
| 2713 | rSubmixModule->removeOutputProfile(address); |
| 2714 | rSubmixModule->removeInputProfile(address); |
| 2715 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2716 | } if ((mix.mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2717 | if (mPolicyMixes.unregisterMix(mix.mDeviceAddress) != NO_ERROR) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2718 | res = INVALID_OPERATION; |
| 2719 | continue; |
| 2720 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2721 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2722 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2723 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2724 | } |
| 2725 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 2726 | void AudioPolicyManager::dumpManualSurroundFormats(String8 *dst) const |
| 2727 | { |
| 2728 | size_t i = 0; |
| 2729 | constexpr size_t audioFormatPrefixLen = sizeof("AUDIO_FORMAT_"); |
| 2730 | for (const auto& fmt : mManualSurroundFormats) { |
| 2731 | if (i++ != 0) dst->append(", "); |
| 2732 | std::string sfmt; |
| 2733 | FormatConverter::toString(fmt, sfmt); |
| 2734 | dst->append(sfmt.size() >= audioFormatPrefixLen ? |
| 2735 | sfmt.c_str() + audioFormatPrefixLen - 1 : sfmt.c_str()); |
| 2736 | } |
| 2737 | } |
| 2738 | |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2739 | status_t AudioPolicyManager::setUidDeviceAffinities(uid_t uid, |
| 2740 | const Vector<AudioDeviceTypeAddr>& devices) { |
| 2741 | ALOGV("%s() uid=%d num devices %zu", __FUNCTION__, uid, devices.size()); |
| 2742 | // uid/device affinity is only for output devices |
| 2743 | for (size_t i = 0; i < devices.size(); i++) { |
| 2744 | if (!audio_is_output_device(devices[i].mType)) { |
| 2745 | ALOGE("setUidDeviceAffinities() device=%08x is NOT an output device", |
| 2746 | devices[i].mType); |
| 2747 | return BAD_VALUE; |
| 2748 | } |
| 2749 | } |
| 2750 | status_t res = mPolicyMixes.setUidDeviceAffinities(uid, devices); |
| 2751 | if (res == NO_ERROR) { |
| 2752 | // reevaluate outputs for all given devices |
| 2753 | for (size_t i = 0; i < devices.size(); i++) { |
| 2754 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2755 | devices[i].mType, devices[i].mAddress, String8(), |
| 2756 | AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2757 | SortedVector<audio_io_handle_t> outputs; |
| 2758 | if (checkOutputsForDevice(devDesc, AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2759 | outputs) != NO_ERROR) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2760 | ALOGE("setUidDeviceAffinities() error in checkOutputsForDevice for device=%08x" |
| 2761 | " addr=%s", devices[i].mType, devices[i].mAddress.string()); |
| 2762 | return INVALID_OPERATION; |
| 2763 | } |
| 2764 | } |
| 2765 | } |
| 2766 | return res; |
| 2767 | } |
| 2768 | |
| 2769 | status_t AudioPolicyManager::removeUidDeviceAffinities(uid_t uid) { |
| 2770 | ALOGV("%s() uid=%d", __FUNCTION__, uid); |
| 2771 | Vector<AudioDeviceTypeAddr> devices; |
| 2772 | status_t res = mPolicyMixes.getDevicesForUid(uid, devices); |
| 2773 | if (res == NO_ERROR) { |
| 2774 | // reevaluate outputs for all found devices |
| 2775 | for (size_t i = 0; i < devices.size(); i++) { |
| 2776 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor( |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 2777 | devices[i].mType, devices[i].mAddress, String8(), |
| 2778 | AUDIO_FORMAT_DEFAULT); |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2779 | SortedVector<audio_io_handle_t> outputs; |
| 2780 | if (checkOutputsForDevice(devDesc, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2781 | outputs) != NO_ERROR) { |
Jean-Michel Trivi | bda70da | 2018-12-19 07:30:15 -0800 | [diff] [blame] | 2782 | ALOGE("%s() error in checkOutputsForDevice for device=%08x addr=%s", |
| 2783 | __FUNCTION__, devices[i].mType, devices[i].mAddress.string()); |
| 2784 | return INVALID_OPERATION; |
| 2785 | } |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | return res; |
| 2790 | } |
| 2791 | |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2792 | void AudioPolicyManager::dump(String8 *dst) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2793 | { |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2794 | dst->appendFormat("\nAudioPolicyManager Dump: %p\n", this); |
| 2795 | dst->appendFormat(" Primary Output: %d\n", |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2796 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2797 | std::string stateLiteral; |
| 2798 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2799 | dst->appendFormat(" Phone state: %s\n", stateLiteral.c_str()); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2800 | const char* forceUses[AUDIO_POLICY_FORCE_USE_CNT] = { |
| 2801 | "communications", "media", "record", "dock", "system", |
| 2802 | "HDMI system audio", "encoded surround output", "vibrate ringing" }; |
| 2803 | for (audio_policy_force_use_t i = AUDIO_POLICY_FORCE_FOR_COMMUNICATION; |
| 2804 | 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] | 2805 | audio_policy_forced_cfg_t forceUseValue = mEngine->getForceUse(i); |
| 2806 | dst->appendFormat(" Force use for %s: %d", forceUses[i], forceUseValue); |
| 2807 | if (i == AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND && |
| 2808 | forceUseValue == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
| 2809 | dst->append(" (MANUAL: "); |
| 2810 | dumpManualSurroundFormats(dst); |
| 2811 | dst->append(")"); |
| 2812 | } |
| 2813 | dst->append("\n"); |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 2814 | } |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2815 | dst->appendFormat(" TTS output %savailable\n", mTtsOutputAvailable ? "" : "not "); |
| 2816 | dst->appendFormat(" Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2817 | dst->appendFormat(" Config source: %s\n", mConfig.getSource().c_str()); // getConfig not const |
| 2818 | mAvailableOutputDevices.dump(dst, String8("Available output")); |
| 2819 | mAvailableInputDevices.dump(dst, String8("Available input")); |
| 2820 | mHwModulesAll.dump(dst); |
| 2821 | mOutputs.dump(dst); |
| 2822 | mInputs.dump(dst); |
| 2823 | mVolumeCurves->dump(dst); |
| 2824 | mEffects.dump(dst); |
| 2825 | mAudioPatches.dump(dst); |
| 2826 | mPolicyMixes.dump(dst); |
| 2827 | mAudioSources.dump(dst); |
Andy Hung | c29d82b | 2018-10-05 12:23:17 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
| 2830 | status_t AudioPolicyManager::dump(int fd) |
| 2831 | { |
| 2832 | String8 result; |
| 2833 | dump(&result); |
Andy Hung | bb54e20 | 2018-10-05 11:42:02 -0700 | [diff] [blame] | 2834 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2835 | return NO_ERROR; |
| 2836 | } |
| 2837 | |
| 2838 | // This function checks for the parameters which can be offloaded. |
| 2839 | // This can be enhanced depending on the capability of the DSP and policy |
| 2840 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2841 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2842 | { |
| 2843 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2844 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2845 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2846 | offloadInfo.format, |
| 2847 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2848 | offloadInfo.has_video); |
| 2849 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2850 | if (mMasterMono) { |
| 2851 | return false; // no offloading if mono is set. |
| 2852 | } |
| 2853 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2854 | // Check if offload has been disabled |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2855 | if (property_get_bool("audio.offload.disable", false /* default_value */)) { |
| 2856 | ALOGV("offload disabled by audio.offload.disable"); |
| 2857 | return false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | // Check if stream type is music, then only allow offload as of now. |
| 2861 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2862 | { |
| 2863 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2864 | return false; |
| 2865 | } |
| 2866 | |
| 2867 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2868 | const bool allowOffloadWithVideo = |
| 2869 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2870 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2871 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2872 | return false; |
| 2873 | } |
| 2874 | |
| 2875 | //If duration is less than minimum value defined in property, return false |
Andy Hung | 0f6e640 | 2018-09-06 11:20:45 -0700 | [diff] [blame] | 2876 | const int min_duration_secs = property_get_int32( |
| 2877 | "audio.offload.min.duration.secs", -1 /* default_value */); |
| 2878 | if (min_duration_secs >= 0) { |
| 2879 | if (offloadInfo.duration_us < min_duration_secs * 1000000LL) { |
| 2880 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%d)", |
| 2881 | min_duration_secs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2882 | return false; |
| 2883 | } |
| 2884 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2885 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2886 | return false; |
| 2887 | } |
| 2888 | |
| 2889 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2890 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2891 | // detects there is an active non offloadable effect. |
| 2892 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2893 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2894 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2895 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2896 | return false; |
| 2897 | } |
| 2898 | |
| 2899 | // See if there is a profile to support this. |
| 2900 | // AUDIO_DEVICE_NONE |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2901 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2902 | offloadInfo.sample_rate, |
| 2903 | offloadInfo.format, |
| 2904 | offloadInfo.channel_mask, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2905 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD, |
| 2906 | true /* directOnly */); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2907 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2908 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2911 | bool AudioPolicyManager::isDirectOutputSupported(const audio_config_base_t& config, |
| 2912 | const audio_attributes_t& attributes) { |
| 2913 | audio_output_flags_t output_flags = AUDIO_OUTPUT_FLAG_NONE; |
| 2914 | audio_attributes_flags_to_audio_output_flags(attributes.flags, output_flags); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 2915 | sp<IOProfile> profile = getProfileForOutput(DeviceVector() /*ignore device */, |
Michael Chan | a94fbb2 | 2018-04-24 14:31:19 +1000 | [diff] [blame] | 2916 | config.sample_rate, |
| 2917 | config.format, |
| 2918 | config.channel_mask, |
| 2919 | output_flags, |
| 2920 | true /* directOnly */); |
| 2921 | ALOGV("%s() profile %sfound with name: %s, " |
| 2922 | "sample rate: %u, format: 0x%x, channel_mask: 0x%x, output flags: 0x%x", |
| 2923 | __FUNCTION__, profile != 0 ? "" : "NOT ", |
| 2924 | (profile != 0 ? profile->getTagName().string() : "null"), |
| 2925 | config.sample_rate, config.format, config.channel_mask, output_flags); |
| 2926 | return (profile != 0); |
| 2927 | } |
| 2928 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2929 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2930 | audio_port_type_t type, |
| 2931 | unsigned int *num_ports, |
| 2932 | struct audio_port *ports, |
| 2933 | unsigned int *generation) |
| 2934 | { |
| 2935 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2936 | generation == NULL) { |
| 2937 | return BAD_VALUE; |
| 2938 | } |
| 2939 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2940 | if (ports == NULL) { |
| 2941 | *num_ports = 0; |
| 2942 | } |
| 2943 | |
| 2944 | size_t portsWritten = 0; |
| 2945 | size_t portsMax = *num_ports; |
| 2946 | *num_ports = 0; |
| 2947 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2948 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2949 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2950 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2951 | for (const auto& dev : mAvailableOutputDevices) { |
| 2952 | if (dev->type() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2953 | continue; |
| 2954 | } |
| 2955 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2956 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2957 | } |
| 2958 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2959 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2960 | } |
| 2961 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2962 | for (const auto& dev : mAvailableInputDevices) { |
| 2963 | if (dev->type() == AUDIO_DEVICE_IN_STUB) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2964 | continue; |
| 2965 | } |
| 2966 | if (portsWritten < portsMax) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 2967 | dev->toAudioPort(&ports[portsWritten++]); |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2968 | } |
| 2969 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2970 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2971 | } |
| 2972 | } |
| 2973 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2974 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2975 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2976 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2977 | } |
| 2978 | *num_ports += mInputs.size(); |
| 2979 | } |
| 2980 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2981 | size_t numOutputs = 0; |
| 2982 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2983 | if (!mOutputs[i]->isDuplicated()) { |
| 2984 | numOutputs++; |
| 2985 | if (portsWritten < portsMax) { |
| 2986 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2987 | } |
| 2988 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2989 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2990 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2991 | } |
| 2992 | } |
| 2993 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2994 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2995 | return NO_ERROR; |
| 2996 | } |
| 2997 | |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 2998 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2999 | { |
Eric Laurent | 99fcae4 | 2018-05-17 16:59:18 -0700 | [diff] [blame] | 3000 | if (port == nullptr || port->id == AUDIO_PORT_HANDLE_NONE) { |
| 3001 | return BAD_VALUE; |
| 3002 | } |
| 3003 | sp<DeviceDescriptor> dev = mAvailableOutputDevices.getDeviceFromId(port->id); |
| 3004 | if (dev != 0) { |
| 3005 | dev->toAudioPort(port); |
| 3006 | return NO_ERROR; |
| 3007 | } |
| 3008 | dev = mAvailableInputDevices.getDeviceFromId(port->id); |
| 3009 | if (dev != 0) { |
| 3010 | dev->toAudioPort(port); |
| 3011 | return NO_ERROR; |
| 3012 | } |
| 3013 | sp<SwAudioOutputDescriptor> out = mOutputs.getOutputFromId(port->id); |
| 3014 | if (out != 0) { |
| 3015 | out->toAudioPort(port); |
| 3016 | return NO_ERROR; |
| 3017 | } |
| 3018 | sp<AudioInputDescriptor> in = mInputs.getInputFromId(port->id); |
| 3019 | if (in != 0) { |
| 3020 | in->toAudioPort(port); |
| 3021 | return NO_ERROR; |
| 3022 | } |
| 3023 | return BAD_VALUE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3024 | } |
| 3025 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3026 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 3027 | audio_patch_handle_t *handle, |
| 3028 | uid_t uid) |
| 3029 | { |
| 3030 | ALOGV("createAudioPatch()"); |
| 3031 | |
| 3032 | if (handle == NULL || patch == NULL) { |
| 3033 | return BAD_VALUE; |
| 3034 | } |
| 3035 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 3036 | |
Mikhail Naganov | ac9858b | 2018-06-15 13:12:37 -0700 | [diff] [blame] | 3037 | if (!audio_patch_is_valid(patch)) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3038 | return BAD_VALUE; |
| 3039 | } |
| 3040 | // only one source per audio patch supported for now |
| 3041 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3042 | return INVALID_OPERATION; |
| 3043 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3044 | |
| 3045 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3046 | return INVALID_OPERATION; |
| 3047 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3048 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3049 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 3050 | return INVALID_OPERATION; |
| 3051 | } |
| 3052 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3053 | |
| 3054 | sp<AudioPatch> patchDesc; |
| 3055 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 3056 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3057 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 3058 | patch->sources[0].role, |
| 3059 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3060 | #if LOG_NDEBUG == 0 |
| 3061 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3062 | 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] | 3063 | patch->sinks[i].role, |
| 3064 | patch->sinks[i].type); |
| 3065 | } |
| 3066 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3067 | |
| 3068 | if (index >= 0) { |
| 3069 | patchDesc = mAudioPatches.valueAt(index); |
| 3070 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3071 | mUidCached, patchDesc->mUid, uid); |
| 3072 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3073 | return INVALID_OPERATION; |
| 3074 | } |
| 3075 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 3076 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3077 | } |
| 3078 | |
| 3079 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3080 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3081 | if (outputDesc == NULL) { |
| 3082 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3083 | return BAD_VALUE; |
| 3084 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3085 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 3086 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3087 | if (patchDesc != 0) { |
| 3088 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 3089 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 3090 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 3091 | return BAD_VALUE; |
| 3092 | } |
| 3093 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3094 | DeviceVector devices; |
| 3095 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3096 | // Only support mix to devices connection |
| 3097 | // TODO add support for mix to mix connection |
| 3098 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3099 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 3100 | return INVALID_OPERATION; |
| 3101 | } |
| 3102 | sp<DeviceDescriptor> devDesc = |
| 3103 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 3104 | if (devDesc == 0) { |
| 3105 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 3106 | return BAD_VALUE; |
| 3107 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3108 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3109 | if (!outputDesc->mProfile->isCompatibleProfile(DeviceVector(devDesc), |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3110 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3111 | NULL, // updatedSamplingRate |
| 3112 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3113 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3114 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3115 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3116 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3117 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 3118 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3119 | return INVALID_OPERATION; |
| 3120 | } |
| 3121 | devices.add(devDesc); |
| 3122 | } |
| 3123 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3124 | return INVALID_OPERATION; |
| 3125 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3126 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3127 | // TODO: reconfigure output format and channels here |
| 3128 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3129 | devices.types(), outputDesc->mIoHandle); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3130 | setOutputDevices(outputDesc, devices, true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3131 | index = mAudioPatches.indexOfKey(*handle); |
| 3132 | if (index >= 0) { |
| 3133 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3134 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 3135 | } |
| 3136 | patchDesc = mAudioPatches.valueAt(index); |
| 3137 | patchDesc->mUid = uid; |
| 3138 | ALOGV("createAudioPatch() success"); |
| 3139 | } else { |
| 3140 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 3141 | return INVALID_OPERATION; |
| 3142 | } |
| 3143 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3144 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 3145 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3146 | // only one sink supported when connecting an input device to a mix |
| 3147 | if (patch->num_sinks > 1) { |
| 3148 | return INVALID_OPERATION; |
| 3149 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3150 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3151 | if (inputDesc == NULL) { |
| 3152 | return BAD_VALUE; |
| 3153 | } |
| 3154 | if (patchDesc != 0) { |
| 3155 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 3156 | return BAD_VALUE; |
| 3157 | } |
| 3158 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3159 | sp<DeviceDescriptor> device = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3160 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3161 | if (device == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3162 | return BAD_VALUE; |
| 3163 | } |
| 3164 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3165 | if (!inputDesc->mProfile->isCompatibleProfile(DeviceVector(device), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3166 | patch->sinks[0].sample_rate, |
| 3167 | NULL, /*updatedSampleRate*/ |
| 3168 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3169 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3170 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 3171 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3172 | // FIXME for the parameter type, |
| 3173 | // and the NONE |
| 3174 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 3175 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3176 | return INVALID_OPERATION; |
| 3177 | } |
| 3178 | // TODO: reconfigure output format and channels here |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3179 | ALOGV("%s() setting device %s on output %d", __func__, |
| 3180 | device->toString().c_str(), inputDesc->mIoHandle); |
| 3181 | setInputDevice(inputDesc->mIoHandle, device, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3182 | index = mAudioPatches.indexOfKey(*handle); |
| 3183 | if (index >= 0) { |
| 3184 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 3185 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 3186 | } |
| 3187 | patchDesc = mAudioPatches.valueAt(index); |
| 3188 | patchDesc->mUid = uid; |
| 3189 | ALOGV("createAudioPatch() success"); |
| 3190 | } else { |
| 3191 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 3192 | return INVALID_OPERATION; |
| 3193 | } |
| 3194 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3195 | // device to device connection |
| 3196 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3197 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3198 | return BAD_VALUE; |
| 3199 | } |
| 3200 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3201 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3202 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3203 | if (srcDevice == 0) { |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 3204 | return BAD_VALUE; |
| 3205 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3206 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3207 | //update source and sink with our own data as the data passed in the patch may |
| 3208 | // be incomplete. |
| 3209 | struct audio_patch newPatch = *patch; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3210 | srcDevice->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3211 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3212 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 3213 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 3214 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 3215 | return INVALID_OPERATION; |
| 3216 | } |
| 3217 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3218 | sp<DeviceDescriptor> sinkDevice = |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3219 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3220 | if (sinkDevice == 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3221 | return BAD_VALUE; |
| 3222 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3223 | sinkDevice->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3224 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3225 | // create a software bridge in PatchPanel if: |
Scott Randolph | f317240 | 2018-01-23 17:06:53 -0800 | [diff] [blame] | 3226 | // - source and sink devices are on different HW modules OR |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3227 | // - audio HAL version is < 3.0 |
Francois Gaffie | 99896da | 2018-04-09 11:05:33 +0200 | [diff] [blame] | 3228 | // - 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] | 3229 | if (!srcDevice->hasSameHwModuleAs(sinkDevice) || |
| 3230 | (srcDevice->getModuleVersionMajor() < 3) || |
| 3231 | !srcDevice->getModule()->supportsPatch(srcDevice, sinkDevice)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3232 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3233 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3234 | return INVALID_OPERATION; |
| 3235 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3236 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3237 | getOutputsForDevices(DeviceVector(sinkDevice), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3238 | // if the sink device is reachable via an opened output stream, request to go via |
| 3239 | // this output stream by adding a second source to the patch description |
jiabin | 4057332 | 2018-11-08 12:08:02 -0800 | [diff] [blame] | 3240 | audio_io_handle_t output = selectOutput(outputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3241 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3242 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3243 | if (outputDesc->isDuplicated()) { |
| 3244 | return INVALID_OPERATION; |
| 3245 | } |
| 3246 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3247 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3248 | newPatch.num_sources = 2; |
| 3249 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3250 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3251 | } |
| 3252 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3253 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3254 | status_t status = installPatch(__func__, index, handle, &newPatch, 0, uid, &patchDesc); |
| 3255 | if (status != NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3256 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3257 | status); |
| 3258 | return INVALID_OPERATION; |
| 3259 | } |
| 3260 | } else { |
| 3261 | return BAD_VALUE; |
| 3262 | } |
| 3263 | } else { |
| 3264 | return BAD_VALUE; |
| 3265 | } |
| 3266 | return NO_ERROR; |
| 3267 | } |
| 3268 | |
| 3269 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3270 | uid_t uid) |
| 3271 | { |
| 3272 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3273 | |
| 3274 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3275 | |
| 3276 | if (index < 0) { |
| 3277 | return BAD_VALUE; |
| 3278 | } |
| 3279 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3280 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3281 | mUidCached, patchDesc->mUid, uid); |
| 3282 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3283 | return INVALID_OPERATION; |
| 3284 | } |
| 3285 | |
| 3286 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3287 | patchDesc->mUid = mUidCached; |
| 3288 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3289 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3290 | if (outputDesc == NULL) { |
| 3291 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3292 | return BAD_VALUE; |
| 3293 | } |
| 3294 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3295 | setOutputDevices(outputDesc, |
| 3296 | getNewOutputDevices(outputDesc, true /*fromCache*/), |
| 3297 | true, |
| 3298 | 0, |
| 3299 | NULL); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3300 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3301 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3302 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3303 | if (inputDesc == NULL) { |
| 3304 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3305 | return BAD_VALUE; |
| 3306 | } |
| 3307 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3308 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3309 | true, |
| 3310 | NULL); |
| 3311 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3312 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3313 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3314 | status, patchDesc->mAfPatchHandle); |
| 3315 | removeAudioPatch(patchDesc->mHandle); |
| 3316 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3317 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3318 | } else { |
| 3319 | return BAD_VALUE; |
| 3320 | } |
| 3321 | } else { |
| 3322 | return BAD_VALUE; |
| 3323 | } |
| 3324 | return NO_ERROR; |
| 3325 | } |
| 3326 | |
| 3327 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3328 | struct audio_patch *patches, |
| 3329 | unsigned int *generation) |
| 3330 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3331 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3332 | return BAD_VALUE; |
| 3333 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3334 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3335 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3336 | } |
| 3337 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3338 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3339 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3340 | ALOGV("setAudioPortConfig()"); |
| 3341 | |
| 3342 | if (config == NULL) { |
| 3343 | return BAD_VALUE; |
| 3344 | } |
| 3345 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3346 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3347 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3348 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3349 | } |
| 3350 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3351 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3352 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3353 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3354 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3355 | if (outputDesc == NULL) { |
| 3356 | return BAD_VALUE; |
| 3357 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3358 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3359 | "setAudioPortConfig() called on duplicated output %d", |
| 3360 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3361 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3362 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3363 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3364 | if (inputDesc == NULL) { |
| 3365 | return BAD_VALUE; |
| 3366 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3367 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3368 | } else { |
| 3369 | return BAD_VALUE; |
| 3370 | } |
| 3371 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3372 | sp<DeviceDescriptor> deviceDesc; |
| 3373 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3374 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3375 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3376 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3377 | } else { |
| 3378 | return BAD_VALUE; |
| 3379 | } |
| 3380 | if (deviceDesc == NULL) { |
| 3381 | return BAD_VALUE; |
| 3382 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3383 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3384 | } else { |
| 3385 | return BAD_VALUE; |
| 3386 | } |
| 3387 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3388 | struct audio_port_config backupConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3389 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3390 | if (status == NO_ERROR) { |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3391 | struct audio_port_config newConfig = {}; |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3392 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3393 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3394 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3395 | if (status != NO_ERROR) { |
| 3396 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3397 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3398 | |
| 3399 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3402 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3403 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3404 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3405 | clearAudioPatches(uid); |
| 3406 | clearSessionRoutes(uid); |
| 3407 | } |
| 3408 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3409 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3410 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3411 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3412 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3413 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3414 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3415 | } |
| 3416 | } |
| 3417 | } |
| 3418 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3419 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3420 | audio_io_handle_t ouptutToSkip) |
| 3421 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3422 | DeviceVector devices = getDevicesForStrategy(strategy, false /*fromCache*/); |
| 3423 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevices(devices, mOutputs); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3424 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3425 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3426 | continue; |
| 3427 | } |
| 3428 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3429 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3430 | continue; |
| 3431 | } |
| 3432 | // If the default device for this strategy is on another output mix, |
| 3433 | // invalidate all tracks in this strategy to force re connection. |
| 3434 | // Otherwise select new device on the output mix. |
| 3435 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3436 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3437 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3438 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3439 | } |
| 3440 | } |
| 3441 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3442 | setOutputDevices( |
| 3443 | outputDesc, getNewOutputDevices(outputDesc, false /*fromCache*/), false); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3444 | } |
| 3445 | } |
| 3446 | } |
| 3447 | |
| 3448 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3449 | { |
| 3450 | // remove output routes associated with this uid |
| 3451 | SortedVector<routing_strategy> affectedStrategies; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3452 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3453 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3454 | for (const auto& client : outputDesc->getClientIterable()) { |
| 3455 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3456 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3457 | affectedStrategies.add(getStrategy(client->stream())); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3458 | } |
| 3459 | } |
| 3460 | } |
| 3461 | // reroute outputs if necessary |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3462 | for (const auto& strategy : affectedStrategies) { |
| 3463 | checkStrategyRoute(strategy, AUDIO_IO_HANDLE_NONE); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | // remove input routes associated with this uid |
| 3467 | SortedVector<audio_source_t> affectedSources; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3468 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3469 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Andy Hung | 39efb7a | 2018-09-26 15:39:28 -0700 | [diff] [blame] | 3470 | for (const auto& client : inputDesc->getClientIterable()) { |
| 3471 | if (client->hasPreferredDevice() && client->uid() == uid) { |
| 3472 | client->setPreferredDeviceId(AUDIO_PORT_HANDLE_NONE); |
| 3473 | affectedSources.add(client->source()); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | } |
| 3477 | // reroute inputs if necessary |
| 3478 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3479 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3480 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3481 | if (affectedSources.indexOf(inputDesc->source()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3482 | inputsToClose.add(inputDesc->mIoHandle); |
| 3483 | } |
| 3484 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3485 | for (const auto& input : inputsToClose) { |
| 3486 | closeInput(input); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3487 | } |
| 3488 | } |
| 3489 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3490 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3491 | { |
| 3492 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3493 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3494 | if (sourceDesc->uid() == uid) { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3495 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3496 | } |
| 3497 | } |
| 3498 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3499 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3500 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3501 | audio_io_handle_t *ioHandle, |
| 3502 | audio_devices_t *device) |
| 3503 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3504 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3505 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 3506 | audio_attributes_t attr = { .source = AUDIO_SOURCE_HOTWORD }; |
| 3507 | *device = getDeviceAndMixForAttributes(attr)->type(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3508 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3509 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3512 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3513 | const audio_attributes_t *attributes, |
| 3514 | audio_port_handle_t *portId, |
| 3515 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3516 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3517 | ALOGV("%s", __FUNCTION__); |
| 3518 | *portId = AUDIO_PORT_HANDLE_NONE; |
| 3519 | |
| 3520 | if (source == NULL || attributes == NULL || portId == NULL) { |
| 3521 | ALOGW("%s invalid argument: source %p attributes %p handle %p", |
| 3522 | __FUNCTION__, source, attributes, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3523 | return BAD_VALUE; |
| 3524 | } |
| 3525 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3526 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3527 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3528 | ALOGW("%s INVALID_OPERATION source->role %d source->type %d", |
| 3529 | __FUNCTION__, source->role, source->type); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3530 | return INVALID_OPERATION; |
| 3531 | } |
| 3532 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3533 | sp<DeviceDescriptor> srcDevice = |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3534 | mAvailableInputDevices.getDevice(source->ext.device.type, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3535 | String8(source->ext.device.address), |
| 3536 | AUDIO_FORMAT_DEFAULT); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3537 | if (srcDevice == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3538 | 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] | 3539 | return BAD_VALUE; |
| 3540 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3541 | |
| 3542 | *portId = AudioPort::getNextUniqueId(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3543 | |
Mikhail Naganov | 7be71d2 | 2018-05-23 16:51:46 -0700 | [diff] [blame] | 3544 | struct audio_patch dummyPatch = {}; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3545 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3546 | |
| 3547 | sp<SourceClientDescriptor> sourceDesc = |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3548 | new SourceClientDescriptor(*portId, uid, *attributes, patchDesc, srcDevice, |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3549 | streamTypefromAttributesInt(attributes), |
| 3550 | getStrategyForAttr(attributes)); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3551 | |
| 3552 | status_t status = connectAudioSource(sourceDesc); |
| 3553 | if (status == NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3554 | mAudioSources.add(*portId, sourceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3555 | } |
| 3556 | return status; |
| 3557 | } |
| 3558 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3559 | status_t AudioPolicyManager::connectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3560 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3561 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3562 | |
| 3563 | // make sure we only have one patch per source. |
| 3564 | disconnectAudioSource(sourceDesc); |
| 3565 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3566 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3567 | routing_strategy strategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3568 | audio_stream_type_t stream = sourceDesc->stream(); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3569 | sp<DeviceDescriptor> srcDevice = sourceDesc->srcDevice(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3570 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3571 | DeviceVector sinkDevices = getDevicesForStrategy(strategy, true); |
| 3572 | ALOG_ASSERT(!sinkDevices.isEmpty(), "connectAudioSource(): no device found for strategy"); |
| 3573 | sp<DeviceDescriptor> sinkDevice = sinkDevices.itemAt(0); |
| 3574 | ALOG_ASSERT(mAvailableOutputDevices.contains(sinkDevice), "%s: Device %s not available", |
| 3575 | __FUNCTION__, sinkDevice->toString().c_str()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3576 | |
| 3577 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3578 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3579 | if (srcDevice->hasSameHwModuleAs(sinkDevice) && |
| 3580 | srcDevice->getModuleVersionMajor() >= 3 && |
| 3581 | sinkDevice->getModule()->supportsPatch(srcDevice, sinkDevice) && |
| 3582 | srcDevice->getAudioPort()->mGains.size() > 0) { |
François Gaffie | 83d789d | 2018-05-29 13:29:19 +0200 | [diff] [blame] | 3583 | ALOGV("%s Device to Device route supported by >=3.0 HAL", __FUNCTION__); |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3584 | // 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] | 3585 | // create patch between src device and output device |
| 3586 | // create Hwoutput and add to mHwOutputs |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3587 | } else { |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3588 | audio_attributes_t resultAttr; |
| 3589 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3590 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3591 | config.sample_rate = sourceDesc->config().sample_rate; |
| 3592 | config.channel_mask = sourceDesc->config().channel_mask; |
| 3593 | config.format = sourceDesc->config().format; |
| 3594 | audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; |
| 3595 | audio_port_handle_t selectedDeviceId = AUDIO_PORT_HANDLE_NONE; |
| 3596 | getOutputForAttrInt(&resultAttr, &output, AUDIO_SESSION_NONE, |
| 3597 | &attributes, &stream, sourceDesc->uid(), &config, &flags, &selectedDeviceId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3598 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3599 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevices.types()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3600 | return INVALID_OPERATION; |
| 3601 | } |
| 3602 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3603 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3604 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevices.types()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3605 | return INVALID_OPERATION; |
| 3606 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3607 | status_t status = outputDesc->start(); |
| 3608 | if (status != NO_ERROR) { |
| 3609 | return status; |
| 3610 | } |
| 3611 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3612 | // create a special patch with no sink and two sources: |
| 3613 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3614 | // be connected as well as the stream type for volume control |
| 3615 | // - the sink is defined by whatever output device is currently selected for the output |
| 3616 | // though which this patch is routed. |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3617 | PatchBuilder patchBuilder; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 3618 | patchBuilder.addSource(srcDevice).addSource(outputDesc, { .stream = stream }); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 3619 | status = mpClientInterface->createAudioPatch(patchBuilder.patch(), |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3620 | &afPatchHandle, |
| 3621 | 0); |
| 3622 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3623 | status, afPatchHandle); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3624 | sourceDesc->patchDesc()->mPatch = *patchBuilder.patch(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3625 | if (status != NO_ERROR) { |
| 3626 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3627 | __FUNCTION__, status); |
| 3628 | return INVALID_OPERATION; |
| 3629 | } |
Hongwei Wang | bb93dfb | 2018-10-23 13:54:22 -0700 | [diff] [blame] | 3630 | |
| 3631 | if (outputDesc->getClient(sourceDesc->portId()) != nullptr) { |
| 3632 | ALOGW("%s source portId has already been attached to outputDesc", __func__); |
| 3633 | return INVALID_OPERATION; |
| 3634 | } |
| 3635 | outputDesc->addClient(sourceDesc); |
| 3636 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3637 | uint32_t delayMs = 0; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3638 | status = startSource(outputDesc, sourceDesc, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3639 | |
| 3640 | if (status != NO_ERROR) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3641 | mpClientInterface->releaseAudioPatch(sourceDesc->patchDesc()->mAfPatchHandle, 0); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3642 | return status; |
| 3643 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3644 | sourceDesc->setSwOutput(outputDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3645 | if (delayMs != 0) { |
| 3646 | usleep(delayMs * 1000); |
| 3647 | } |
| 3648 | } |
| 3649 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3650 | sourceDesc->patchDesc()->mAfPatchHandle = afPatchHandle; |
| 3651 | addAudioPatch(sourceDesc->patchDesc()->mHandle, sourceDesc->patchDesc()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3652 | |
| 3653 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3656 | status_t AudioPolicyManager::stopAudioSource(audio_port_handle_t portId) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3657 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3658 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueFor(portId); |
| 3659 | ALOGV("%s port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3660 | if (sourceDesc == 0) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3661 | ALOGW("%s unknown source for port ID %d", __FUNCTION__, portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3662 | return BAD_VALUE; |
| 3663 | } |
| 3664 | status_t status = disconnectAudioSource(sourceDesc); |
| 3665 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3666 | mAudioSources.removeItem(portId); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3667 | return status; |
| 3668 | } |
| 3669 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3670 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3671 | { |
| 3672 | if (mMasterMono == mono) { |
| 3673 | return NO_ERROR; |
| 3674 | } |
| 3675 | mMasterMono = mono; |
| 3676 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3677 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3678 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3679 | // |
| 3680 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3681 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3682 | // play back offloaded. |
| 3683 | if (mMasterMono) { |
| 3684 | Vector<audio_io_handle_t> offloaded; |
| 3685 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3686 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3687 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3688 | offloaded.push(desc->mIoHandle); |
| 3689 | } |
| 3690 | } |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 3691 | for (const auto& handle : offloaded) { |
| 3692 | closeOutput(handle); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3693 | } |
| 3694 | } |
| 3695 | // update master mono for all remaining outputs |
| 3696 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3697 | updateMono(mOutputs.keyAt(i)); |
| 3698 | } |
| 3699 | return NO_ERROR; |
| 3700 | } |
| 3701 | |
| 3702 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3703 | { |
| 3704 | *mono = mMasterMono; |
| 3705 | return NO_ERROR; |
| 3706 | } |
| 3707 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3708 | float AudioPolicyManager::getStreamVolumeDB( |
| 3709 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3710 | { |
| 3711 | return computeVolume(stream, index, device); |
| 3712 | } |
| 3713 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3714 | status_t AudioPolicyManager::getSurroundFormats(unsigned int *numSurroundFormats, |
| 3715 | audio_format_t *surroundFormats, |
| 3716 | bool *surroundFormatsEnabled, |
| 3717 | bool reported) |
| 3718 | { |
| 3719 | if (numSurroundFormats == NULL || (*numSurroundFormats != 0 && |
| 3720 | (surroundFormats == NULL || surroundFormatsEnabled == NULL))) { |
| 3721 | return BAD_VALUE; |
| 3722 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3723 | ALOGV("%s() numSurroundFormats %d surroundFormats %p surroundFormatsEnabled %p reported %d", |
| 3724 | __func__, *numSurroundFormats, surroundFormats, surroundFormatsEnabled, reported); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3725 | |
| 3726 | size_t formatsWritten = 0; |
| 3727 | size_t formatsMax = *numSurroundFormats; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3728 | std::unordered_set<audio_format_t> formats; // Uses primary surround formats only |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3729 | if (reported) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3730 | // Return formats from all device profiles that have already been resolved by |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3731 | // checkOutputsForDevice(). |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3732 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 3733 | sp<DeviceDescriptor> device = mAvailableOutputDevices[i]; |
| 3734 | FormatVector supportedFormats = |
| 3735 | device->getAudioPort()->getAudioProfiles().getSupportedFormats(); |
| 3736 | for (size_t j = 0; j < supportedFormats.size(); j++) { |
| 3737 | if (mConfig.getSurroundFormats().count(supportedFormats[j]) != 0) { |
| 3738 | formats.insert(supportedFormats[j]); |
| 3739 | } else { |
| 3740 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3741 | if (pair.second.count(supportedFormats[j]) != 0) { |
| 3742 | formats.insert(pair.first); |
| 3743 | break; |
| 3744 | } |
| 3745 | } |
| 3746 | } |
| 3747 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3748 | } |
| 3749 | } else { |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3750 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 3751 | formats.insert(pair.first); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3752 | } |
| 3753 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3754 | *numSurroundFormats = formats.size(); |
| 3755 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 3756 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3757 | for (const auto& format: formats) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3758 | if (formatsWritten < formatsMax) { |
Mikhail Naganov | 2563f23 | 2018-09-27 14:48:01 -0700 | [diff] [blame] | 3759 | surroundFormats[formatsWritten] = format; |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3760 | bool formatEnabled = true; |
| 3761 | switch (forceUse) { |
| 3762 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL: |
| 3763 | formatEnabled = mManualSurroundFormats.count(format) != 0; |
| 3764 | break; |
| 3765 | case AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER: |
| 3766 | formatEnabled = false; |
| 3767 | break; |
| 3768 | default: // AUTO or ALWAYS => true |
| 3769 | break; |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3770 | } |
| 3771 | surroundFormatsEnabled[formatsWritten++] = formatEnabled; |
| 3772 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3773 | } |
| 3774 | return NO_ERROR; |
| 3775 | } |
| 3776 | |
| 3777 | status_t AudioPolicyManager::setSurroundFormatEnabled(audio_format_t audioFormat, bool enabled) |
| 3778 | { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3779 | ALOGV("%s() format 0x%X enabled %d", __func__, audioFormat, enabled); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3780 | const auto& formatIter = mConfig.getSurroundFormats().find(audioFormat); |
| 3781 | if (formatIter == mConfig.getSurroundFormats().end()) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3782 | ALOGW("%s() format 0x%X is not a known surround format", __func__, audioFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3783 | return BAD_VALUE; |
| 3784 | } |
| 3785 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3786 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND) != |
| 3787 | AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3788 | ALOGW("%s() not in manual mode for surround sound format selection", __func__); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3789 | return INVALID_OPERATION; |
| 3790 | } |
| 3791 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3792 | if ((mManualSurroundFormats.count(audioFormat) != 0) == enabled) { |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3793 | return NO_ERROR; |
| 3794 | } |
| 3795 | |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3796 | std::unordered_set<audio_format_t> surroundFormatsBackup(mManualSurroundFormats); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3797 | if (enabled) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3798 | mManualSurroundFormats.insert(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3799 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3800 | mManualSurroundFormats.insert(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3801 | } |
| 3802 | } else { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3803 | mManualSurroundFormats.erase(audioFormat); |
Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 3804 | for (const auto& subFormat : formatIter->second) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3805 | mManualSurroundFormats.erase(subFormat); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | |
| 3809 | sp<SwAudioOutputDescriptor> outputDesc; |
| 3810 | bool profileUpdated = false; |
Mikhail Naganov | 708e038 | 2018-05-30 09:53:04 -0700 | [diff] [blame] | 3811 | DeviceVector hdmiOutputDevices = mAvailableOutputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3812 | AUDIO_DEVICE_OUT_HDMI); |
| 3813 | for (size_t i = 0; i < hdmiOutputDevices.size(); i++) { |
| 3814 | // Simulate reconnection to update enabled surround sound formats. |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3815 | String8 address = hdmiOutputDevices[i]->address(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3816 | String8 name = hdmiOutputDevices[i]->getName(); |
| 3817 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3818 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3819 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3820 | name.c_str(), |
| 3821 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3822 | if (status != NO_ERROR) { |
| 3823 | continue; |
| 3824 | } |
| 3825 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_HDMI, |
| 3826 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3827 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3828 | name.c_str(), |
| 3829 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3830 | profileUpdated |= (status == NO_ERROR); |
| 3831 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3832 | // 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] | 3833 | DeviceVector hdmiInputDevices = mAvailableInputDevices.getDevicesFromTypeMask( |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3834 | AUDIO_DEVICE_IN_HDMI); |
| 3835 | for (size_t i = 0; i < hdmiInputDevices.size(); i++) { |
| 3836 | // Simulate reconnection to update enabled surround sound formats. |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 3837 | String8 address = hdmiInputDevices[i]->address(); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3838 | String8 name = hdmiInputDevices[i]->getName(); |
| 3839 | status_t status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3840 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 3841 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3842 | name.c_str(), |
| 3843 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3844 | if (status != NO_ERROR) { |
| 3845 | continue; |
| 3846 | } |
| 3847 | status = setDeviceConnectionStateInt(AUDIO_DEVICE_IN_HDMI, |
| 3848 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 3849 | address.c_str(), |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 3850 | name.c_str(), |
| 3851 | AUDIO_FORMAT_DEFAULT); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3852 | profileUpdated |= (status == NO_ERROR); |
| 3853 | } |
| 3854 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3855 | if (!profileUpdated) { |
Mikhail Naganov | 5dddbfd | 2018-09-11 14:15:05 -0700 | [diff] [blame] | 3856 | ALOGW("%s() no audio profiles updated, undoing surround formats change", __func__); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 3857 | mManualSurroundFormats = std::move(surroundFormatsBackup); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 3858 | } |
| 3859 | |
| 3860 | return profileUpdated ? NO_ERROR : INVALID_OPERATION; |
| 3861 | } |
| 3862 | |
Eric Laurent | f32108e | 2018-10-04 17:22:04 -0700 | [diff] [blame] | 3863 | void AudioPolicyManager::setAppState(uid_t uid, app_state_t state) |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3864 | { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3865 | ALOGV("%s(uid:%d, state:%d)", __func__, uid, state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3866 | |
Eric Laurent | a9f8665 | 2018-11-28 17:23:11 -0800 | [diff] [blame] | 3867 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3868 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
| 3869 | RecordClientVector clients = inputDesc->clientsList(false /*activeOnly*/); |
Eric Laurent | 8f42ea1 | 2018-08-08 09:08:25 -0700 | [diff] [blame] | 3870 | for (const auto& client : clients) { |
| 3871 | if (uid == client->uid()) { |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3872 | client->setAppState(state); |
Svet Ganov | f4ddfef | 2018-01-16 07:37:58 -0800 | [diff] [blame] | 3873 | } |
| 3874 | } |
| 3875 | } |
| 3876 | } |
| 3877 | |
jiabin | 6012f91 | 2018-11-02 17:06:30 -0700 | [diff] [blame] | 3878 | bool AudioPolicyManager::isHapticPlaybackSupported() |
| 3879 | { |
| 3880 | for (const auto& hwModule : mHwModules) { |
| 3881 | const OutputProfileCollection &outputProfiles = hwModule->getOutputProfiles(); |
| 3882 | for (const auto &outProfile : outputProfiles) { |
| 3883 | struct audio_port audioPort; |
| 3884 | outProfile->toAudioPort(&audioPort); |
| 3885 | for (size_t i = 0; i < audioPort.num_channel_masks; i++) { |
| 3886 | if (audioPort.channel_masks[i] & AUDIO_CHANNEL_HAPTIC_ALL) { |
| 3887 | return true; |
| 3888 | } |
| 3889 | } |
| 3890 | } |
| 3891 | } |
| 3892 | return false; |
| 3893 | } |
| 3894 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3895 | status_t AudioPolicyManager::disconnectAudioSource(const sp<SourceClientDescriptor>& sourceDesc) |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3896 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3897 | ALOGV("%s port Id %d", __FUNCTION__, sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3898 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3899 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3900 | if (patchDesc == 0) { |
| 3901 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3902 | sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3903 | return BAD_VALUE; |
| 3904 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3905 | removeAudioPatch(sourceDesc->patchDesc()->mHandle); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3906 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3907 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3908 | if (swOutputDesc != 0) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3909 | status_t status = stopSource(swOutputDesc, sourceDesc); |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 3910 | if (status == NO_ERROR) { |
| 3911 | swOutputDesc->stop(); |
| 3912 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3913 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3914 | } else { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3915 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->hwOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3916 | if (hwOutputDesc != 0) { |
| 3917 | // release patch between src device and output device |
| 3918 | // close Hwoutput and remove from mHwOutputs |
| 3919 | } else { |
| 3920 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3921 | } |
| 3922 | } |
| 3923 | return NO_ERROR; |
| 3924 | } |
| 3925 | |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3926 | sp<SourceClientDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3927 | audio_io_handle_t output, routing_strategy strategy) |
| 3928 | { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3929 | sp<SourceClientDescriptor> source; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3930 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3931 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3932 | audio_attributes_t attributes = sourceDesc->attributes(); |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 3933 | routing_strategy sourceStrategy = getStrategyForAttr(&attributes); |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 3934 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->swOutput().promote(); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3935 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3936 | source = sourceDesc; |
| 3937 | break; |
| 3938 | } |
| 3939 | } |
| 3940 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3941 | } |
| 3942 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3943 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3944 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3945 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3946 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3947 | { |
Mikhail Naganov | 2773dd7 | 2017-12-08 10:12:11 -0800 | [diff] [blame] | 3948 | return mAudioPortGeneration++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3951 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3952 | static const char *kConfigLocationList[] = |
| 3953 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3954 | static const int kConfigLocationListSize = |
| 3955 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3956 | |
| 3957 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3958 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3959 | std::vector<const char*> fileNames; |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3960 | status_t ret; |
| 3961 | |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3962 | if (property_get_bool("ro.bluetooth.a2dp_offload.supported", false) && |
| 3963 | property_get_bool("persist.bluetooth.a2dp_offload.disabled", false)) { |
| 3964 | // A2DP offload supported but disabled: try to use special XML file |
| 3965 | fileNames.push_back(AUDIO_POLICY_A2DP_OFFLOAD_DISABLED_XML_CONFIG_FILE_NAME); |
| 3966 | } |
| 3967 | fileNames.push_back(AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3968 | |
| 3969 | for (const char* fileName : fileNames) { |
| 3970 | for (int i = 0; i < kConfigLocationListSize; i++) { |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3971 | snprintf(audioPolicyXmlConfigFile, sizeof(audioPolicyXmlConfigFile), |
| 3972 | "%s/%s", kConfigLocationList[i], fileName); |
Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 3973 | ret = deserializeAudioPolicyFile(audioPolicyXmlConfigFile, &config); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3974 | if (ret == NO_ERROR) { |
Mikhail Naganov | 2e5167e1 | 2018-04-19 13:41:22 -0700 | [diff] [blame] | 3975 | config.setSource(audioPolicyXmlConfigFile); |
Petri Gynther | f497f29 | 2018-04-17 18:46:10 -0700 | [diff] [blame] | 3976 | return ret; |
| 3977 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3978 | } |
| 3979 | } |
| 3980 | return ret; |
| 3981 | } |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3982 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3983 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface, |
| 3984 | bool /*forTesting*/) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3985 | : |
Andy Hung | 4ef19fa | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 3986 | 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] | 3987 | mpClientInterface(clientInterface), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3988 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3989 | mA2dpSuspended(false), |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 3990 | mVolumeCurves(new VolumeCurvesCollection()), |
| 3991 | mConfig(mHwModulesAll, mAvailableOutputDevices, mAvailableInputDevices, |
Mikhail Naganov | bcbcb1b | 2017-12-13 13:03:35 -0800 | [diff] [blame] | 3992 | mDefaultOutputDevice, static_cast<VolumeCurvesCollection*>(mVolumeCurves.get())), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3993 | mAudioPortGeneration(1), |
| 3994 | mBeaconMuteRefCount(0), |
| 3995 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3996 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3997 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3998 | mMasterMono(false), |
Eric Laurent | 4eb58f1 | 2018-12-07 16:41:02 -0800 | [diff] [blame] | 3999 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4000 | { |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4001 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4002 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4003 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
| 4004 | : AudioPolicyManager(clientInterface, false /*forTesting*/) |
| 4005 | { |
| 4006 | loadConfig(); |
| 4007 | initialize(); |
| 4008 | } |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4009 | |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 4010 | // This check is to catch any legacy platform updating to Q without having |
| 4011 | // switched to XML since its deprecation on O. |
| 4012 | // TODO: after Q release, remove this check and flag as XML is now the only |
| 4013 | // option and all legacy platform should have transitioned to XML. |
| 4014 | #ifndef USE_XML_AUDIO_POLICY_CONF |
| 4015 | #error Audio policy no longer supports legacy .conf configuration format |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4016 | #endif |
Kevin Rocard | dfa1e8a | 2018-10-26 16:42:07 -0700 | [diff] [blame] | 4017 | |
| 4018 | void AudioPolicyManager::loadConfig() { |
| 4019 | if (deserializeAudioPolicyXmlConfig(getConfig()) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4020 | ALOGE("could not load audio policy configuration file, setting defaults"); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4021 | getConfig().setDefault(); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4022 | } |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | status_t AudioPolicyManager::initialize() { |
| 4026 | mVolumeCurves->initializeVolumeCurves(getConfig().isSpeakerDrcEnabled()); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 4027 | |
| 4028 | // 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] | 4029 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 4030 | if (!engineInstance) { |
| 4031 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4032 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4033 | } |
| 4034 | // Retrieve the Policy Manager Interface |
| 4035 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 4036 | if (mEngine == NULL) { |
| 4037 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4038 | return NO_INIT; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4039 | } |
| 4040 | mEngine->setObserver(this); |
| 4041 | status_t status = mEngine->initCheck(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4042 | if (status != NO_ERROR) { |
| 4043 | LOG_FATAL("Policy engine not initialized(err=%d)", status); |
| 4044 | return status; |
| 4045 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4046 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4047 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4048 | // open all output streams needed to access attached devices |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4049 | for (const auto& hwModule : mHwModulesAll) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4050 | hwModule->setHandle(mpClientInterface->loadHwModule(hwModule->getName())); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4051 | if (hwModule->getHandle() == AUDIO_MODULE_HANDLE_NONE) { |
| 4052 | ALOGW("could not open HW module %s", hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4053 | continue; |
| 4054 | } |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4055 | mHwModules.push_back(hwModule); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4056 | // open all output streams needed to access attached devices |
| 4057 | // except for direct output streams that are only opened when they are actually |
| 4058 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4059 | // This also validates mAvailableOutputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4060 | for (const auto& outProfile : hwModule->getOutputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4061 | if (!outProfile->canOpenNewIo()) { |
| 4062 | ALOGE("Invalid Output profile max open count %u for profile %s", |
| 4063 | outProfile->maxOpenCount, outProfile->getTagName().c_str()); |
| 4064 | continue; |
| 4065 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4066 | if (!outProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4067 | ALOGW("Output profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4068 | continue; |
| 4069 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4070 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4071 | mTtsOutputAvailable = true; |
| 4072 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4073 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4074 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4075 | continue; |
| 4076 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4077 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 4078 | DeviceVector availProfileDevices = supportedDevices.filter(mAvailableOutputDevices); |
| 4079 | sp<DeviceDescriptor> supportedDevice = 0; |
| 4080 | if (supportedDevices.contains(mDefaultOutputDevice)) { |
| 4081 | supportedDevice = mDefaultOutputDevice; |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4082 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4083 | // choose first device present in profile's SupportedDevices also part of |
| 4084 | // mAvailableOutputDevices. |
| 4085 | if (availProfileDevices.isEmpty()) { |
| 4086 | continue; |
| 4087 | } |
| 4088 | supportedDevice = availProfileDevices.itemAt(0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4090 | if (!mAvailableOutputDevices.contains(supportedDevice)) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4091 | continue; |
| 4092 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4093 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 4094 | mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4095 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4096 | status_t status = outputDesc->open(nullptr, DeviceVector(supportedDevice), |
| 4097 | AUDIO_STREAM_DEFAULT, |
| 4098 | AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4099 | if (status != NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4100 | ALOGW("Cannot open output stream for devices %s on hw module %s", |
| 4101 | supportedDevice->toString().c_str(), hwModule->getName()); |
| 4102 | continue; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4103 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4104 | for (const auto &device : availProfileDevices) { |
| 4105 | // give a valid ID to an attached device once confirmed it is reachable |
| 4106 | if (!device->isAttached()) { |
| 4107 | device->attach(hwModule); |
| 4108 | } |
| 4109 | } |
| 4110 | if (mPrimaryOutput == 0 && |
| 4111 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 4112 | mPrimaryOutput = outputDesc; |
| 4113 | } |
| 4114 | addOutput(output, outputDesc); |
| 4115 | setOutputDevices(outputDesc, |
| 4116 | DeviceVector(supportedDevice), |
| 4117 | true, |
| 4118 | 0, |
| 4119 | NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4120 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4121 | // open input streams needed to access attached devices to validate |
| 4122 | // mAvailableInputDevices list |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4123 | for (const auto& inProfile : hwModule->getInputProfiles()) { |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4124 | if (!inProfile->canOpenNewIo()) { |
| 4125 | ALOGE("Invalid Input profile max open count %u for profile %s", |
| 4126 | inProfile->maxOpenCount, inProfile->getTagName().c_str()); |
| 4127 | continue; |
| 4128 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4129 | if (!inProfile->hasSupportedDevices()) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4130 | ALOGW("Input profile contains no device on module %s", hwModule->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4131 | continue; |
| 4132 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4133 | // chose first device present in profile's SupportedDevices also part of |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4134 | // available input devices |
| 4135 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 4136 | DeviceVector availProfileDevices = supportedDevices.filter(mAvailableInputDevices); |
| 4137 | if (availProfileDevices.isEmpty()) { |
| 4138 | ALOGE("%s: Input device list is empty!", __FUNCTION__); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4139 | continue; |
| 4140 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 4141 | sp<AudioInputDescriptor> inputDesc = |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4142 | new AudioInputDescriptor(inProfile, mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4143 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4144 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4145 | status_t status = inputDesc->open(nullptr, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4146 | availProfileDevices.itemAt(0), |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4147 | AUDIO_SOURCE_MIC, |
| 4148 | AUDIO_INPUT_FLAG_NONE, |
| 4149 | &input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4150 | if (status != NO_ERROR) { |
| 4151 | ALOGW("Cannot open input stream for device %s on hw module %s", |
| 4152 | availProfileDevices.toString().c_str(), |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4153 | hwModule->getName()); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4154 | continue; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 4155 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4156 | for (const auto &device : availProfileDevices) { |
| 4157 | // give a valid ID to an attached device once confirmed it is reachable |
| 4158 | if (!device->isAttached()) { |
| 4159 | device->attach(hwModule); |
| 4160 | device->importAudioPort(inProfile, true); |
| 4161 | } |
| 4162 | } |
| 4163 | inputDesc->close(); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | // make sure all attached devices have been allocated a unique ID |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4167 | auto checkAndSetAvailable = [this](auto& devices) { |
Eric Laurent | 7f0f431 | 2019-01-18 10:47:14 -0800 | [diff] [blame] | 4168 | for (size_t i = 0; i < devices.size();) { |
| 4169 | const auto &device = devices[i]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4170 | if (!device->isAttached()) { |
| 4171 | ALOGW("device %s is unreachable", device->toString().c_str()); |
| 4172 | devices.remove(device); |
| 4173 | continue; |
| 4174 | } |
| 4175 | // Device is now validated and can be appended to the available devices of the engine |
| 4176 | mEngine->setDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 7f0f431 | 2019-01-18 10:47:14 -0800 | [diff] [blame] | 4177 | i++; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4178 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4179 | }; |
| 4180 | checkAndSetAvailable(mAvailableOutputDevices); |
| 4181 | checkAndSetAvailable(mAvailableInputDevices); |
| 4182 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4183 | // make sure default device is reachable |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4184 | if (mDefaultOutputDevice == 0 || !mAvailableOutputDevices.contains(mDefaultOutputDevice)) { |
| 4185 | ALOGE_IF(mDefaultOutputDevice != 0, "Default device %s is unreachable", |
| 4186 | mDefaultOutputDevice->toString().c_str()); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4187 | status = NO_INIT; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4188 | } |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4189 | // If microphones address is empty, set it according to device type |
| 4190 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
François Gaffie | aca677c | 2018-05-03 10:47:50 +0200 | [diff] [blame] | 4191 | if (mAvailableInputDevices[i]->address().isEmpty()) { |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4192 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BUILTIN_MIC) { |
jiabin | 20729a7 | 2019-01-04 16:01:55 -0800 | [diff] [blame] | 4193 | mAvailableInputDevices[i]->setAddress(String8(AUDIO_BOTTOM_MICROPHONE_ADDRESS)); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4194 | } else if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_BACK_MIC) { |
jiabin | 20729a7 | 2019-01-04 16:01:55 -0800 | [diff] [blame] | 4195 | mAvailableInputDevices[i]->setAddress(String8(AUDIO_BACK_MICROPHONE_ADDRESS)); |
jiabin | 9ff780e | 2018-03-19 18:19:52 -0700 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4199 | |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4200 | if (mPrimaryOutput == 0) { |
| 4201 | ALOGE("Failed to open primary output"); |
| 4202 | status = NO_INIT; |
| 4203 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4204 | |
Tomoharu Kasahara | 71c9091 | 2018-10-31 09:10:12 +0900 | [diff] [blame] | 4205 | // Silence ALOGV statements |
| 4206 | property_set("log.tag." LOG_TAG, "D"); |
| 4207 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4208 | updateDevicesAndOutputs(); |
Mikhail Naganov | ad3f8a1 | 2017-12-12 13:24:23 -0800 | [diff] [blame] | 4209 | return status; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4210 | } |
| 4211 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4212 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4213 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4214 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4215 | mOutputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4216 | } |
| 4217 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4218 | mInputs.valueAt(i)->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4219 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4220 | mAvailableOutputDevices.clear(); |
| 4221 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4222 | mOutputs.clear(); |
| 4223 | mInputs.clear(); |
| 4224 | mHwModules.clear(); |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4225 | mHwModulesAll.clear(); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 4226 | mManualSurroundFormats.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4227 | } |
| 4228 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4229 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4230 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4231 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4232 | } |
| 4233 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4234 | // --- |
| 4235 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4236 | void AudioPolicyManager::addOutput(audio_io_handle_t output, |
| 4237 | const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4238 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4239 | mOutputs.add(output, outputDesc); |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4240 | applyStreamVolumes(outputDesc, AUDIO_DEVICE_NONE, 0 /* delayMs */, true /* force */); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 4241 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4242 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4243 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4244 | } |
| 4245 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4246 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 4247 | { |
| 4248 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4249 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
Eric Laurent | 98e3819 | 2018-02-15 18:31:53 -0800 | [diff] [blame] | 4252 | void AudioPolicyManager::addInput(audio_io_handle_t input, |
| 4253 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4254 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4255 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4256 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4257 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4258 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4259 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4260 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4261 | SortedVector<audio_io_handle_t>& outputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4262 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4263 | audio_devices_t deviceType = device->type(); |
| 4264 | const String8 &address = device->address(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4265 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4266 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4267 | if (audio_device_is_digital(deviceType)) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4268 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4269 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4270 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4271 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4272 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4273 | // first list already open outputs that can be routed to this device |
| 4274 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4275 | desc = mOutputs.valueAt(i); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4276 | if (!desc->isDuplicated() && desc->supportsDevice(device) |
| 4277 | && desc->deviceSupportsEncodedFormats(deviceType)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4278 | ALOGV("checkOutputsForDevice(): adding opened output %d on device %s", |
| 4279 | mOutputs.keyAt(i), device->toString().c_str()); |
| 4280 | outputs.add(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4281 | } |
| 4282 | } |
| 4283 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4284 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4285 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4286 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4287 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4288 | if (profile->supportsDevice(device)) { |
| 4289 | profiles.add(profile); |
| 4290 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %s", |
| 4291 | j, hwModule->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4292 | } |
| 4293 | } |
| 4294 | } |
| 4295 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4296 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4297 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4298 | if (profiles.isEmpty() && outputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4299 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4300 | return BAD_VALUE; |
| 4301 | } |
| 4302 | |
| 4303 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4304 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4305 | 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] | 4306 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4307 | |
| 4308 | // nothing to do if one output is already opened for this profile |
| 4309 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4310 | for (j = 0; j < outputs.size(); j++) { |
| 4311 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4313 | // matching profile: save the sample rates, format and channel masks supported |
| 4314 | // by the profile in our device descriptor |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4315 | if (audio_device_is_digital(deviceType)) { |
| 4316 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4317 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4318 | break; |
| 4319 | } |
| 4320 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4321 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4322 | continue; |
| 4323 | } |
| 4324 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4325 | if (!profile->canOpenNewIo()) { |
| 4326 | ALOGW("Max Output number %u already opened for this profile %s", |
| 4327 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4328 | continue; |
| 4329 | } |
| 4330 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4331 | 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] | 4332 | deviceType, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4333 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4334 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4335 | status_t status = desc->open(nullptr, DeviceVector(device), |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4336 | AUDIO_STREAM_DEFAULT, AUDIO_OUTPUT_FLAG_NONE, &output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4337 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4338 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4339 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4340 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4341 | char *param = audio_device_address_to_parameter(deviceType, address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4342 | mpClientInterface->setParameters(output, String8(param)); |
| 4343 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4345 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4346 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4347 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4348 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4349 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4350 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4351 | desc->close(); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4352 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4353 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4354 | profile->pickAudioProfile( |
| 4355 | config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4356 | config.offload_info.sample_rate = config.sample_rate; |
| 4357 | config.offload_info.channel_mask = config.channel_mask; |
| 4358 | config.offload_info.format = config.format; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4359 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4360 | status_t status = desc->open(&config, DeviceVector(device), |
| 4361 | AUDIO_STREAM_DEFAULT, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4362 | AUDIO_OUTPUT_FLAG_NONE, &output); |
| 4363 | if (status != NO_ERROR) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4364 | output = AUDIO_IO_HANDLE_NONE; |
| 4365 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4366 | } |
| 4367 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4368 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4369 | addOutput(output, desc); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4370 | if (device_distinguishes_on_address(deviceType) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4371 | sp<AudioPolicyMix> policyMix; |
François Gaffie | b141c52 | 2018-03-12 11:47:40 +0100 | [diff] [blame] | 4372 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) == NO_ERROR) { |
| 4373 | policyMix->setOutput(desc); |
| 4374 | desc->mPolicyMix = policyMix->getMix(); |
| 4375 | } else { |
| 4376 | ALOGW("checkOutputsForDevice() cannot find policy for address %s", |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4377 | address.string()); |
| 4378 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4379 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4380 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4381 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4382 | // no duplicated output for direct outputs and |
| 4383 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4384 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4386 | //TODO: configure audio effect output stage here |
| 4387 | |
| 4388 | // 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] | 4389 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4390 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4391 | status_t status = dupOutputDesc->openDuplicating(mPrimaryOutput, desc, |
| 4392 | &duplicatedOutput); |
| 4393 | if (status == NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4394 | // add duplicated output descriptor |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4395 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4396 | } else { |
| 4397 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4398 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4399 | desc->close(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4400 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4401 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4402 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4403 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4404 | } |
| 4405 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4406 | } else { |
| 4407 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4408 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4409 | if (output == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4410 | ALOGW("checkOutputsForDevice() could not open output for device %x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4411 | profiles.removeAt(profile_index); |
| 4412 | profile_index--; |
| 4413 | } else { |
| 4414 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4415 | // Load digital format info only for digital devices |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4416 | if (audio_device_is_digital(deviceType)) { |
| 4417 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4418 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4419 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4420 | if (device_distinguishes_on_address(deviceType)) { |
| 4421 | ALOGV("checkOutputsForDevice(): setOutputDevices %s", |
| 4422 | device->toString().c_str()); |
| 4423 | setOutputDevices(desc, DeviceVector(device), true/*force*/, 0/*delay*/, |
| 4424 | NULL/*patch handle*/); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4425 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4426 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4427 | } |
| 4428 | } |
| 4429 | |
| 4430 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4431 | ALOGW("checkOutputsForDevice(): No output available for device %04x", deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4432 | return BAD_VALUE; |
| 4433 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4434 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4435 | // check if one opened output is not needed any more after disconnecting one device |
| 4436 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4437 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4438 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4439 | // exact match on device |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4440 | if (device_distinguishes_on_address(deviceType) && desc->supportsDevice(device) |
| 4441 | && desc->deviceSupportsEncodedFormats(deviceType)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4442 | outputs.add(mOutputs.keyAt(i)); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4443 | } else if (!mAvailableOutputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4444 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4445 | mOutputs.keyAt(i)); |
| 4446 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4447 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4448 | } |
| 4449 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4450 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4451 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4452 | for (size_t j = 0; j < hwModule->getOutputProfiles().size(); j++) { |
| 4453 | sp<IOProfile> profile = hwModule->getOutputProfiles()[j]; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4454 | if (profile->supportsDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4455 | ALOGV("checkOutputsForDevice(): " |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4456 | "clearing direct output profile %zu on module %s", |
| 4457 | j, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4458 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | } |
| 4462 | } |
| 4463 | return NO_ERROR; |
| 4464 | } |
| 4465 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4466 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4467 | audio_policy_dev_state_t state, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4468 | SortedVector<audio_io_handle_t>& inputs) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4469 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4470 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4471 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4472 | if (audio_device_is_digital(device->type())) { |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4473 | // erase all current sample rates, formats and channel masks |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4474 | device->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4477 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4478 | // first list already open inputs that can be routed to this device |
| 4479 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4480 | desc = mInputs.valueAt(input_index); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4481 | if (desc->mProfile->supportsDeviceTypes(device->type())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4482 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4483 | inputs.add(mInputs.keyAt(input_index)); |
| 4484 | } |
| 4485 | } |
| 4486 | |
| 4487 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4488 | SortedVector< sp<IOProfile> > profiles; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4489 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4490 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4491 | profile_index < hwModule->getInputProfiles().size(); |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 4492 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4493 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4494 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4495 | if (profile->supportsDevice(device)) { |
| 4496 | profiles.add(profile); |
| 4497 | ALOGV("checkInputsForDevice(): adding profile %zu from module %s", |
| 4498 | profile_index, hwModule->getName()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4499 | } |
| 4500 | } |
| 4501 | } |
| 4502 | |
| 4503 | if (profiles.isEmpty() && inputs.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4504 | 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] | 4505 | return BAD_VALUE; |
| 4506 | } |
| 4507 | |
| 4508 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4509 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4510 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4511 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4512 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4513 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4514 | // nothing to do if one input is already opened for this profile |
| 4515 | size_t input_index; |
| 4516 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4517 | desc = mInputs.valueAt(input_index); |
| 4518 | if (desc->mProfile == profile) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4519 | if (audio_device_is_digital(device->type())) { |
| 4520 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4521 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4522 | break; |
| 4523 | } |
| 4524 | } |
| 4525 | if (input_index != mInputs.size()) { |
| 4526 | continue; |
| 4527 | } |
| 4528 | |
Eric Laurent | 3974e3b | 2017-12-07 17:58:43 -0800 | [diff] [blame] | 4529 | if (!profile->canOpenNewIo()) { |
| 4530 | ALOGW("Max Input number %u already opened for this profile %s", |
| 4531 | profile->maxOpenCount, profile->getTagName().c_str()); |
| 4532 | continue; |
| 4533 | } |
| 4534 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4535 | desc = new AudioInputDescriptor(profile, mpClientInterface); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4536 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4537 | status_t status = desc->open(nullptr, |
| 4538 | device, |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4539 | AUDIO_SOURCE_MIC, |
| 4540 | AUDIO_INPUT_FLAG_NONE, |
| 4541 | &input); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4542 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4543 | if (status == NO_ERROR) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4544 | const String8& address = device->address(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4545 | if (!address.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4546 | char *param = audio_device_address_to_parameter(device->type(), address); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4547 | mpClientInterface->setParameters(input, String8(param)); |
| 4548 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4549 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4550 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4551 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4552 | ALOGW("checkInputsForDevice() direct input missing param"); |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4553 | desc->close(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4554 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | if (input != 0) { |
| 4558 | addInput(input, desc); |
| 4559 | } |
| 4560 | } // endif input != 0 |
| 4561 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4562 | if (input == AUDIO_IO_HANDLE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4563 | ALOGW("%s could not open input for device %s", __func__, |
| 4564 | device->toString().c_str()); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4565 | profiles.removeAt(profile_index); |
| 4566 | profile_index--; |
| 4567 | } else { |
| 4568 | inputs.add(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4569 | if (audio_device_is_digital(device->type())) { |
| 4570 | device->importAudioPort(profile); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4571 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4572 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4573 | } |
| 4574 | } // end scan profiles |
| 4575 | |
| 4576 | if (profiles.isEmpty()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4577 | 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] | 4578 | return BAD_VALUE; |
| 4579 | } |
| 4580 | } else { |
| 4581 | // Disconnect |
| 4582 | // check if one opened input is not needed any more after disconnecting one device |
| 4583 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4584 | desc = mInputs.valueAt(input_index); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4585 | if (!mAvailableInputDevices.containsAtLeastOne(desc->supportedDevices())) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4586 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4587 | mInputs.keyAt(input_index)); |
| 4588 | inputs.add(mInputs.keyAt(input_index)); |
| 4589 | } |
| 4590 | } |
| 4591 | // Clear any profiles associated with the disconnected device. |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4592 | for (const auto& hwModule : mHwModules) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4593 | for (size_t profile_index = 0; |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4594 | profile_index < hwModule->getInputProfiles().size(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4595 | profile_index++) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 4596 | sp<IOProfile> profile = hwModule->getInputProfiles()[profile_index]; |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4597 | if (profile->supportsDevice(device)) { |
Mikhail Naganov | d412014 | 2017-12-06 15:49:22 -0800 | [diff] [blame] | 4598 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %s", |
| 4599 | profile_index, hwModule->getName()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4600 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4601 | } |
| 4602 | } |
| 4603 | } |
| 4604 | } // end disconnect |
| 4605 | |
| 4606 | return NO_ERROR; |
| 4607 | } |
| 4608 | |
| 4609 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4610 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4611 | { |
| 4612 | ALOGV("closeOutput(%d)", output); |
| 4613 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4614 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4615 | if (outputDesc == NULL) { |
| 4616 | ALOGW("closeOutput() unknown output %d", output); |
| 4617 | return; |
| 4618 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4619 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4620 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4621 | // look for duplicated outputs connected to the output being removed. |
| 4622 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4623 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | if (dupOutputDesc->isDuplicated() && |
| 4625 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4626 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4627 | sp<SwAudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4628 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4629 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4630 | } else { |
| 4631 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4632 | } |
| 4633 | // As all active tracks on duplicated output will be deleted, |
| 4634 | // and as they were also referenced on the other output, the reference |
| 4635 | // count for their stream type must be adjusted accordingly on |
| 4636 | // the other output. |
Andy Hung | af036da | 2018-09-21 10:46:21 -0700 | [diff] [blame] | 4637 | const bool wasActive = outputDesc2->isActive(); |
| 4638 | for (const auto &clientPair : dupOutputDesc->getActiveClients()) { |
| 4639 | outputDesc2->changeStreamActiveCount(clientPair.first, -clientPair.second); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4640 | } |
Eric Laurent | 733ce94 | 2017-12-07 12:18:25 -0800 | [diff] [blame] | 4641 | // stop() will be a no op if the output is still active but is needed in case all |
| 4642 | // active streams refcounts where cleared above |
| 4643 | if (wasActive) { |
| 4644 | outputDesc2->stop(); |
| 4645 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4647 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4648 | |
| 4649 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4650 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4651 | } |
| 4652 | } |
| 4653 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4654 | nextAudioPortGeneration(); |
| 4655 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4656 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4657 | if (index >= 0) { |
| 4658 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4659 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4660 | mAudioPatches.removeItemsAt(index); |
| 4661 | mpClientInterface->onAudioPatchListUpdate(); |
| 4662 | } |
| 4663 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4664 | outputDesc->close(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4665 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4666 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4667 | mPreviousOutputs = mOutputs; |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4668 | |
| 4669 | // MSD patches may have been released to support a non-MSD direct output. Reset MSD patch if |
| 4670 | // no direct outputs are open. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4671 | if (!getMsdAudioOutDevices().isEmpty()) { |
Dean Wheatley | 3023b38 | 2018-08-09 07:42:40 +1000 | [diff] [blame] | 4672 | bool directOutputOpen = false; |
| 4673 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4674 | if (mOutputs[i]->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 4675 | directOutputOpen = true; |
| 4676 | break; |
| 4677 | } |
| 4678 | } |
| 4679 | if (!directOutputOpen) { |
| 4680 | ALOGV("no direct outputs open, reset MSD patch"); |
| 4681 | setMsdPatch(); |
| 4682 | } |
| 4683 | } |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4687 | { |
| 4688 | ALOGV("closeInput(%d)", input); |
| 4689 | |
| 4690 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4691 | if (inputDesc == NULL) { |
| 4692 | ALOGW("closeInput() unknown input %d", input); |
| 4693 | return; |
| 4694 | } |
| 4695 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4696 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4697 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4698 | sp<DeviceDescriptor> device = inputDesc->getDevice(); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4699 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4700 | if (index >= 0) { |
| 4701 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4702 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4703 | mAudioPatches.removeItemsAt(index); |
| 4704 | mpClientInterface->onAudioPatchListUpdate(); |
| 4705 | } |
| 4706 | |
Eric Laurent | fe23112 | 2017-11-17 17:48:06 -0800 | [diff] [blame] | 4707 | inputDesc->close(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4708 | mInputs.removeItem(input); |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4709 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4710 | DeviceVector primaryInputDevices = availablePrimaryModuleInputDevices(); |
| 4711 | if (primaryInputDevices.contains(device) && |
Haynes Mathew George | 1d539d9 | 2018-03-16 11:40:49 -0700 | [diff] [blame] | 4712 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
| 4713 | SoundTrigger::setCaptureState(false); |
| 4714 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4715 | } |
| 4716 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4717 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevices( |
| 4718 | const DeviceVector &devices, |
| 4719 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4720 | { |
| 4721 | SortedVector<audio_io_handle_t> outputs; |
| 4722 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4723 | ALOGVV("%s() devices %s", __func__, devices.toString().c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4724 | for (size_t i = 0; i < openOutputs.size(); i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4725 | ALOGVV("output %zu isDuplicated=%d device=%s", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4726 | i, openOutputs.valueAt(i)->isDuplicated(), |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4727 | openOutputs.valueAt(i)->supportedDevices().toString().c_str()); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4728 | if (openOutputs.valueAt(i)->supportsAllDevices(devices) |
| 4729 | && openOutputs.valueAt(i)->deviceSupportsEncodedFormats(devices.types())) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4730 | ALOGVV("%s() found output %d", __func__, openOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4731 | outputs.add(openOutputs.keyAt(i)); |
| 4732 | } |
| 4733 | } |
| 4734 | return outputs; |
| 4735 | } |
| 4736 | |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4737 | void AudioPolicyManager::checkForDeviceAndOutputChanges(std::function<bool()> onOutputsChecked) |
| 4738 | { |
| 4739 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 4740 | // output is suspended before any tracks are moved to it |
| 4741 | checkA2dpSuspend(); |
| 4742 | checkOutputForAllStrategies(); |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4743 | if (onOutputsChecked != nullptr && onOutputsChecked()) checkA2dpSuspend(); |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4744 | updateDevicesAndOutputs(); |
Mikhail Naganov | 7bd9b8d | 2018-11-15 02:09:03 +0000 | [diff] [blame] | 4745 | if (mHwModules.getModuleFromName(AUDIO_HARDWARE_MODULE_ID_MSD) != 0) { |
Mikhail Naganov | 15be9d2 | 2017-11-08 14:18:13 +1100 | [diff] [blame] | 4746 | setMsdPatch(); |
| 4747 | } |
Mikhail Naganov | 3797715 | 2018-07-11 15:54:44 -0700 | [diff] [blame] | 4748 | } |
| 4749 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4750 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4751 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4752 | DeviceVector oldDevices = getDevicesForStrategy(strategy, true /*fromCache*/); |
| 4753 | DeviceVector newDevices = getDevicesForStrategy(strategy, false /*fromCache*/); |
| 4754 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevices(oldDevices, mPreviousOutputs); |
| 4755 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevices(newDevices, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4756 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4757 | // also take into account external policy-related changes: add all outputs which are |
| 4758 | // associated with policies in the "before" and "after" output vectors |
| 4759 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4760 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4761 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4762 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4763 | srcOutputs.add(desc->mIoHandle); |
| 4764 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4765 | } |
| 4766 | } |
| 4767 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4768 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4769 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4770 | dstOutputs.add(desc->mIoHandle); |
| 4771 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4772 | } |
| 4773 | } |
| 4774 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 4775 | if (!dstOutputs.isEmpty() && srcOutputs != dstOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4776 | // get maximum latency of all source outputs to determine the minimum mute time guaranteeing |
| 4777 | // audio from invalidated tracks will be rendered when unmuting |
| 4778 | uint32_t maxLatency = 0; |
| 4779 | for (audio_io_handle_t srcOut : srcOutputs) { |
| 4780 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4781 | if (desc != 0 && maxLatency < desc->latency()) { |
| 4782 | maxLatency = desc->latency(); |
| 4783 | } |
| 4784 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 4785 | ALOGV_IF(!(srcOutputs.isEmpty() || dstOutputs.isEmpty()), |
| 4786 | "%s: strategy %d, moving from output %s to output %s", __func__, strategy, |
| 4787 | std::to_string(srcOutputs[0]).c_str(), |
| 4788 | std::to_string(dstOutputs[0]).c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4789 | // mute strategy while moving tracks from one output to another |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4790 | for (audio_io_handle_t srcOut : srcOutputs) { |
Eric Laurent | ac3a690 | 2018-05-11 16:39:10 -0700 | [diff] [blame] | 4791 | sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueFor(srcOut); |
| 4792 | if (desc != 0 && isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4793 | setStrategyMute(strategy, true, desc); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4794 | setStrategyMute(strategy, false, desc, maxLatency * LATENCY_MUTE_FACTOR, |
| 4795 | newDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4796 | } |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 4797 | sp<SourceClientDescriptor> source = |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 4798 | getSourceForStrategyOnOutput(srcOut, strategy); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4799 | if (source != 0){ |
| 4800 | connectAudioSource(source); |
| 4801 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4802 | } |
| 4803 | |
| 4804 | // Move effects associated to this strategy from previous output to new output |
| 4805 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4806 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4807 | } |
| 4808 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4809 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4810 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4811 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | } |
| 4816 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4817 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4818 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4819 | 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] | 4820 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4821 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4822 | 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] | 4823 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4824 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4825 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4826 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4827 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4828 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4829 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | } |
| 4831 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4832 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4833 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4834 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Aniket Kumar Lata | a8ee996 | 2018-01-31 20:24:23 -0800 | [diff] [blame] | 4835 | if (a2dpOutput == 0 || mOutputs.isA2dpOffloadedOnPrimary()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4836 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4837 | return; |
| 4838 | } |
| 4839 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4840 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4841 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4842 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4843 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4844 | |
| 4845 | // if suspended, restore A2DP output if: |
| 4846 | // ((SCO device is NOT connected) || |
| 4847 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4848 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4849 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4850 | // if not suspended, suspend A2DP output if: |
| 4851 | // (SCO device is connected) && |
| 4852 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4853 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4854 | // |
| 4855 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4856 | if (!isScoConnected || |
| 4857 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4858 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4859 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4860 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4861 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4862 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4863 | |
| 4864 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4865 | mA2dpSuspended = false; |
| 4866 | } |
| 4867 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4868 | if (isScoConnected && |
| 4869 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4870 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4871 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4872 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4873 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4874 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4875 | |
| 4876 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4877 | mA2dpSuspended = true; |
| 4878 | } |
| 4879 | } |
| 4880 | } |
| 4881 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4882 | template <class IoDescriptor, class Filter> |
| 4883 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4884 | IoDescriptor& desc, Filter filter, bool& active, const DeviceVector& devices) |
| 4885 | { |
| 4886 | auto activeClients = desc->clientsList(true /*activeOnly*/); |
| 4887 | auto activeClientsWithRoute = |
| 4888 | desc->clientsList(true /*activeOnly*/, filter, true /*preferredDevice*/); |
| 4889 | active = activeClients.size() > 0; |
| 4890 | if (active && activeClients.size() == activeClientsWithRoute.size()) { |
| 4891 | return devices.getDeviceFromId(activeClientsWithRoute[0]->preferredDeviceId()); |
| 4892 | } |
| 4893 | return nullptr; |
| 4894 | } |
| 4895 | |
| 4896 | template <class IoCollection, class Filter> |
| 4897 | sp<DeviceDescriptor> AudioPolicyManager::findPreferredDevice( |
| 4898 | IoCollection& ioCollection, Filter filter, const DeviceVector& devices) |
| 4899 | { |
| 4900 | sp<DeviceDescriptor> device; |
| 4901 | for (size_t i = 0; i < ioCollection.size(); i++) { |
| 4902 | auto desc = ioCollection.valueAt(i); |
| 4903 | bool active; |
| 4904 | sp<DeviceDescriptor> curDevice = findPreferredDevice(desc, filter, active, devices); |
| 4905 | if (active && curDevice == nullptr) { |
| 4906 | return nullptr; |
| 4907 | } else if (curDevice != nullptr) { |
| 4908 | device = curDevice; |
| 4909 | } |
| 4910 | } |
| 4911 | return device; |
| 4912 | } |
| 4913 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4914 | DeviceVector AudioPolicyManager::getNewOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 4915 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4916 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4917 | DeviceVector devices; |
| 4918 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4919 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4920 | if (index >= 0) { |
| 4921 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4922 | if (patchDesc->mUid != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4923 | ALOGV("%s device %s forced by patch %d", __func__, |
| 4924 | outputDesc->devices().toString().c_str(), outputDesc->getPatchHandle()); |
| 4925 | return outputDesc->devices(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4926 | } |
| 4927 | } |
| 4928 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4929 | // Honor explicit routing requests only if no client using default routing is active on this |
| 4930 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 4931 | bool active; // unused |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4932 | sp<DeviceDescriptor> device = |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 4933 | findPreferredDevice(outputDesc, STRATEGY_NONE, active, mAvailableOutputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4934 | if (device != nullptr) { |
| 4935 | return DeviceVector(device); |
Eric Laurent | f3a5a60 | 2018-05-22 18:42:55 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4938 | // 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] | 4939 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4940 | // use device for strategy enforced audible |
| 4941 | // 2: we are in call or the strategy phone is active on the output: |
| 4942 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4943 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4944 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4945 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4946 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4947 | // 5: the strategy accessibility is active on the output: |
| 4948 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4949 | // 6: the strategy "respectful" sonification is active on the output: |
| 4950 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4951 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4952 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4953 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4955 | // 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] | 4956 | // use device for strategy t-t-s |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4957 | |
| 4958 | // FIXME: extend use of isStrategyActiveOnSameModule() to all strategies |
| 4959 | // with a refined rule considering mutually exclusive devices (using same backend) |
| 4960 | // as opposed to all streams on the same audio HAL module. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4961 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4962 | 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] | 4963 | devices = getDevicesForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | } else if (isInCall() || |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 4965 | isStrategyActiveOnSameModule(outputDesc, STRATEGY_PHONE)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4966 | devices = getDevicesForStrategy(STRATEGY_PHONE, fromCache); |
Yung Ti Su | 3e7eb5e | 2018-06-13 11:48:42 +0800 | [diff] [blame] | 4967 | } else if (isStrategyActiveOnSameModule(outputDesc, STRATEGY_SONIFICATION)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4968 | devices = getDevicesForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4969 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4970 | devices = getDevicesForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4971 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4972 | devices = getDevicesForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4973 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4974 | devices = getDevicesForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4975 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4976 | devices = getDevicesForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4977 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4978 | devices = getDevicesForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4979 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4980 | devices = getDevicesForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4981 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4982 | devices = getDevicesForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4983 | } |
| 4984 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4985 | ALOGV("getNewOutputDevice() selected devices %s", devices.toString().c_str()); |
| 4986 | return devices; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4987 | } |
| 4988 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4989 | sp<DeviceDescriptor> AudioPolicyManager::getNewInputDevice( |
| 4990 | const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4991 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4992 | sp<DeviceDescriptor> device; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4993 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4994 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4995 | if (index >= 0) { |
| 4996 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4997 | if (patchDesc->mUid != mUidCached) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 4998 | ALOGV("getNewInputDevice() device %s forced by patch %d", |
| 4999 | inputDesc->getDevice()->toString().c_str(), inputDesc->getPatchHandle()); |
| 5000 | return inputDesc->getDevice(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5001 | } |
| 5002 | } |
| 5003 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5004 | // Honor explicit routing requests only if no client using default routing is active on this |
| 5005 | // input: a specific app can not force routing for other apps by setting a preferred device. |
| 5006 | bool active; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5007 | device = findPreferredDevice(inputDesc, AUDIO_SOURCE_DEFAULT, active, mAvailableInputDevices); |
| 5008 | if (device != nullptr) { |
| 5009 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5010 | } |
| 5011 | |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5012 | // If we are not in call and no client is active on this input, this methods returns |
| 5013 | // 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] | 5014 | audio_attributes_t attributes = inputDesc->getHighestPriorityAttributes(); |
| 5015 | if (attributes.source == AUDIO_SOURCE_DEFAULT && isInCall()) { |
| 5016 | attributes.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
Eric Laurent | dc95a25 | 2018-04-12 12:46:56 -0700 | [diff] [blame] | 5017 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5018 | if (attributes.source != AUDIO_SOURCE_DEFAULT) { |
| 5019 | device = getDeviceAndMixForAttributes(attributes); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 5020 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5021 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5022 | return device; |
| 5023 | } |
| 5024 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5025 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 5026 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 5027 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5028 | } |
| 5029 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5030 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5031 | return (uint32_t)getStrategy(stream); |
| 5032 | } |
| 5033 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5034 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5035 | // By checking the range of stream before calling getStrategy, we avoid |
| 5036 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 5037 | // 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] | 5038 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5039 | return AUDIO_DEVICE_NONE; |
| 5040 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5041 | DeviceVector activeDevices; |
| 5042 | DeviceVector devices; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5043 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 5044 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5045 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5046 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5047 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5048 | DeviceVector curDevices = |
| 5049 | getDevicesForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
| 5050 | devices.merge(curDevices); |
| 5051 | for (audio_io_handle_t output : getOutputsForDevices(curDevices, mOutputs)) { |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5052 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5053 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5054 | activeDevices.merge(outputDesc->devices()); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 5055 | } |
| 5056 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5057 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5058 | |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 5059 | // Favor devices selected on active streams if any to report correct device in case of |
| 5060 | // explicit device selection |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5061 | if (!activeDevices.isEmpty()) { |
Eric Laurent | b0688d6 | 2018-08-14 15:49:18 -0700 | [diff] [blame] | 5062 | devices = activeDevices; |
| 5063 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5064 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 5065 | and doesn't really need to.*/ |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5066 | DeviceVector speakerSafeDevices = devices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER_SAFE); |
| 5067 | if (!speakerSafeDevices.isEmpty()) { |
| 5068 | devices.merge(mAvailableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER)); |
| 5069 | devices.remove(speakerSafeDevices); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 5070 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5071 | return devices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5074 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 5075 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5076 | 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] | 5077 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5078 | } |
| 5079 | |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5080 | routing_strategy AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5081 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5082 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5083 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5084 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5085 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5086 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5087 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5088 | // usage to strategy mapping |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5089 | return mEngine->getStrategyForUsage(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5090 | } |
| 5091 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5092 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5093 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5094 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5095 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 5096 | updateDevicesAndOutputs(); |
| 5097 | break; |
| 5098 | default: |
| 5099 | break; |
| 5100 | } |
| 5101 | } |
| 5102 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5103 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 5104 | |
| 5105 | // skip beacon mute management if a dedicated TTS output is available |
| 5106 | if (mTtsOutputAvailable) { |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5110 | switch(event) { |
| 5111 | case STARTING_OUTPUT: |
| 5112 | mBeaconMuteRefCount++; |
| 5113 | break; |
| 5114 | case STOPPING_OUTPUT: |
| 5115 | if (mBeaconMuteRefCount > 0) { |
| 5116 | mBeaconMuteRefCount--; |
| 5117 | } |
| 5118 | break; |
| 5119 | case STARTING_BEACON: |
| 5120 | mBeaconPlayingRefCount++; |
| 5121 | break; |
| 5122 | case STOPPING_BEACON: |
| 5123 | if (mBeaconPlayingRefCount > 0) { |
| 5124 | mBeaconPlayingRefCount--; |
| 5125 | } |
| 5126 | break; |
| 5127 | } |
| 5128 | |
| 5129 | if (mBeaconMuteRefCount > 0) { |
| 5130 | // any playback causes beacon to be muted |
| 5131 | return setBeaconMute(true); |
| 5132 | } else { |
| 5133 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 5134 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 5139 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 5140 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 5141 | // keep track of muted state to avoid repeating mute/unmute operations |
| 5142 | if (mBeaconMuted != mute) { |
| 5143 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 5144 | ALOGV("\t muting %d", mute); |
| 5145 | uint32_t maxLatency = 0; |
| 5146 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5147 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5148 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5149 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 5150 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 5151 | const uint32_t latency = desc->latency() * 2; |
| 5152 | if (latency > maxLatency) { |
| 5153 | maxLatency = latency; |
| 5154 | } |
| 5155 | } |
| 5156 | mBeaconMuted = mute; |
| 5157 | return maxLatency; |
| 5158 | } |
| 5159 | return 0; |
| 5160 | } |
| 5161 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5162 | DeviceVector AudioPolicyManager::getDevicesForStrategy(routing_strategy strategy, bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5163 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5164 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5165 | // case the last active client route is used |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5166 | sp<DeviceDescriptor> device = findPreferredDevice(mOutputs, strategy, mAvailableOutputDevices); |
| 5167 | if (device != nullptr) { |
| 5168 | return DeviceVector(device); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5169 | } |
| 5170 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5171 | if (fromCache) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5172 | ALOGVV("%s from cache strategy %d, device %s", __func__, strategy, |
| 5173 | mDevicesForStrategy[strategy].toString().c_str()); |
| 5174 | return mDevicesForStrategy[strategy]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5175 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5176 | return mAvailableOutputDevices.getDevicesFromTypeMask(mEngine->getDeviceForStrategy(strategy)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5177 | } |
| 5178 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5179 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5180 | { |
| 5181 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5182 | mDevicesForStrategy[i] = getDevicesForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5183 | } |
| 5184 | mPreviousOutputs = mOutputs; |
| 5185 | } |
| 5186 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5187 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5188 | audio_devices_t prevDeviceType, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5189 | uint32_t delayMs) |
| 5190 | { |
| 5191 | // mute/unmute strategies using an incompatible device combination |
| 5192 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 5193 | // if unmuting, unmute only after the specified delay |
| 5194 | if (outputDesc->isDuplicated()) { |
| 5195 | return 0; |
| 5196 | } |
| 5197 | |
| 5198 | uint32_t muteWaitMs = 0; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5199 | audio_devices_t deviceType = outputDesc->devices().types(); |
| 5200 | bool shouldMute = outputDesc->isActive() && (popcount(deviceType) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5201 | |
| 5202 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5203 | audio_devices_t curDeviceType = |
| 5204 | getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 5205 | curDeviceType = curDeviceType & outputDesc->supportedDevices().types(); |
| 5206 | bool mute = shouldMute && (curDeviceType & deviceType) && (curDeviceType != deviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | bool doMute = false; |
| 5208 | |
| 5209 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 5210 | doMute = true; |
| 5211 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 5212 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 5213 | doMute = true; |
| 5214 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 5215 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5216 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5217 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5218 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5219 | // 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] | 5220 | if (!desc->supportedDevices().containsAtLeastOne(outputDesc->supportedDevices())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5221 | continue; |
| 5222 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 5223 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5224 | mute ? "muting" : "unmuting", i, curDeviceType); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5225 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5226 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5227 | if (mute) { |
| 5228 | // FIXME: should not need to double latency if volume could be applied |
| 5229 | // immediately by the audioflinger mixer. We must account for the delay |
| 5230 | // between now and the next time the audioflinger thread for this output |
| 5231 | // will process a buffer (which corresponds to one buffer size, |
| 5232 | // usually 1/2 or 1/4 of the latency). |
| 5233 | if (muteWaitMs < desc->latency() * 2) { |
| 5234 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5235 | } |
| 5236 | } |
| 5237 | } |
| 5238 | } |
| 5239 | } |
| 5240 | } |
| 5241 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5242 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 5243 | // different per device volumes |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5244 | if (outputDesc->isActive() && (deviceType != prevDeviceType)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5245 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 5246 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 5247 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 5248 | if (muteWaitMs < tempMuteWaitMs) { |
| 5249 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5250 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5251 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5252 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5253 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 5254 | // make sure that we do not start the temporary mute period too early in case of |
| 5255 | // delayed device change |
| 5256 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5257 | setStrategyMute((routing_strategy)i, false, outputDesc, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5258 | delayMs + tempMuteDurationMs, deviceType); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 5259 | } |
| 5260 | } |
| 5261 | } |
| 5262 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5263 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 5264 | if (muteWaitMs > delayMs) { |
| 5265 | muteWaitMs -= delayMs; |
| 5266 | usleep(muteWaitMs * 1000); |
| 5267 | return muteWaitMs; |
| 5268 | } |
| 5269 | return 0; |
| 5270 | } |
| 5271 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5272 | uint32_t AudioPolicyManager::setOutputDevices(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 5273 | const DeviceVector &devices, |
| 5274 | bool force, |
| 5275 | int delayMs, |
| 5276 | audio_patch_handle_t *patchHandle, |
| 5277 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5278 | { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5279 | ALOGV("%s device %s delayMs %d", __func__, devices.toString().c_str(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5280 | uint32_t muteWaitMs; |
| 5281 | |
| 5282 | if (outputDesc->isDuplicated()) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5283 | muteWaitMs = setOutputDevices(outputDesc->subOutput1(), devices, force, delayMs, |
| 5284 | nullptr /* patchHandle */, requiresMuteCheck); |
| 5285 | muteWaitMs += setOutputDevices(outputDesc->subOutput2(), devices, force, delayMs, |
| 5286 | nullptr /* patchHandle */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5287 | return muteWaitMs; |
| 5288 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5289 | |
| 5290 | // filter devices according to output selected |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5291 | DeviceVector filteredDevices = outputDesc->filterSupportedDevices(devices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5292 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5293 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5294 | // output profile |
| 5295 | if (!devices.isEmpty() && filteredDevices.isEmpty()) { |
| 5296 | ALOGV("%s: unsupported device %s for output", __func__, devices.toString().c_str()); |
| 5297 | return 0; |
| 5298 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5299 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5300 | DeviceVector prevDevices = outputDesc->devices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5301 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5302 | ALOGV("setOutputDevices() prevDevice %s", prevDevices.toString().c_str()); |
| 5303 | |
| 5304 | if (!filteredDevices.isEmpty()) { |
| 5305 | outputDesc->setDevices(filteredDevices); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5306 | } |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5307 | |
| 5308 | // if the outputs are not materially active, there is no need to mute. |
| 5309 | if (requiresMuteCheck) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5310 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevices.types(), delayMs); |
Jean-Michel Trivi | b3733cf | 2018-02-15 19:17:50 +0000 | [diff] [blame] | 5311 | } else { |
| 5312 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 5313 | muteWaitMs = 0; |
| 5314 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5315 | |
| 5316 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5317 | // the requested device is AUDIO_DEVICE_NONE |
| 5318 | // OR the requested device is the same as current device |
| 5319 | // AND force is not specified |
| 5320 | // AND the output is connected by a valid audio patch. |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5321 | // Doing this check here allows the caller to call setOutputDevices() without conditions |
| 5322 | if ((!filteredDevices.isEmpty() || filteredDevices == prevDevices) && |
| 5323 | !force && outputDesc->getPatchHandle() != 0) { |
| 5324 | ALOGV("%s setting same device %s or null device, force=%d, patch handle=%d", __func__, |
| 5325 | filteredDevices.toString().c_str(), force, outputDesc->getPatchHandle()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5326 | return muteWaitMs; |
| 5327 | } |
| 5328 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5329 | ALOGV("%s changing device to %s", __func__, filteredDevices.toString().c_str()); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5330 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5331 | // do the routing |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5332 | if (filteredDevices.isEmpty()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5333 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5334 | } else { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5335 | PatchBuilder patchBuilder; |
| 5336 | patchBuilder.addSource(outputDesc); |
| 5337 | ALOG_ASSERT(filteredDevices.size() <= AUDIO_PATCH_PORTS_MAX, "Too many sink ports"); |
| 5338 | for (const auto &filteredDevice : filteredDevices) { |
| 5339 | patchBuilder.addSink(filteredDevice); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5340 | } |
| 5341 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5342 | installPatch(__func__, patchHandle, outputDesc.get(), patchBuilder.patch(), delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5343 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5344 | |
| 5345 | // update stream volumes according to new device |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5346 | applyStreamVolumes(outputDesc, filteredDevices.types(), delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5347 | |
| 5348 | return muteWaitMs; |
| 5349 | } |
| 5350 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5351 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5352 | int delayMs, |
| 5353 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5354 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5355 | ssize_t index; |
| 5356 | if (patchHandle) { |
| 5357 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5358 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5359 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5360 | } |
| 5361 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5362 | return INVALID_OPERATION; |
| 5363 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5364 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5365 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5366 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5367 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5368 | removeAudioPatch(patchDesc->mHandle); |
| 5369 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5370 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5371 | return status; |
| 5372 | } |
| 5373 | |
| 5374 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5375 | const sp<DeviceDescriptor> &device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5376 | bool force, |
| 5377 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5378 | { |
| 5379 | status_t status = NO_ERROR; |
| 5380 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5381 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5382 | if ((device != nullptr) && ((device != inputDesc->getDevice()) || force)) { |
| 5383 | inputDesc->setDevice(device); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5384 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5385 | if (mAvailableInputDevices.contains(device)) { |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5386 | PatchBuilder patchBuilder; |
| 5387 | patchBuilder.addSink(inputDesc, |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5388 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5389 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5390 | [inputDesc](const PatchBuilder::mix_usecase_t& usecase) { |
| 5391 | auto result = usecase; |
| 5392 | if (result.source == AUDIO_SOURCE_HOTWORD && !inputDesc->isSoundTrigger()) { |
| 5393 | result.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5394 | } |
| 5395 | return result; }). |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5396 | //only one input device for now |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5397 | addSource(device); |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 5398 | status = installPatch(__func__, patchHandle, inputDesc.get(), patchBuilder.patch(), 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5399 | } |
| 5400 | } |
| 5401 | return status; |
| 5402 | } |
| 5403 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5404 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5405 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5406 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5407 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5408 | ssize_t index; |
| 5409 | if (patchHandle) { |
| 5410 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5411 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5412 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5413 | } |
| 5414 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5415 | return INVALID_OPERATION; |
| 5416 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5417 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5418 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5419 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5420 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5421 | removeAudioPatch(patchDesc->mHandle); |
| 5422 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5423 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5424 | return status; |
| 5425 | } |
| 5426 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5427 | sp<IOProfile> AudioPolicyManager::getInputProfile(const sp<DeviceDescriptor> &device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5428 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5429 | audio_format_t& format, |
| 5430 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5431 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5432 | { |
| 5433 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5434 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5435 | // |
| 5436 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5437 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5438 | |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5439 | sp<IOProfile> firstInexact; |
| 5440 | uint32_t updatedSamplingRate = 0; |
| 5441 | audio_format_t updatedFormat = AUDIO_FORMAT_INVALID; |
| 5442 | audio_channel_mask_t updatedChannelMask = AUDIO_CHANNEL_INVALID; |
Mikhail Naganov | 7e22e94 | 2017-12-07 10:04:29 -0800 | [diff] [blame] | 5443 | for (const auto& hwModule : mHwModules) { |
Mikhail Naganov | a5e165d | 2017-12-07 17:08:02 -0800 | [diff] [blame] | 5444 | for (const auto& profile : hwModule->getInputProfiles()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5445 | // profile->log(); |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5446 | //updatedFormat = format; |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5447 | if (profile->isCompatibleProfile(DeviceVector(device), samplingRate, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5448 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5449 | format, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5450 | &format, /*updatedFormat*/ |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5451 | channelMask, |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5452 | &channelMask /*updatedChannelMask*/, |
| 5453 | // FIXME ugly cast |
| 5454 | (audio_output_flags_t) flags, |
| 5455 | true /*exactMatchRequiredForInputFlags*/)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5456 | return profile; |
| 5457 | } |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5458 | if (firstInexact == nullptr && profile->isCompatibleProfile(DeviceVector(device), |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5459 | samplingRate, |
| 5460 | &updatedSamplingRate, |
| 5461 | format, |
| 5462 | &updatedFormat, |
| 5463 | channelMask, |
| 5464 | &updatedChannelMask, |
| 5465 | // FIXME ugly cast |
| 5466 | (audio_output_flags_t) flags, |
| 5467 | false /*exactMatchRequiredForInputFlags*/)) { |
| 5468 | firstInexact = profile; |
| 5469 | } |
| 5470 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5471 | } |
| 5472 | } |
Glenn Kasten | 730b926 | 2018-03-29 15:01:26 -0700 | [diff] [blame] | 5473 | if (firstInexact != nullptr) { |
| 5474 | samplingRate = updatedSamplingRate; |
| 5475 | format = updatedFormat; |
| 5476 | channelMask = updatedChannelMask; |
| 5477 | return firstInexact; |
| 5478 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5479 | return NULL; |
| 5480 | } |
| 5481 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5482 | sp<DeviceDescriptor> AudioPolicyManager::getDeviceAndMixForAttributes( |
| 5483 | const audio_attributes_t &attributes, AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5484 | { |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5485 | // Honor explicit routing requests only if all active clients have a preferred route in which |
| 5486 | // case the last active client route is used |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5487 | sp<DeviceDescriptor> device = |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5488 | findPreferredDevice(mInputs, attributes.source, mAvailableInputDevices); |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5489 | if (device != nullptr) { |
| 5490 | return device; |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5491 | } |
| 5492 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5493 | sp<DeviceDescriptor> selectedDeviceFromMix = |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5494 | mPolicyMixes.getDeviceAndMixForInputSource(attributes.source, mAvailableInputDevices, |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5495 | policyMix); |
| 5496 | return (selectedDeviceFromMix != nullptr) ? |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5497 | selectedDeviceFromMix : getDeviceForAttributes(attributes); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5498 | } |
| 5499 | |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5500 | sp<DeviceDescriptor> AudioPolicyManager::getDeviceForAttributes(const audio_attributes_t &attributes) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5501 | { |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5502 | audio_devices_t device = mEngine->getDeviceForInputSource(attributes.source); |
| 5503 | if (attributes.source == AUDIO_SOURCE_REMOTE_SUBMIX && |
| 5504 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0) { |
| 5505 | return mAvailableInputDevices.getDevice(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5506 | String8(attributes.tags + strlen("addr=")), |
| 5507 | AUDIO_FORMAT_DEFAULT); |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5508 | } |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 5509 | return mAvailableInputDevices.getDevice(device, String8(), AUDIO_FORMAT_DEFAULT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5510 | } |
| 5511 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5512 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5513 | int index, |
| 5514 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5515 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5516 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5517 | |
| 5518 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5519 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5520 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5521 | // the ringtone volume |
| 5522 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5523 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5524 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5525 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5526 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5527 | } |
| 5528 | |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5529 | // in-call: always cap volume by voice volume + some low headroom |
| 5530 | if ((stream != AUDIO_STREAM_VOICE_CALL) && |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5531 | (isInCall() || mOutputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL))) { |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5532 | switch (stream) { |
| 5533 | case AUDIO_STREAM_SYSTEM: |
| 5534 | case AUDIO_STREAM_RING: |
| 5535 | case AUDIO_STREAM_MUSIC: |
| 5536 | case AUDIO_STREAM_ALARM: |
| 5537 | case AUDIO_STREAM_NOTIFICATION: |
| 5538 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5539 | case AUDIO_STREAM_DTMF: |
| 5540 | case AUDIO_STREAM_ACCESSIBILITY: { |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5541 | int voiceVolumeIndex = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5542 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_VOICE_CALL, device); |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5543 | const float maxVoiceVolDb = |
Eric Laurent | dcd4ab1 | 2018-06-29 17:45:13 -0700 | [diff] [blame] | 5544 | computeVolume(AUDIO_STREAM_VOICE_CALL, voiceVolumeIndex, device) |
Eric Laurent | 7731b5a | 2018-04-06 15:47:22 -0700 | [diff] [blame] | 5545 | + IN_CALL_EARPIECE_HEADROOM_DB; |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5546 | if (volumeDB > maxVoiceVolDb) { |
| 5547 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5548 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5549 | volumeDB = maxVoiceVolDb; |
| 5550 | } |
| 5551 | } break; |
| 5552 | default: |
| 5553 | break; |
| 5554 | } |
| 5555 | } |
| 5556 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5557 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5558 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5559 | // - always attenuate notifications volume by 6dB |
| 5560 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5561 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5562 | // - if music is playing, always limit the volume to current music volume, |
| 5563 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5564 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5565 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5566 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5567 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5568 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Jakub Pawlowski | 00f928c | 2018-03-22 13:20:03 -0700 | [diff] [blame] | 5569 | AUDIO_DEVICE_OUT_USB_HEADSET | |
| 5570 | AUDIO_DEVICE_OUT_HEARING_AID)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5571 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5572 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5573 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5574 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5575 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5576 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5577 | // when the phone is ringing we must consider that music could have been paused just before |
| 5578 | // by the music application and behave as if music was active if the last music track was |
| 5579 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5580 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5581 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5582 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5583 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5584 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5585 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5586 | musicDevice), |
| 5587 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5588 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5589 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5590 | if (volumeDB > minVolDB) { |
| 5591 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5592 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5593 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5594 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5595 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5596 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5597 | // intended to be played |
| 5598 | if ((volumeDB > -96.0f) && |
| 5599 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5600 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5601 | stream, device, |
| 5602 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5603 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5604 | } |
| 5605 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5606 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5607 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5608 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5609 | } |
| 5610 | } |
| 5611 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5612 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5613 | } |
| 5614 | |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5615 | int AudioPolicyManager::rescaleVolumeIndex(int srcIndex, |
| 5616 | audio_stream_type_t srcStream, |
| 5617 | audio_stream_type_t dstStream) |
| 5618 | { |
| 5619 | if (srcStream == dstStream) { |
| 5620 | return srcIndex; |
| 5621 | } |
| 5622 | float minSrc = (float)mVolumeCurves->getVolumeIndexMin(srcStream); |
| 5623 | float maxSrc = (float)mVolumeCurves->getVolumeIndexMax(srcStream); |
| 5624 | float minDst = (float)mVolumeCurves->getVolumeIndexMin(dstStream); |
| 5625 | float maxDst = (float)mVolumeCurves->getVolumeIndexMax(dstStream); |
| 5626 | |
| 5627 | return (int)(minDst + ((srcIndex - minSrc) * (maxDst - minDst)) / (maxSrc - minSrc)); |
| 5628 | } |
| 5629 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5630 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5631 | int index, |
| 5632 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5633 | audio_devices_t device, |
| 5634 | int delayMs, |
| 5635 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5636 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5637 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5638 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5639 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5640 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5641 | return NO_ERROR; |
| 5642 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5643 | audio_policy_forced_cfg_t forceUseForComm = |
| 5644 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5645 | // 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] | 5646 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5647 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5648 | 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] | 5649 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5650 | return INVALID_OPERATION; |
| 5651 | } |
| 5652 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5653 | if (device == AUDIO_DEVICE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5654 | device = outputDesc->devices().types(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5655 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5656 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5657 | float volumeDb = computeVolume(stream, index, device); |
HW Lee | da7581e | 2018-05-22 18:31:34 +0800 | [diff] [blame] | 5658 | if (outputDesc->isFixedVolume(device) || |
| 5659 | // Force VoIP volume to max for bluetooth SCO |
| 5660 | ((stream == AUDIO_STREAM_VOICE_CALL || stream == AUDIO_STREAM_BLUETOOTH_SCO) && |
| 5661 | (device & AUDIO_DEVICE_OUT_ALL_SCO) != 0)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5662 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5663 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5664 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5665 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5666 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5667 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5668 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5669 | float voiceVolume; |
| 5670 | // 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] | 5671 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | 3839bc0 | 2018-07-10 18:33:34 -0700 | [diff] [blame] | 5672 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5673 | } else { |
| 5674 | voiceVolume = 1.0; |
| 5675 | } |
| 5676 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5677 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5678 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5679 | mLastVoiceVolume = voiceVolume; |
| 5680 | } |
| 5681 | } |
| 5682 | |
| 5683 | return NO_ERROR; |
| 5684 | } |
| 5685 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5686 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5687 | audio_devices_t device, |
| 5688 | int delayMs, |
| 5689 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5690 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5691 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5692 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5693 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5694 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5695 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5696 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5697 | device, |
| 5698 | delayMs, |
| 5699 | force); |
| 5700 | } |
| 5701 | } |
| 5702 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5703 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5704 | bool on, |
| 5705 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5706 | int delayMs, |
| 5707 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5708 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5709 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5710 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5711 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5712 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5713 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5714 | } |
| 5715 | } |
| 5716 | } |
| 5717 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5718 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5719 | bool on, |
| 5720 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5721 | int delayMs, |
| 5722 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5723 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5724 | if (device == AUDIO_DEVICE_NONE) { |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5725 | device = outputDesc->devices().types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5726 | } |
| 5727 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5728 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5729 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5730 | |
| 5731 | if (on) { |
| 5732 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5733 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5734 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5735 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5736 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5737 | } |
| 5738 | } |
| 5739 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5740 | outputDesc->mMuteCount[stream]++; |
| 5741 | } else { |
| 5742 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5743 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5744 | return; |
| 5745 | } |
| 5746 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5747 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5748 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5749 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5750 | device, |
| 5751 | delayMs); |
| 5752 | } |
| 5753 | } |
| 5754 | } |
| 5755 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5756 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5757 | { |
| 5758 | // flags to stream type mapping |
| 5759 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5760 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5761 | } |
| 5762 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5763 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5764 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5765 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5766 | return AUDIO_STREAM_TTS; |
| 5767 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5768 | |
Ari Hausman-Cohen | 5c00d01 | 2018-06-26 17:09:11 -0700 | [diff] [blame] | 5769 | return audio_usage_to_stream_type(attr->usage); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5770 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5771 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5772 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5773 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5774 | // has flags that map to a strategy? |
| 5775 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5776 | return true; |
| 5777 | } |
| 5778 | |
| 5779 | // has known usage? |
| 5780 | switch (paa->usage) { |
| 5781 | case AUDIO_USAGE_UNKNOWN: |
| 5782 | case AUDIO_USAGE_MEDIA: |
| 5783 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5784 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5785 | case AUDIO_USAGE_ALARM: |
| 5786 | case AUDIO_USAGE_NOTIFICATION: |
| 5787 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5788 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5789 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5790 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5791 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5792 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5793 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5794 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5795 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5796 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5797 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5798 | break; |
| 5799 | default: |
| 5800 | return false; |
| 5801 | } |
| 5802 | return true; |
| 5803 | } |
| 5804 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5805 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5806 | routing_strategy strategy, uint32_t inPastMs, |
| 5807 | nsecs_t sysTime) const |
| 5808 | { |
| 5809 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5810 | sysTime = systemTime(); |
| 5811 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5812 | 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] | 5813 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
Eric Laurent | 97ac871 | 2018-07-27 18:59:02 -0700 | [diff] [blame] | 5814 | (STRATEGY_NONE == strategy)) && |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5815 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5816 | return true; |
| 5817 | } |
| 5818 | } |
| 5819 | return false; |
| 5820 | } |
| 5821 | |
François Gaffie | 11d3010 | 2018-11-02 16:09:09 +0100 | [diff] [blame] | 5822 | bool AudioPolicyManager::isStrategyActiveOnSameModule(const sp<SwAudioOutputDescriptor>& outputDesc, |
| 5823 | routing_strategy strategy, uint32_t inPastMs, |
| 5824 | nsecs_t sysTime) const |
Eric Laurent | 484e927 | 2018-06-07 17:29:23 -0700 | [diff] [blame] | 5825 | { |
| 5826 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 5827 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 5828 | if (outputDesc->sharesHwModuleWith(desc) |
| 5829 | && isStrategyActive(desc, strategy, inPastMs, sysTime)) { |
| 5830 | return true; |
| 5831 | } |
| 5832 | } |
| 5833 | return false; |
| 5834 | } |
| 5835 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5836 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5837 | { |
| 5838 | return mEngine->getForceUse(usage); |
| 5839 | } |
| 5840 | |
| 5841 | bool AudioPolicyManager::isInCall() |
| 5842 | { |
| 5843 | return isStateInCall(mEngine->getPhoneState()); |
| 5844 | } |
| 5845 | |
| 5846 | bool AudioPolicyManager::isStateInCall(int state) |
| 5847 | { |
| 5848 | return is_state_in_call(state); |
| 5849 | } |
| 5850 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5851 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5852 | { |
| 5853 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
Eric Laurent | 3e6c7e1 | 2018-07-27 17:09:23 -0700 | [diff] [blame] | 5854 | sp<SourceClientDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5855 | if (sourceDesc->srcDevice()->equals(deviceDesc)) { |
| 5856 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->portId()); |
| 5857 | stopAudioSource(sourceDesc->portId()); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | |
| 5861 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5862 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5863 | bool release = false; |
| 5864 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5865 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5866 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5867 | source->ext.device.type == deviceDesc->type()) { |
| 5868 | release = true; |
| 5869 | } |
| 5870 | } |
| 5871 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5872 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5873 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5874 | sink->ext.device.type == deviceDesc->type()) { |
| 5875 | release = true; |
| 5876 | } |
| 5877 | } |
| 5878 | if (release) { |
| 5879 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5880 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5881 | } |
| 5882 | } |
Francois Gaffie | 716e143 | 2019-01-14 16:58:59 +0100 | [diff] [blame] | 5883 | |
| 5884 | mHwModules.cleanUpForDevice(deviceDesc); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5885 | } |
| 5886 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5887 | void AudioPolicyManager::modifySurroundFormats( |
| 5888 | const sp<DeviceDescriptor>& devDesc, FormatVector *formatsPtr) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5889 | std::unordered_set<audio_format_t> enforcedSurround( |
| 5890 | devDesc->encodedFormats().begin(), devDesc->encodedFormats().end()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5891 | std::unordered_set<audio_format_t> allSurround; // A flat set of all known surround formats |
| 5892 | for (const auto& pair : mConfig.getSurroundFormats()) { |
| 5893 | allSurround.insert(pair.first); |
| 5894 | for (const auto& subformat : pair.second) allSurround.insert(subformat); |
| 5895 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5896 | |
| 5897 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5898 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5899 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5900 | // This is the resulting set of formats depending on the surround mode: |
| 5901 | // 'all surround' = allSurround |
| 5902 | // 'enforced surround' = enforcedSurround [may include IEC69137 which isn't raw surround fmt] |
| 5903 | // 'non-surround' = not in 'all surround' and not in 'enforced surround' |
| 5904 | // 'manual surround' = mManualSurroundFormats |
| 5905 | // AUTO: formats v 'enforced surround' |
| 5906 | // ALWAYS: formats v 'all surround' v 'enforced surround' |
| 5907 | // NEVER: formats ^ 'non-surround' |
| 5908 | // MANUAL: formats ^ ('non-surround' v 'manual surround' v (IEC69137 ^ 'enforced surround')) |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5909 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5910 | std::unordered_set<audio_format_t> formatSet; |
| 5911 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL |
| 5912 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5913 | // formatSet is (formats ^ 'non-surround') |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5914 | for (auto formatIter = formatsPtr->begin(); formatIter != formatsPtr->end(); ++formatIter) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5915 | if (allSurround.count(*formatIter) == 0 && enforcedSurround.count(*formatIter) == 0) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5916 | formatSet.insert(*formatIter); |
| 5917 | } |
| 5918 | } |
| 5919 | } else { |
| 5920 | formatSet.insert(formatsPtr->begin(), formatsPtr->end()); |
| 5921 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5922 | formatsPtr->clear(); // Re-filled from the formatSet at the end. |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5923 | |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5924 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5925 | formatSet.insert(mManualSurroundFormats.begin(), mManualSurroundFormats.end()); |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5926 | // Enable IEC61937 when in MANUAL mode if it's enforced for this device. |
| 5927 | if (enforcedSurround.count(AUDIO_FORMAT_IEC61937) != 0) { |
| 5928 | formatSet.insert(AUDIO_FORMAT_IEC61937); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5929 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5930 | } else if (forceUse != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { // AUTO or ALWAYS |
| 5931 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5932 | formatSet.insert(allSurround.begin(), allSurround.end()); |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5933 | } |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5934 | formatSet.insert(enforcedSurround.begin(), enforcedSurround.end()); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5935 | } |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5936 | for (const auto& format : formatSet) { |
| 5937 | formatsPtr->push(format); |
| 5938 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5939 | } |
| 5940 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5941 | void AudioPolicyManager::modifySurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5942 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5943 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5944 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5945 | |
| 5946 | // If NEVER, then remove support for channelMasks > stereo. |
| 5947 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5948 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5949 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5950 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5951 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5952 | channelMasks.removeAt(maskIndex); |
| 5953 | } else { |
| 5954 | maskIndex++; |
| 5955 | } |
| 5956 | } |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5957 | // If ALWAYS or MANUAL, then make sure we at least support 5.1 |
| 5958 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS |
| 5959 | || forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5960 | bool supports5dot1 = false; |
| 5961 | // Are there any channel masks that can be considered "surround"? |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 5962 | for (audio_channel_mask_t channelMask : channelMasks) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5963 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5964 | supports5dot1 = true; |
| 5965 | break; |
| 5966 | } |
| 5967 | } |
| 5968 | // If not then add 5.1 support. |
| 5969 | if (!supports5dot1) { |
| 5970 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5971 | 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] | 5972 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5973 | } |
| 5974 | } |
| 5975 | |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5976 | void AudioPolicyManager::updateAudioProfiles(const sp<DeviceDescriptor>& devDesc, |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5977 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5978 | AudioProfileVector &profiles) |
| 5979 | { |
| 5980 | String8 reply; |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5981 | audio_devices_t device = devDesc->type(); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5982 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5983 | // Format MUST be checked first to update the list of AudioProfile |
| 5984 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5985 | reply = mpClientInterface->getParameters( |
| 5986 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
jiabin | 8177290 | 2018-04-02 17:52:27 -0700 | [diff] [blame] | 5987 | ALOGV("%s: supported formats %d, %s", __FUNCTION__, ioHandle, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5988 | AudioParameter repliedParameters(reply); |
| 5989 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5990 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5991 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5992 | return; |
| 5993 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5994 | FormatVector formats = formatsFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 5995 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 5996 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 5997 | modifySurroundFormats(devDesc, &formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5998 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5999 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6000 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6001 | |
Mikhail Naganov | cf84e59 | 2017-12-07 11:25:11 -0800 | [diff] [blame] | 6002 | for (audio_format_t format : profiles.getSupportedFormats()) { |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6003 | ChannelsVector channelMasks; |
| 6004 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6005 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6006 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6007 | |
| 6008 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6009 | reply = mpClientInterface->getParameters( |
| 6010 | ioHandle, |
| 6011 | requestedParameters.toString() + ";" + |
| 6012 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6013 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6014 | AudioParameter repliedParameters(reply); |
| 6015 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6016 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6017 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6018 | } |
| 6019 | } |
| 6020 | if (profiles.hasDynamicChannelsFor(format)) { |
| 6021 | reply = mpClientInterface->getParameters(ioHandle, |
| 6022 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6023 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6024 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6025 | AudioParameter repliedParameters(reply); |
| 6026 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 6027 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 6028 | channelMasks = channelMasksFromString(reply.string()); |
Mikhail Naganov | 100f012 | 2018-11-29 11:22:16 -0800 | [diff] [blame] | 6029 | if (device == AUDIO_DEVICE_OUT_HDMI |
| 6030 | || isDeviceOfModule(devDesc, AUDIO_HARDWARE_MODULE_ID_MSD)) { |
Mikhail Naganov | d5e1805 | 2018-11-30 14:55:45 -0800 | [diff] [blame] | 6031 | modifySurroundChannelMasks(&channelMasks); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 6032 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6033 | } |
| 6034 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 6035 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 6036 | } |
| 6037 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 6038 | |
Mikhail Naganov | dc76968 | 2018-05-04 15:34:08 -0700 | [diff] [blame] | 6039 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6040 | audio_patch_handle_t *patchHandle, |
| 6041 | AudioIODescriptorInterface *ioDescriptor, |
| 6042 | const struct audio_patch *patch, |
| 6043 | int delayMs) |
| 6044 | { |
| 6045 | ssize_t index = mAudioPatches.indexOfKey( |
| 6046 | patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE ? |
| 6047 | *patchHandle : ioDescriptor->getPatchHandle()); |
| 6048 | sp<AudioPatch> patchDesc; |
| 6049 | status_t status = installPatch( |
| 6050 | caller, index, patchHandle, patch, delayMs, mUidCached, &patchDesc); |
| 6051 | if (status == NO_ERROR) { |
| 6052 | ioDescriptor->setPatchHandle(patchDesc->mHandle); |
| 6053 | } |
| 6054 | return status; |
| 6055 | } |
| 6056 | |
| 6057 | status_t AudioPolicyManager::installPatch(const char *caller, |
| 6058 | ssize_t index, |
| 6059 | audio_patch_handle_t *patchHandle, |
| 6060 | const struct audio_patch *patch, |
| 6061 | int delayMs, |
| 6062 | uid_t uid, |
| 6063 | sp<AudioPatch> *patchDescPtr) |
| 6064 | { |
| 6065 | sp<AudioPatch> patchDesc; |
| 6066 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 6067 | if (index >= 0) { |
| 6068 | patchDesc = mAudioPatches.valueAt(index); |
| 6069 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 6070 | } |
| 6071 | |
| 6072 | status_t status = mpClientInterface->createAudioPatch(patch, &afPatchHandle, delayMs); |
| 6073 | ALOGV("%s() AF::createAudioPatch returned %d patchHandle %d num_sources %d num_sinks %d", |
| 6074 | caller, status, afPatchHandle, patch->num_sources, patch->num_sinks); |
| 6075 | if (status == NO_ERROR) { |
| 6076 | if (index < 0) { |
| 6077 | patchDesc = new AudioPatch(patch, uid); |
| 6078 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 6079 | } else { |
| 6080 | patchDesc->mPatch = *patch; |
| 6081 | } |
| 6082 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 6083 | if (patchHandle) { |
| 6084 | *patchHandle = patchDesc->mHandle; |
| 6085 | } |
| 6086 | nextAudioPortGeneration(); |
| 6087 | mpClientInterface->onAudioPatchListUpdate(); |
| 6088 | } |
| 6089 | if (patchDescPtr) *patchDescPtr = patchDesc; |
| 6090 | return status; |
| 6091 | } |
| 6092 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 6093 | } // namespace android |