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" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 |
| 28 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 29 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 30 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 31 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 32 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 33 | #include <AudioPolicyManagerInterface.h> |
| 34 | #include <AudioPolicyEngineInstance.h> |
Mathias Agopian | 05d19b0 | 2017-02-28 16:28:19 -0800 | [diff] [blame] | 35 | #include <cutils/atomic.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 38 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 39 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 41 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 42 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 43 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #ifndef USE_XML_AUDIO_POLICY_CONF |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 45 | #include <ConfigParsingUtils.h> |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <StreamDescriptor.h> |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 47 | #endif |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 48 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 49 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 50 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 52 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 53 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 54 | //FIXME: workaround for truncated touch sounds |
| 55 | // to be removed when the problem is handled by system UI |
| 56 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 57 | |
| 58 | // Largest difference in dB on earpiece in call between the voice volume and another |
| 59 | // media / notification / system volume. |
| 60 | constexpr float IN_CALL_EARPIECE_HEADROOM_DB = 3.f; |
| 61 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 62 | // ---------------------------------------------------------------------------- |
| 63 | // AudioPolicyInterface implementation |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 66 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 67 | audio_policy_dev_state_t state, |
| 68 | const char *device_address, |
| 69 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 70 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 71 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 72 | } |
| 73 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 74 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 75 | audio_policy_dev_state_t state, |
| 76 | const String8 &device_address) |
| 77 | { |
| 78 | AudioParameter param(device_address); |
| 79 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 80 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 81 | param.addInt(key, device); |
| 82 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 83 | } |
| 84 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 85 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 86 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 87 | const char *device_address, |
| 88 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 89 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 90 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 91 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | |
| 93 | // connect/disconnect only 1 device at a time |
| 94 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 95 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 96 | sp<DeviceDescriptor> devDesc = |
| 97 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 98 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 99 | // handle output devices |
| 100 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 101 | SortedVector <audio_io_handle_t> outputs; |
| 102 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 103 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 104 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 105 | // save a copy of the opened output descriptors before any output is opened or closed |
| 106 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 107 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 108 | switch (state) |
| 109 | { |
| 110 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 111 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 112 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 113 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 114 | return INVALID_OPERATION; |
| 115 | } |
| 116 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 117 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 118 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 119 | index = mAvailableOutputDevices.add(devDesc); |
| 120 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 121 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 122 | if (module == 0) { |
| 123 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 124 | device); |
| 125 | mAvailableOutputDevices.remove(devDesc); |
| 126 | return INVALID_OPERATION; |
| 127 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 128 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 129 | } else { |
| 130 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 131 | } |
| 132 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 133 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 134 | // parameters on newly connected devices (instead of opening the outputs...) |
| 135 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 136 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 137 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 138 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 139 | |
| 140 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 141 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 142 | return INVALID_OPERATION; |
| 143 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 144 | // Propagate device availability to Engine |
| 145 | mEngine->setDeviceConnectionState(devDesc, state); |
| 146 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 147 | // outputs should never be empty here |
| 148 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 149 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 150 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 151 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 152 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 153 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 154 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 155 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 156 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 157 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 158 | return INVALID_OPERATION; |
| 159 | } |
| 160 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 161 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 162 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 163 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 164 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 165 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 166 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 167 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 168 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 169 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 170 | |
| 171 | // Propagate device availability to Engine |
| 172 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 173 | } break; |
| 174 | |
| 175 | default: |
| 176 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 177 | return BAD_VALUE; |
| 178 | } |
| 179 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 180 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 181 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 182 | checkA2dpSuspend(); |
| 183 | checkOutputForAllStrategies(); |
| 184 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 185 | if (!outputs.isEmpty()) { |
| 186 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 187 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 188 | // close unused outputs after device disconnection or direct outputs that have been |
| 189 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 190 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 192 | (desc->mDirectOpenCount == 0))) { |
| 193 | closeOutput(outputs[i]); |
| 194 | } |
| 195 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 196 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 197 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 201 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 202 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 203 | updateCallRouting(newDevice); |
| 204 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 205 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 206 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 207 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 208 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 209 | // do not force device change on duplicated output because if device is 0, it will |
| 210 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 211 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 212 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 213 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 214 | // always force when disconnecting (a non-duplicated device) |
| 215 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 216 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 217 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 220 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 221 | cleanUpForDevice(devDesc); |
| 222 | } |
| 223 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 224 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 225 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 226 | } // end if is output device |
| 227 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | // handle input devices |
| 229 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 230 | SortedVector <audio_io_handle_t> inputs; |
| 231 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 232 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | switch (state) |
| 234 | { |
| 235 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 236 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 237 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 238 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 239 | return INVALID_OPERATION; |
| 240 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 241 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 242 | if (module == NULL) { |
| 243 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 244 | device); |
| 245 | return INVALID_OPERATION; |
| 246 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 247 | |
| 248 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 249 | // parameters on newly connected devices (instead of opening the inputs...) |
| 250 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 251 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 252 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 253 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 254 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 255 | return INVALID_OPERATION; |
| 256 | } |
| 257 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 258 | index = mAvailableInputDevices.add(devDesc); |
| 259 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 260 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 261 | } else { |
| 262 | return NO_MEMORY; |
| 263 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 264 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 265 | // Propagate device availability to Engine |
| 266 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 267 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 268 | |
| 269 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 270 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 271 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 272 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 273 | return INVALID_OPERATION; |
| 274 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 275 | |
| 276 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 277 | |
| 278 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 279 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 280 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 281 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 282 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 283 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 284 | // Propagate device availability to Engine |
| 285 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 286 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 287 | |
| 288 | default: |
| 289 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 290 | return BAD_VALUE; |
| 291 | } |
| 292 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 293 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 294 | // As the input device list can impact the output device selection, update |
| 295 | // getDeviceForStrategy() cache |
| 296 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 297 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 298 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 299 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 300 | updateCallRouting(newDevice); |
| 301 | } |
| 302 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 303 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 304 | cleanUpForDevice(devDesc); |
| 305 | } |
| 306 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 307 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 308 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 309 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 310 | |
| 311 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 312 | return BAD_VALUE; |
| 313 | } |
| 314 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 315 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 316 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 317 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 318 | sp<DeviceDescriptor> devDesc = |
| 319 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 320 | (strlen(device_address) != 0)/*matchAddress*/); |
| 321 | |
| 322 | if (devDesc == 0) { |
| 323 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 324 | device, device_address); |
| 325 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 326 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 327 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 328 | DeviceVector *deviceVector; |
| 329 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 330 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 331 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 333 | deviceVector = &mAvailableInputDevices; |
| 334 | } else { |
| 335 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 336 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 337 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 338 | |
| 339 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 340 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 343 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 344 | const char *device_address, |
| 345 | const char *device_name) |
| 346 | { |
| 347 | status_t status; |
| 348 | |
| 349 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 350 | device, device_address, device_name); |
| 351 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 352 | // connect/disconnect only 1 device at a time |
| 353 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 354 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 355 | // Check if the device is currently connected |
| 356 | sp<DeviceDescriptor> devDesc = |
| 357 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 358 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 359 | if (index < 0) { |
| 360 | // Nothing to do: device is not connected |
| 361 | return NO_ERROR; |
| 362 | } |
| 363 | |
| 364 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 365 | // This will force reading again the device configuration |
| 366 | status = setDeviceConnectionState(device, |
| 367 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 368 | device_address, device_name); |
| 369 | if (status != NO_ERROR) { |
| 370 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 371 | status); |
| 372 | return status; |
| 373 | } |
| 374 | |
| 375 | status = setDeviceConnectionState(device, |
| 376 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 377 | device_address, device_name); |
| 378 | if (status != NO_ERROR) { |
| 379 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 380 | status); |
| 381 | return status; |
| 382 | } |
| 383 | |
| 384 | return NO_ERROR; |
| 385 | } |
| 386 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 387 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 388 | { |
| 389 | bool createTxPatch = false; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 390 | status_t status; |
| 391 | audio_patch_handle_t afPatchHandle; |
| 392 | DeviceVector deviceList; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 393 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 394 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 395 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 396 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 397 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 398 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 399 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 400 | |
| 401 | // release existing RX patch if any |
| 402 | if (mCallRxPatch != 0) { |
| 403 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 404 | mCallRxPatch.clear(); |
| 405 | } |
| 406 | // release TX patch if any |
| 407 | if (mCallTxPatch != 0) { |
| 408 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 409 | mCallTxPatch.clear(); |
| 410 | } |
| 411 | |
| 412 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 413 | // via setOutputDevice() on primary output. |
| 414 | // Otherwise, create two audio patches for TX and RX path. |
| 415 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 416 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 417 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 418 | // of it due to legacy implementation. If not, create a patch. |
| 419 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 420 | == AUDIO_DEVICE_NONE) { |
| 421 | createTxPatch = true; |
| 422 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 423 | } else { // create RX path audio patch |
| 424 | struct audio_patch patch; |
| 425 | |
| 426 | patch.num_sources = 1; |
| 427 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 428 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 429 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 430 | "updateCallRouting() selected device not in output device list"); |
| 431 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 432 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 433 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 434 | "updateCallRouting() no telephony RX device"); |
| 435 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 436 | |
| 437 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 438 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 439 | |
| 440 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 441 | SortedVector<audio_io_handle_t> outputs = |
| 442 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 443 | audio_io_handle_t output = selectOutput(outputs, |
| 444 | AUDIO_OUTPUT_FLAG_NONE, |
| 445 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 446 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 447 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 448 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 449 | "updateCallRouting() RX device output is duplicated"); |
| 450 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 451 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | patch.num_sources = 2; |
| 453 | } |
| 454 | |
| 455 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 456 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 457 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 458 | status); |
| 459 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 460 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 461 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 462 | mCallRxPatch->mUid = mUidCached; |
| 463 | } |
| 464 | createTxPatch = true; |
| 465 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 466 | if (createTxPatch) { // create TX path audio patch |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 467 | struct audio_patch patch; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 468 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 469 | patch.num_sources = 1; |
| 470 | patch.num_sinks = 1; |
| 471 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 472 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 473 | "updateCallRouting() selected device not in input device list"); |
| 474 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 475 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 476 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 477 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 478 | "updateCallRouting() no telephony TX device"); |
| 479 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 480 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 481 | |
| 482 | SortedVector<audio_io_handle_t> outputs = |
| 483 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 484 | audio_io_handle_t output = selectOutput(outputs, |
| 485 | AUDIO_OUTPUT_FLAG_NONE, |
| 486 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 487 | // request to reuse existing output stream if one is already opened to reach the TX |
| 488 | // path output device |
| 489 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 490 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 491 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 492 | "updateCallRouting() RX device output is duplicated"); |
| 493 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 494 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 495 | patch.num_sources = 2; |
| 496 | } |
| 497 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 498 | // terminate active capture if on the same HW module as the call TX source device |
| 499 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 500 | // call TX device but this information is not in the audio patch and logic here must be |
| 501 | // symmetric to the one in startInput() |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 502 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 503 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 504 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 505 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 506 | AudioSessionCollection activeSessions = |
| 507 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 508 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 509 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 510 | stopInput(activeDesc->mIoHandle, activeSession); |
| 511 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 512 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 516 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 517 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 518 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 519 | status); |
| 520 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 521 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 522 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 523 | mCallTxPatch->mUid = mUidCached; |
| 524 | } |
| 525 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 526 | |
| 527 | return muteWaitMs; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 530 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | { |
| 532 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 533 | // store previous phone state for management of sonification strategy below |
| 534 | int oldState = mEngine->getPhoneState(); |
| 535 | |
| 536 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 537 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 538 | return; |
| 539 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 540 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 541 | // if leaving call state, handle special case of active streams |
| 542 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 543 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 544 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 545 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 546 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 547 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 548 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 549 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 550 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 551 | } |
| 552 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 553 | /** |
| 554 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 555 | * routing command. |
| 556 | */ |
| 557 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 558 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 559 | |
| 560 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 561 | checkA2dpSuspend(); |
| 562 | checkOutputForAllStrategies(); |
| 563 | updateDevicesAndOutputs(); |
| 564 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | int delayMs = 0; |
| 566 | if (isStateInCall(state)) { |
| 567 | nsecs_t sysTime = systemTime(); |
| 568 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 569 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 570 | // mute media and sonification strategies and delay device switch by the largest |
| 571 | // latency of any output where either strategy is active. |
| 572 | // 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] | 573 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 574 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 575 | sysTime) || |
| 576 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 577 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 578 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 579 | (delayMs < (int)desc->latency()*2)) { |
| 580 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 581 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 582 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 583 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 584 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 585 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 586 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 587 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 588 | } |
| 589 | } |
| 590 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 591 | if (hasPrimaryOutput()) { |
| 592 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 593 | // the device returned is not necessarily reachable via this output |
| 594 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 595 | // force routing command to audio hardware when ending call |
| 596 | // even if no device change is needed |
| 597 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 598 | rxDevice = mPrimaryOutput->device(); |
| 599 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 600 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 601 | if (state == AUDIO_MODE_IN_CALL) { |
| 602 | updateCallRouting(rxDevice, delayMs); |
| 603 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 604 | if (mCallRxPatch != 0) { |
| 605 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 606 | mCallRxPatch.clear(); |
| 607 | } |
| 608 | if (mCallTxPatch != 0) { |
| 609 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 610 | mCallTxPatch.clear(); |
| 611 | } |
| 612 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 613 | } else { |
| 614 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 615 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 616 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 617 | // if entering in call state, handle special case of active streams |
| 618 | // pertaining to sonification strategy see handleIncallSonification() |
| 619 | if (isStateInCall(state)) { |
| 620 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 621 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 622 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 623 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 624 | |
| 625 | // force reevaluating accessibility routing when call starts |
| 626 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | // 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] | 630 | if (state == AUDIO_MODE_RINGTONE && |
| 631 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 632 | mLimitRingtoneVolume = true; |
| 633 | } else { |
| 634 | mLimitRingtoneVolume = false; |
| 635 | } |
| 636 | } |
| 637 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 638 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 639 | return mEngine->getPhoneState(); |
| 640 | } |
| 641 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 642 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 643 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 644 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 645 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 646 | if (config == mEngine->getForceUse(usage)) { |
| 647 | return; |
| 648 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 649 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 650 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 651 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 652 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 653 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 654 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 655 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 656 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 657 | |
| 658 | // check for device and output changes triggered by new force usage |
| 659 | checkA2dpSuspend(); |
| 660 | checkOutputForAllStrategies(); |
| 661 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 662 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 663 | //FIXME: workaround for truncated touch sounds |
| 664 | // to be removed when the problem is handled by system UI |
| 665 | uint32_t delayMs = 0; |
| 666 | uint32_t waitMs = 0; |
| 667 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 668 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 669 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 670 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 671 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 672 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 673 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 674 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 675 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 676 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 677 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 678 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 679 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 680 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 681 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 682 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 683 | } |
| 684 | } |
| 685 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 686 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 687 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 688 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 689 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 690 | // Force new input selection if the new device can not be reached via current input |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 691 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 692 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 693 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 694 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 695 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 696 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 697 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 700 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 701 | { |
| 702 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 703 | } |
| 704 | |
| 705 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 706 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 707 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 708 | audio_devices_t device, |
| 709 | uint32_t samplingRate, |
| 710 | audio_format_t format, |
| 711 | audio_channel_mask_t channelMask, |
| 712 | audio_output_flags_t flags) |
| 713 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 714 | // only retain flags that will drive the direct output profile selection |
| 715 | // if explicitly requested |
| 716 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 717 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 718 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 719 | flags = |
| 720 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 721 | |
| 722 | sp<IOProfile> profile; |
| 723 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 724 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 725 | if (mHwModules[i]->mHandle == 0) { |
| 726 | continue; |
| 727 | } |
| 728 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 729 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 730 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 731 | samplingRate, NULL /*updatedSamplingRate*/, |
| 732 | format, NULL /*updatedFormat*/, |
| 733 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 734 | flags)) { |
| 735 | continue; |
| 736 | } |
| 737 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 738 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 739 | continue; |
| 740 | } |
| 741 | // if several profiles are compatible, give priority to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 742 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 743 | continue; |
| 744 | } |
| 745 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 746 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 747 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 748 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 749 | } |
| 750 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 751 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 752 | } |
| 753 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 754 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 755 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 756 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 757 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 758 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 759 | 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] | 760 | |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 761 | ALOGV("getOutput() stream %d selected device %08x, output %d", stream, device, output); |
| 762 | return output; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 765 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 766 | audio_io_handle_t *output, |
| 767 | audio_session_t session, |
| 768 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 769 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 770 | const audio_config_t *config, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 771 | audio_output_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 772 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 773 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 774 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 775 | audio_attributes_t attributes; |
| 776 | if (attr != NULL) { |
| 777 | if (!isValidAttributes(attr)) { |
| 778 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 779 | attr->usage, attr->content_type, attr->flags, |
| 780 | attr->tags); |
| 781 | return BAD_VALUE; |
| 782 | } |
| 783 | attributes = *attr; |
| 784 | } else { |
| 785 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 786 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 787 | return BAD_VALUE; |
| 788 | } |
| 789 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 790 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 791 | |
| 792 | // TODO: check for existing client for this port ID |
| 793 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 794 | *portId = AudioPort::getNextUniqueId(); |
| 795 | } |
| 796 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 797 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 798 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 799 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 800 | if (!audio_has_proportional_frames(config->format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 801 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 802 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 803 | *stream = streamTypefromAttributesInt(&attributes); |
| 804 | *output = desc->mIoHandle; |
| 805 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 806 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 807 | } |
| 808 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 809 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 810 | return BAD_VALUE; |
| 811 | } |
| 812 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 813 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 814 | " session %d selectedDeviceId %d", |
| 815 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 816 | session, *selectedDeviceId); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 817 | |
| 818 | *stream = streamTypefromAttributesInt(&attributes); |
| 819 | |
| 820 | // Explicit routing? |
| 821 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 822 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 823 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 824 | if (mAvailableOutputDevices[i]->getId() == *selectedDeviceId) { |
| 825 | deviceDesc = mAvailableOutputDevices[i]; |
| 826 | break; |
| 827 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 831 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 832 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 833 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 834 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 835 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 836 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 837 | } |
| 838 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 839 | ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 840 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 841 | |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 842 | *output = getOutputForDevice(device, session, *stream, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 843 | config->sample_rate, config->format, config->channel_mask, |
| 844 | flags, &config->offload_info); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 845 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 846 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 847 | return INVALID_OPERATION; |
| 848 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 849 | |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 850 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 851 | *selectedDeviceId = outputDevices.size() > 0 ? outputDevices.itemAt(0)->getId() |
| 852 | : AUDIO_PORT_HANDLE_NONE; |
| 853 | |
| 854 | ALOGV(" getOutputForAttr() returns output %d selectedDeviceId %d", *output, *selectedDeviceId); |
| 855 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 856 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 860 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 861 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 862 | audio_stream_type_t stream, |
| 863 | uint32_t samplingRate, |
| 864 | audio_format_t format, |
| 865 | audio_channel_mask_t channelMask, |
| 866 | audio_output_flags_t flags, |
| 867 | const audio_offload_info_t *offloadInfo) |
| 868 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 869 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 870 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 871 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 872 | // open a direct output if required by specified parameters |
| 873 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 874 | // and all common behaviors are driven by checking only the direct flag |
| 875 | // this should normally be set appropriately in the policy configuration file |
| 876 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 877 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 878 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 879 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 880 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 881 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 882 | // only allow deep buffering for music stream type |
| 883 | if (stream != AUDIO_STREAM_MUSIC) { |
| 884 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 885 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 886 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 887 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 888 | // use DEEP_BUFFER as default output for music stream type |
| 889 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 890 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 891 | if (stream == AUDIO_STREAM_TTS) { |
| 892 | flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 893 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 894 | audio_is_linear_pcm(format)) { |
| 895 | flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
| 896 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 897 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 898 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 899 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 900 | sp<IOProfile> profile; |
| 901 | |
| 902 | // 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] | 903 | // and not explicitly requested |
| 904 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 905 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 906 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 907 | goto non_direct_output; |
| 908 | } |
| 909 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 910 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 911 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 912 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 913 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 914 | // This may prevent offloading in rare situations where effects are left active by apps |
| 915 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 916 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 917 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 918 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 919 | profile = getProfileForDirectOutput(device, |
| 920 | samplingRate, |
| 921 | format, |
| 922 | channelMask, |
| 923 | (audio_output_flags_t)flags); |
| 924 | } |
| 925 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 926 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 927 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 | |
| 929 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 930 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 931 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 932 | outputDesc = desc; |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 933 | // reuse direct output if currently open by the same client |
| 934 | // and configured with same parameters |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 935 | if ((samplingRate == outputDesc->mSamplingRate) && |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 936 | audio_formats_match(format, outputDesc->mFormat) && |
| 937 | (channelMask == outputDesc->mChannelMask)) { |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 938 | if (session == outputDesc->mDirectClientSession) { |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 939 | outputDesc->mDirectOpenCount++; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 940 | ALOGV("getOutputForDevice() reusing direct output %d for session %d", |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 941 | mOutputs.keyAt(i), session); |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 942 | return mOutputs.keyAt(i); |
| 943 | } else { |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 944 | ALOGV("getOutputForDevice() do not reuse direct output because" |
| 945 | "current client (%d) is not the same as requesting client (%d)", |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 946 | outputDesc->mDirectClientSession, session); |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 947 | goto non_direct_output; |
| 948 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 949 | } |
| 950 | } |
| 951 | } |
| 952 | // close direct output if currently open and configured with different parameters |
| 953 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 954 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 956 | |
| 957 | // if the selected profile is offloaded and no offload info was specified, |
| 958 | // create a default one |
| 959 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 960 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 961 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 962 | defaultOffloadInfo.sample_rate = samplingRate; |
| 963 | defaultOffloadInfo.channel_mask = channelMask; |
| 964 | defaultOffloadInfo.format = format; |
| 965 | defaultOffloadInfo.stream_type = stream; |
| 966 | defaultOffloadInfo.bit_rate = 0; |
| 967 | defaultOffloadInfo.duration_us = -1; |
| 968 | defaultOffloadInfo.has_video = true; // conservative |
| 969 | defaultOffloadInfo.is_streaming = true; // likely |
| 970 | offloadInfo = &defaultOffloadInfo; |
| 971 | } |
| 972 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 973 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 975 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 976 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 977 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 978 | config.sample_rate = samplingRate; |
| 979 | config.channel_mask = channelMask; |
| 980 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 981 | if (offloadInfo != NULL) { |
| 982 | config.offload_info = *offloadInfo; |
| 983 | } |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 984 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 985 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 986 | : String8(""); |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 987 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 988 | &output, |
| 989 | &config, |
| 990 | &outputDesc->mDevice, |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 991 | address, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 992 | &outputDesc->mLatency, |
| 993 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 994 | |
| 995 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 996 | if (status != NO_ERROR || |
| 997 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 998 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 999 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 1000 | ALOGV("getOutputForDevice() failed opening direct output: output %d samplingRate %d %d," |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1001 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1002 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1003 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1004 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1005 | mpClientInterface->closeOutput(output); |
| 1006 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1007 | // fall back to mixer output if possible when the direct output could not be open |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1008 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1009 | goto non_direct_output; |
| 1010 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1011 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1012 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1013 | outputDesc->mSamplingRate = config.sample_rate; |
| 1014 | outputDesc->mChannelMask = config.channel_mask; |
| 1015 | outputDesc->mFormat = config.format; |
| 1016 | outputDesc->mRefCount[stream] = 0; |
| 1017 | outputDesc->mStopTime[stream] = 0; |
| 1018 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1019 | outputDesc->mDirectClientSession = session; |
| 1020 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1021 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1022 | mPreviousOutputs = mOutputs; |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 1023 | ALOGV("getOutputForDevice() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1024 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1025 | return output; |
| 1026 | } |
| 1027 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1028 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1029 | |
| 1030 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1031 | // stamps are embedded in audio data |
| 1032 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1033 | return AUDIO_IO_HANDLE_NONE; |
| 1034 | } |
| 1035 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1036 | // ignoring channel mask due to downmix capability in mixer |
| 1037 | |
| 1038 | // open a non direct output |
| 1039 | |
| 1040 | // for non direct outputs, only PCM is supported |
| 1041 | if (audio_is_linear_pcm(format)) { |
| 1042 | // get which output is suitable for the specified stream. The actual |
| 1043 | // routing change will happen when startOutput() will be called |
| 1044 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1045 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1046 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1047 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1048 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | } |
Eric Laurent | f4e6345 | 2017-11-06 19:31:46 +0000 | [diff] [blame^] | 1050 | ALOGW_IF((output == 0), "getOutputForDevice() could not find output for stream %d, " |
| 1051 | "samplingRate %d, format %d, channels %x, flags %x", |
| 1052 | stream, samplingRate, format, channelMask, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1053 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1054 | return output; |
| 1055 | } |
| 1056 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1057 | 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] | 1058 | audio_output_flags_t flags, |
| 1059 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | { |
| 1061 | // select one output among several that provide a path to a particular device or set of |
| 1062 | // devices (the list was previously build by getOutputsForDevice()). |
| 1063 | // The priority is as follows: |
| 1064 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1065 | // 2: the output with the bit depth the closest to the requested one |
| 1066 | // 3: the primary output |
| 1067 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | |
| 1069 | if (outputs.size() == 0) { |
| 1070 | return 0; |
| 1071 | } |
| 1072 | if (outputs.size() == 1) { |
| 1073 | return outputs[0]; |
| 1074 | } |
| 1075 | |
| 1076 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1077 | audio_io_handle_t outputForFlags = 0; |
| 1078 | audio_io_handle_t outputForPrimary = 0; |
| 1079 | audio_io_handle_t outputForFormat = 0; |
| 1080 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1081 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1082 | |
| 1083 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1084 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1085 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1086 | // if a valid format is specified, skip output if not compatible |
| 1087 | if (format != AUDIO_FORMAT_INVALID) { |
| 1088 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1089 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1090 | continue; |
| 1091 | } |
| 1092 | } else if (!audio_is_linear_pcm(format)) { |
| 1093 | continue; |
| 1094 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1095 | if (AudioPort::isBetterFormatMatch( |
| 1096 | outputDesc->mFormat, bestFormat, format)) { |
| 1097 | outputForFormat = outputs[i]; |
| 1098 | bestFormat = outputDesc->mFormat; |
| 1099 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1102 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1103 | if (commonFlags >= maxCommonFlags) { |
| 1104 | if (commonFlags == maxCommonFlags) { |
| 1105 | if (AudioPort::isBetterFormatMatch( |
| 1106 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1107 | outputForFlags = outputs[i]; |
| 1108 | bestFormatForFlags = outputDesc->mFormat; |
| 1109 | } |
| 1110 | } else { |
| 1111 | outputForFlags = outputs[i]; |
| 1112 | maxCommonFlags = commonFlags; |
| 1113 | bestFormatForFlags = outputDesc->mFormat; |
| 1114 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1115 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1116 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1117 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1118 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1123 | if (outputForFlags != 0) { |
| 1124 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1125 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1126 | if (outputForFormat != 0) { |
| 1127 | return outputForFormat; |
| 1128 | } |
| 1129 | if (outputForPrimary != 0) { |
| 1130 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | return outputs[0]; |
| 1134 | } |
| 1135 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1136 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1137 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1138 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1139 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1140 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1141 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1142 | ssize_t index = mOutputs.indexOfKey(output); |
| 1143 | if (index < 0) { |
| 1144 | ALOGW("startOutput() unknown output %d", output); |
| 1145 | return BAD_VALUE; |
| 1146 | } |
| 1147 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1148 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1149 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1150 | // Routing? |
| 1151 | mOutputRoutes.incRouteActivity(session); |
| 1152 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1153 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1154 | AudioMix *policyMix = NULL; |
| 1155 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1156 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1157 | policyMix = outputDesc->mPolicyMix; |
| 1158 | address = policyMix->mDeviceAddress.string(); |
| 1159 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1160 | newDevice = policyMix->mDeviceType; |
| 1161 | } else { |
| 1162 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1163 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1164 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1165 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1166 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1167 | } else { |
| 1168 | newDevice = AUDIO_DEVICE_NONE; |
| 1169 | } |
| 1170 | |
| 1171 | uint32_t delayMs = 0; |
| 1172 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1173 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1174 | |
| 1175 | if (status != NO_ERROR) { |
| 1176 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1177 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1178 | } |
| 1179 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1180 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1181 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1182 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1183 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1184 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1185 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1186 | "remote-submix"); |
| 1187 | } |
| 1188 | |
| 1189 | if (delayMs != 0) { |
| 1190 | usleep(delayMs * 1000); |
| 1191 | } |
| 1192 | |
| 1193 | return status; |
| 1194 | } |
| 1195 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1196 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1197 | audio_stream_type_t stream, |
| 1198 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1199 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1200 | uint32_t *delayMs) |
| 1201 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1202 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1203 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1204 | |
| 1205 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1206 | if (stream == AUDIO_STREAM_TTS) { |
| 1207 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1208 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1209 | return INVALID_OPERATION; |
| 1210 | } else { |
| 1211 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1212 | } |
| 1213 | } else { |
| 1214 | // some playback other than beacon starts |
| 1215 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1216 | } |
| 1217 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1218 | // 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] | 1219 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1220 | bool force = !outputDesc->isActive() && |
| 1221 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1222 | |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1223 | // requiresMuteCheck is false when we can bypass mute strategy. |
| 1224 | // It covers a common case when there is no materially active audio |
| 1225 | // and muting would result in unnecessary delay and dropped audio. |
| 1226 | const uint32_t outputLatencyMs = outputDesc->latency(); |
| 1227 | bool requiresMuteCheck = outputDesc->isActive(outputLatencyMs * 2); // account for drain |
| 1228 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1229 | // increment usage count for this stream on the requested output: |
| 1230 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1231 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1232 | outputDesc->changeRefCount(stream, 1); |
| 1233 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1234 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1235 | selectOutputForMusicEffects(); |
| 1236 | } |
| 1237 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1238 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1239 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1240 | if (device == AUDIO_DEVICE_NONE) { |
| 1241 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1242 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1243 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1244 | routing_strategy strategy = getStrategy(stream); |
| 1245 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1246 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1247 | (beaconMuteLatency > 0); |
| 1248 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1249 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1250 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1251 | if (desc != outputDesc) { |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1252 | // An output has a shared device if |
| 1253 | // - managed by the same hw module |
| 1254 | // - supports the currently selected device |
| 1255 | const bool sharedDevice = outputDesc->sharesHwModuleWith(desc) |
| 1256 | && (desc->supportedDevices() & device) != AUDIO_DEVICE_NONE; |
| 1257 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1258 | // force a device change if any other output is: |
| 1259 | // - managed by the same hw module |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1260 | // - supports currently selected device |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1261 | // - has a current device selection that differs from selected device. |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1262 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1263 | // In this case, the audio HAL must receive the new device selection so that it can |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1264 | // change the device currently selected by the other output. |
| 1265 | if (sharedDevice && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1266 | desc->device() != device && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1267 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1268 | force = true; |
| 1269 | } |
| 1270 | // 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] | 1271 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1272 | // event occurred for beacon |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1273 | const uint32_t latencyMs = desc->latency(); |
| 1274 | const bool isActive = desc->isActive(latencyMs * 2); // account for drain |
| 1275 | |
| 1276 | if (shouldWait && isActive && (waitMs < latencyMs)) { |
| 1277 | waitMs = latencyMs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1278 | } |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1279 | |
| 1280 | // Require mute check if another output is on a shared device |
| 1281 | // and currently active to have proper drain and avoid pops. |
| 1282 | // Note restoring AudioTracks onto this output needs to invoke |
| 1283 | // a volume ramp if there is no mute. |
| 1284 | requiresMuteCheck |= sharedDevice && isActive; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1285 | } |
| 1286 | } |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1287 | |
| 1288 | const uint32_t muteWaitMs = |
| 1289 | setOutputDevice(outputDesc, device, force, 0, NULL, address, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1290 | |
| 1291 | // handle special case for sonification while in call |
| 1292 | if (isInCall()) { |
| 1293 | handleIncallSonification(stream, true, false); |
| 1294 | } |
| 1295 | |
| 1296 | // apply volume rules for current stream and device if necessary |
| 1297 | checkAndSetVolume(stream, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1298 | mVolumeCurves->getVolumeIndex(stream, outputDesc->device()), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1299 | outputDesc, |
Shuhei Miyazaki | 6fe7033 | 2017-07-31 15:21:28 +0900 | [diff] [blame] | 1300 | outputDesc->device()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1301 | |
| 1302 | // update the outputs if starting an output with a stream that can affect notification |
| 1303 | // routing |
| 1304 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1305 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1306 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1307 | if (strategy == STRATEGY_SONIFICATION) { |
| 1308 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1309 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1310 | |
| 1311 | if (waitMs > muteWaitMs) { |
| 1312 | *delayMs = waitMs - muteWaitMs; |
| 1313 | } |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 1314 | |
| 1315 | // FIXME: A device change (muteWaitMs > 0) likely introduces a volume change. |
| 1316 | // A volume change enacted by APM with 0 delay is not synchronous, as it goes |
| 1317 | // via AudioCommandThread to AudioFlinger. Hence it is possible that the volume |
| 1318 | // change occurs after the MixerThread starts and causes a stream volume |
| 1319 | // glitch. |
| 1320 | // |
| 1321 | // We do not introduce additional delay here. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1322 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1323 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1324 | return NO_ERROR; |
| 1325 | } |
| 1326 | |
| 1327 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1328 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1329 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1330 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1331 | { |
| 1332 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1333 | ssize_t index = mOutputs.indexOfKey(output); |
| 1334 | if (index < 0) { |
| 1335 | ALOGW("stopOutput() unknown output %d", output); |
| 1336 | return BAD_VALUE; |
| 1337 | } |
| 1338 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1339 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1340 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1341 | if (outputDesc->mRefCount[stream] == 1) { |
| 1342 | // Automatically disable the remote submix input when output is stopped on a |
| 1343 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1344 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1345 | outputDesc->mPolicyMix != NULL && |
| 1346 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1347 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1348 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1349 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1350 | "remote-submix"); |
| 1351 | } |
| 1352 | } |
| 1353 | |
| 1354 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1355 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1356 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1357 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1358 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1359 | |
| 1360 | if (forceDeviceUpdate) { |
| 1361 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1362 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1365 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1368 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1369 | audio_stream_type_t stream, |
| 1370 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1371 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1372 | // always handle stream stop, check which stream type is stopping |
| 1373 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1374 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1375 | // handle special case for sonification while in call |
| 1376 | if (isInCall()) { |
| 1377 | handleIncallSonification(stream, false, false); |
| 1378 | } |
| 1379 | |
| 1380 | if (outputDesc->mRefCount[stream] > 0) { |
| 1381 | // decrement usage count of this stream on the output |
| 1382 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1383 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1384 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1385 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1386 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1387 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1388 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1389 | // the track stop() command is received and at that time the audio track buffer can |
| 1390 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1391 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1392 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1393 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1394 | |
| 1395 | // force restoring the device selection on other active outputs if it differs from the |
| 1396 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1397 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1398 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1399 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1400 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1401 | desc->isActive() && |
| 1402 | outputDesc->sharesHwModuleWith(desc) && |
| 1403 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1404 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1405 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1406 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1407 | newDevice2, |
| 1408 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1409 | delayMs); |
| 1410 | // re-apply device specific volume if not done by setOutputDevice() |
| 1411 | if (!force) { |
| 1412 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1413 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1417 | handleNotificationRoutingForStream(stream); |
| 1418 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1419 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1420 | selectOutputForMusicEffects(); |
| 1421 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1422 | return NO_ERROR; |
| 1423 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1424 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1425 | return INVALID_OPERATION; |
| 1426 | } |
| 1427 | } |
| 1428 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1429 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1430 | audio_stream_type_t stream __unused, |
| 1431 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1432 | { |
| 1433 | ALOGV("releaseOutput() %d", output); |
| 1434 | ssize_t index = mOutputs.indexOfKey(output); |
| 1435 | if (index < 0) { |
| 1436 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1437 | return; |
| 1438 | } |
| 1439 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1440 | // Routing |
| 1441 | mOutputRoutes.removeRoute(session); |
| 1442 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1443 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1444 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1445 | if (desc->mDirectOpenCount <= 0) { |
| 1446 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1447 | desc->mDirectOpenCount, output); |
| 1448 | return; |
| 1449 | } |
| 1450 | if (--desc->mDirectOpenCount == 0) { |
| 1451 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1452 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1458 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1459 | audio_io_handle_t *input, |
| 1460 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1461 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1462 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1463 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1464 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1465 | input_type_t *inputType, |
| 1466 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1468 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1469 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1470 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1471 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1472 | status_t status = NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1473 | // handle legacy remote submix case where the address was not always specified |
| 1474 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1475 | audio_source_t halInputSource; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1476 | audio_source_t inputSource = attr->source; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1477 | AudioMix *policyMix = NULL; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1478 | DeviceVector inputDevices; |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1479 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1480 | // Explicit routing? |
| 1481 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1482 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 1483 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1484 | if (mAvailableInputDevices[i]->getId() == *selectedDeviceId) { |
| 1485 | deviceDesc = mAvailableInputDevices[i]; |
| 1486 | break; |
| 1487 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1488 | } |
| 1489 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1490 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1491 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1492 | // special case for mmap capture: if an input IO handle is specified, we reuse this input if |
| 1493 | // possible |
| 1494 | if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) == AUDIO_INPUT_FLAG_MMAP_NOIRQ && |
| 1495 | *input != AUDIO_IO_HANDLE_NONE) { |
| 1496 | ssize_t index = mInputs.indexOfKey(*input); |
| 1497 | if (index < 0) { |
| 1498 | ALOGW("getInputForAttr() unknown MMAP input %d", *input); |
| 1499 | status = BAD_VALUE; |
| 1500 | goto error; |
| 1501 | } |
| 1502 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1503 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1504 | if (audioSession == 0) { |
| 1505 | ALOGW("getInputForAttr() unknown session %d on input %d", session, *input); |
| 1506 | status = BAD_VALUE; |
| 1507 | goto error; |
| 1508 | } |
| 1509 | // For MMAP mode, the first call to getInputForAttr() is made on behalf of audioflinger. |
| 1510 | // The second call is for the first active client and sets the UID. Any further call |
| 1511 | // corresponds to a new client and is only permitted from the same UId. |
| 1512 | if (audioSession->openCount() == 1) { |
| 1513 | audioSession->setUid(uid); |
| 1514 | } else if (audioSession->uid() != uid) { |
| 1515 | ALOGW("getInputForAttr() bad uid %d for session %d uid %d", |
| 1516 | uid, session, audioSession->uid()); |
| 1517 | status = INVALID_OPERATION; |
| 1518 | goto error; |
| 1519 | } |
| 1520 | audioSession->changeOpenCount(1); |
| 1521 | *inputType = API_INPUT_LEGACY; |
| 1522 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1523 | *portId = AudioPort::getNextUniqueId(); |
| 1524 | } |
| 1525 | inputDevices = mAvailableInputDevices.getDevicesFromType(inputDesc->mDevice); |
| 1526 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1527 | : AUDIO_PORT_HANDLE_NONE; |
| 1528 | ALOGI("%s reusing MMAP input %d for session %d", __FUNCTION__, *input, session); |
| 1529 | |
| 1530 | return NO_ERROR; |
| 1531 | } |
| 1532 | |
| 1533 | *input = AUDIO_IO_HANDLE_NONE; |
| 1534 | *inputType = API_INPUT_INVALID; |
| 1535 | |
| 1536 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1537 | inputSource = AUDIO_SOURCE_MIC; |
| 1538 | } |
| 1539 | halInputSource = inputSource; |
| 1540 | |
| 1541 | // TODO: check for existing client for this port ID |
| 1542 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1543 | *portId = AudioPort::getNextUniqueId(); |
| 1544 | } |
| 1545 | |
| 1546 | audio_devices_t device; |
| 1547 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1548 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1549 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1550 | status = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
| 1551 | if (status != NO_ERROR) { |
| 1552 | goto error; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1553 | } |
| 1554 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1555 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1556 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1557 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1558 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1559 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1560 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1561 | status = BAD_VALUE; |
| 1562 | goto error; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1563 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1564 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1565 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1566 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1567 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1568 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1569 | // know about it and is therefore considered "legacy" |
| 1570 | *inputType = API_INPUT_LEGACY; |
| 1571 | } else { |
| 1572 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1573 | // an external policy |
| 1574 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1575 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1576 | } else if (audio_is_remote_submix_device(device)) { |
| 1577 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1578 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1579 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1580 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1581 | } else { |
| 1582 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1583 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1584 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1588 | config->sample_rate, config->format, config->channel_mask, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1589 | policyMix); |
| 1590 | if (*input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1591 | status = INVALID_OPERATION; |
| 1592 | goto error; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1593 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1594 | |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1595 | inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1596 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1597 | : AUDIO_PORT_HANDLE_NONE; |
| 1598 | |
| 1599 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d", |
| 1600 | *input, *inputType, *selectedDeviceId); |
| 1601 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1602 | return NO_ERROR; |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 1603 | |
| 1604 | error: |
| 1605 | mInputRoutes.removeRoute(session); |
| 1606 | return status; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | |
| 1610 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1611 | String8 address, |
| 1612 | audio_session_t session, |
| 1613 | uid_t uid, |
| 1614 | audio_source_t inputSource, |
| 1615 | uint32_t samplingRate, |
| 1616 | audio_format_t format, |
| 1617 | audio_channel_mask_t channelMask, |
| 1618 | audio_input_flags_t flags, |
| 1619 | AudioMix *policyMix) |
| 1620 | { |
| 1621 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1622 | audio_source_t halInputSource = inputSource; |
| 1623 | bool isSoundTrigger = false; |
| 1624 | |
| 1625 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1626 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1627 | if (index >= 0) { |
| 1628 | input = mSoundTriggerSessions.valueFor(session); |
| 1629 | isSoundTrigger = true; |
| 1630 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1631 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1632 | } else { |
| 1633 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1634 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1635 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1636 | audio_is_linear_pcm(format)) { |
| 1637 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1640 | // find a compatible input profile (not necessarily identical in parameters) |
| 1641 | sp<IOProfile> profile; |
| 1642 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1643 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1644 | audio_format_t profileFormat = format; |
| 1645 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1646 | audio_input_flags_t profileFlags = flags; |
| 1647 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1648 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1649 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1650 | profileFlags); |
| 1651 | if (profile != 0) { |
| 1652 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1653 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1654 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1655 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1656 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1657 | } else { // fail |
Glenn Kasten | faa10a6 | 2017-05-25 15:52:05 -0700 | [diff] [blame] | 1658 | ALOGW("getInputForDevice() could not find profile for device 0x%X, " |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1659 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1660 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1661 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1662 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1663 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1664 | // Pick input sampling rate if not specified by client |
| 1665 | if (samplingRate == 0) { |
| 1666 | samplingRate = profileSamplingRate; |
| 1667 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1668 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1669 | if (profile->getModuleHandle() == 0) { |
| 1670 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1671 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1672 | } |
| 1673 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1674 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1675 | inputSource, |
| 1676 | format, |
| 1677 | samplingRate, |
| 1678 | channelMask, |
| 1679 | flags, |
| 1680 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1681 | isSoundTrigger, |
| 1682 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1683 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1684 | // FIXME: disable concurrent capture until UI is ready |
| 1685 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1686 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1687 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1688 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1689 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1690 | // reuse input if: |
| 1691 | // - it shares the same profile |
| 1692 | // AND |
| 1693 | // - it is not a reroute submix input |
| 1694 | // AND |
| 1695 | // - it is: not used for sound trigger |
| 1696 | // OR |
| 1697 | // used for sound trigger and all clients use the same session ID |
| 1698 | // |
| 1699 | if ((profile == desc->mProfile) && |
| 1700 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1701 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1702 | |
| 1703 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1704 | if (as != 0) { |
| 1705 | // do not allow unmatching properties on same session |
| 1706 | if (as->matches(audioSession)) { |
| 1707 | as->changeOpenCount(1); |
| 1708 | } else { |
| 1709 | ALOGW("getInputForDevice() record with different attributes" |
| 1710 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1711 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1712 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1713 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1714 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1715 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1716 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1717 | // Reuse the already opened input stream on this profile if: |
| 1718 | // - the new capture source is background OR |
| 1719 | // - the path requested configurations match OR |
| 1720 | // - the new source priority is less than the highest source priority on this input |
| 1721 | // If the input stream cannot be reused, close it before opening a new stream |
| 1722 | // on the same profile for the new client so that the requested path configuration |
| 1723 | // can be selected. |
| 1724 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1725 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1726 | desc->mChannelMask != channelMask || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1727 | !audio_formats_match(desc->mFormat, format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1728 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1729 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1730 | reusedInputDesc = desc; |
| 1731 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1732 | } else { |
| 1733 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1734 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1735 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1736 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1737 | } |
| 1738 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1739 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1740 | if (reusedInputDesc != 0) { |
| 1741 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1742 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1743 | audio_session_t currentSession = sessions.keyAt(j); |
| 1744 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1745 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1746 | } |
| 1747 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1748 | #endif |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1749 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1750 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1751 | config.sample_rate = profileSamplingRate; |
| 1752 | config.channel_mask = profileChannelMask; |
| 1753 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1754 | |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1755 | if (address == "") { |
| 1756 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
| 1757 | // the inputs vector must be of size 1, but we don't want to crash here |
| 1758 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1759 | } |
| 1760 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1761 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1762 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1763 | &config, |
| 1764 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1765 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1766 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1767 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1768 | |
| 1769 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1770 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1771 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1772 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1773 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1774 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1775 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1776 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1777 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1778 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1779 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1780 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1783 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1784 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1785 | inputDesc->mFormat = profileFormat; |
| 1786 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1787 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1788 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1789 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1790 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1791 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1792 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1793 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1794 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1797 | //static |
| 1798 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1799 | { |
| 1800 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1801 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1802 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1803 | } |
| 1804 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1805 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1806 | const sp<AudioSession>& audioSession) |
| 1807 | { |
| 1808 | // Do not allow capture if an active voice call is using a software patch and |
| 1809 | // the call TX source device is on the same HW module. |
| 1810 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1811 | // call TX device but this information is not in the audio patch |
| 1812 | if (mCallTxPatch != 0 && |
| 1813 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1814 | return false; |
| 1815 | } |
| 1816 | |
| 1817 | // starting concurrent capture is enabled if: |
| 1818 | // 1) capturing for re-routing |
| 1819 | // 2) capturing for HOTWORD source |
| 1820 | // 3) capturing for FM TUNER source |
| 1821 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1822 | |
| 1823 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1824 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1825 | return true; |
| 1826 | } |
| 1827 | |
| 1828 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1829 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1830 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1831 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1832 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1833 | return false; |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | return true; |
| 1838 | } |
| 1839 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1840 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1841 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1842 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1843 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1844 | unsigned int numModules = 0; |
| 1845 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1846 | |
| 1847 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1848 | if (status == NO_ERROR && numModules != 0) { |
| 1849 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1850 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1851 | if (nModules == NULL) { |
| 1852 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1853 | // ram the next time this function is called. |
| 1854 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1855 | return false; |
| 1856 | } |
| 1857 | |
| 1858 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1859 | if (status == NO_ERROR) { |
| 1860 | soundTriggerSupportsConcurrentCapture = true; |
| 1861 | for (size_t i = 0; i < numModules; ++i) { |
| 1862 | soundTriggerSupportsConcurrentCapture &= |
| 1863 | nModules[i].properties.concurrent_capture; |
| 1864 | } |
| 1865 | } |
| 1866 | free(nModules); |
| 1867 | } |
| 1868 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1869 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1870 | } |
| 1871 | return mSoundTriggerSupportsConcurrentCapture; |
| 1872 | } |
| 1873 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1874 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1875 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1876 | audio_session_t session, |
| 1877 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1878 | { |
| 1879 | ALOGV("startInput() input %d", input); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1880 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1881 | ssize_t index = mInputs.indexOfKey(input); |
| 1882 | if (index < 0) { |
| 1883 | ALOGW("startInput() unknown input %d", input); |
| 1884 | return BAD_VALUE; |
| 1885 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1886 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1887 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1888 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1889 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1890 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1891 | return BAD_VALUE; |
| 1892 | } |
| 1893 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1894 | // FIXME: disable concurrent capture until UI is ready |
| 1895 | #if 0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1896 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1897 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1898 | return INVALID_OPERATION; |
| 1899 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1900 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1901 | if (isInCall()) { |
| 1902 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1903 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1904 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1905 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1906 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1907 | #else |
| 1908 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1909 | if (mCallTxPatch != 0 && |
| 1910 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1911 | ALOGW("startInput(%d) failed: call in progress", input); |
| 1912 | return INVALID_OPERATION; |
| 1913 | } |
| 1914 | |
| 1915 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1916 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1917 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 1918 | |
| 1919 | if (is_virtual_input_device(activeDesc->mDevice)) { |
| 1920 | continue; |
| 1921 | } |
| 1922 | |
Eric Laurent | cb4dae2 | 2017-07-01 19:39:32 -0700 | [diff] [blame] | 1923 | if ((audioSession->flags() & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0 && |
| 1924 | activeDesc->getId() == inputDesc->getId()) { |
| 1925 | continue; |
| 1926 | } |
| 1927 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1928 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1929 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 1930 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1931 | if (activeDesc->hasPreemptedSession(session)) { |
| 1932 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 1933 | "other input %d already started for HOTWORD", |
| 1934 | input, activeDesc->mIoHandle); |
| 1935 | return INVALID_OPERATION; |
| 1936 | } |
| 1937 | } else { |
| 1938 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 1939 | input, activeDesc->mIoHandle); |
| 1940 | return INVALID_OPERATION; |
| 1941 | } |
| 1942 | } else { |
| 1943 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 1944 | ALOGW("startInput(%d) failed: other input %d already started", |
| 1945 | input, activeDesc->mIoHandle); |
| 1946 | return INVALID_OPERATION; |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1951 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 1952 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 1953 | // that's running. |
| 1954 | const bool allowConcurrentWithSoundTrigger = |
| 1955 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 1956 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1957 | // if capture is allowed, preempt currently active HOTWORD captures |
| 1958 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1959 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 1960 | |
| 1961 | if (is_virtual_input_device(activeDesc->mDevice)) { |
| 1962 | continue; |
| 1963 | } |
| 1964 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 1965 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 1966 | continue; |
| 1967 | } |
| 1968 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1969 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1970 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1971 | AudioSessionCollection activeSessions = |
| 1972 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 1973 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1974 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; |
| 1975 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
| 1976 | sessions.add(activeSession); |
| 1977 | inputDesc->setPreemptedSessions(sessions); |
| 1978 | stopInput(activeHandle, activeSession); |
| 1979 | releaseInput(activeHandle, activeSession); |
| 1980 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 1981 | input, activeDesc->mIoHandle); |
| 1982 | } |
| 1983 | } |
| 1984 | } |
| 1985 | #endif |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1986 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1987 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1988 | // are considered for device selection |
| 1989 | audioSession->changeActiveCount(1); |
| 1990 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1991 | // Routing? |
| 1992 | mInputRoutes.incRouteActivity(session); |
| 1993 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1994 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1995 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 1996 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1997 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1998 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1999 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2000 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
| 2001 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2002 | if ((inputDesc->mPolicyMix != NULL) |
| 2003 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2004 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2005 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2006 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2007 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2008 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2009 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 2010 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2011 | SoundTrigger::setCaptureState(true); |
| 2012 | } |
| 2013 | |
| 2014 | // automatically enable the remote submix output when input is started if not |
| 2015 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2016 | // For remote submix (a virtual device), we open only one input per capture request. |
| 2017 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2018 | String8 address = String8(""); |
| 2019 | if (inputDesc->mPolicyMix == NULL) { |
| 2020 | address = String8("0"); |
| 2021 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2022 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2023 | } |
| 2024 | if (address != "") { |
| 2025 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2026 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2027 | address, "remote-submix"); |
| 2028 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2029 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2030 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2033 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2034 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2035 | return NO_ERROR; |
| 2036 | } |
| 2037 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2038 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 2039 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | { |
| 2041 | ALOGV("stopInput() input %d", input); |
| 2042 | ssize_t index = mInputs.indexOfKey(input); |
| 2043 | if (index < 0) { |
| 2044 | ALOGW("stopInput() unknown input %d", input); |
| 2045 | return BAD_VALUE; |
| 2046 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2047 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2048 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2049 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2050 | if (index < 0) { |
| 2051 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 2052 | return BAD_VALUE; |
| 2053 | } |
| 2054 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2055 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | ALOGW("stopInput() input %d already stopped", input); |
| 2057 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2060 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2061 | |
| 2062 | // Routing? |
| 2063 | mInputRoutes.decRouteActivity(session); |
| 2064 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2065 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2066 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2067 | if (inputDesc->isActive()) { |
| 2068 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2069 | } else { |
| 2070 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2071 | if ((inputDesc->mPolicyMix != NULL) |
| 2072 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2073 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2074 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2075 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2076 | |
| 2077 | // automatically disable the remote submix output when input is stopped if not |
| 2078 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2079 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2080 | String8 address = String8(""); |
| 2081 | if (inputDesc->mPolicyMix == NULL) { |
| 2082 | address = String8("0"); |
| 2083 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2084 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2085 | } |
| 2086 | if (address != "") { |
| 2087 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2088 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2089 | address, "remote-submix"); |
| 2090 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2091 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2092 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2093 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2094 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2095 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2096 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2097 | // primary HW module |
| 2098 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2099 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2100 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2101 | SoundTrigger::setCaptureState(false); |
| 2102 | } |
| 2103 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2104 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2105 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2106 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2109 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 2110 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2111 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2112 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2113 | ALOGV("releaseInput() %d", input); |
| 2114 | ssize_t index = mInputs.indexOfKey(input); |
| 2115 | if (index < 0) { |
| 2116 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 2117 | return; |
| 2118 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2119 | |
| 2120 | // Routing |
| 2121 | mInputRoutes.removeRoute(session); |
| 2122 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2123 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 2124 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2125 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2126 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2127 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2128 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2129 | return; |
| 2130 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2131 | |
| 2132 | if (audioSession->openCount() == 0) { |
| 2133 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2134 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2135 | return; |
| 2136 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2137 | |
| 2138 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2139 | inputDesc->removeAudioSession(session); |
| 2140 | } |
| 2141 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2142 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2143 | ALOGV("releaseInput() exit > 0"); |
| 2144 | return; |
| 2145 | } |
| 2146 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2147 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2148 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2149 | ALOGV("releaseInput() exit"); |
| 2150 | } |
| 2151 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2152 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2153 | bool patchRemoved = false; |
| 2154 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2155 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2156 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2157 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2158 | if (patch_index >= 0) { |
| 2159 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2160 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2161 | mAudioPatches.removeItemsAt(patch_index); |
| 2162 | patchRemoved = true; |
| 2163 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2164 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 2165 | } |
| 2166 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2167 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2168 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2169 | |
| 2170 | if (patchRemoved) { |
| 2171 | mpClientInterface->onAudioPatchListUpdate(); |
| 2172 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2173 | } |
| 2174 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2175 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2176 | int indexMin, |
| 2177 | int indexMax) |
| 2178 | { |
| 2179 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2180 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2181 | |
| 2182 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2183 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2184 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2185 | continue; |
| 2186 | } |
| 2187 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2188 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2191 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2192 | int index, |
| 2193 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2194 | { |
| 2195 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2196 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 2197 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2198 | return BAD_VALUE; |
| 2199 | } |
| 2200 | if (!audio_is_output_device(device)) { |
| 2201 | return BAD_VALUE; |
| 2202 | } |
| 2203 | |
| 2204 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2205 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2206 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2207 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2208 | stream, device, index); |
| 2209 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2210 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2211 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2212 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2213 | continue; |
| 2214 | } |
| 2215 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2216 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2217 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2218 | // update volume on all outputs and streams matching the following: |
| 2219 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2220 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2221 | // the requested device |
| 2222 | // - For non default requested device, currently selected device on the output is either the |
| 2223 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2224 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2225 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2226 | status_t status = NO_ERROR; |
| 2227 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2228 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2229 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2230 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2231 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2232 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2233 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2234 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2235 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2236 | continue; |
| 2237 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2238 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2239 | audio_devices_t curStreamDevice = Volume::getDeviceForVolume(getDeviceForStrategy( |
| 2240 | curStrategy, false /*fromCache*/)); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2241 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2242 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2243 | continue; |
| 2244 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2245 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2246 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2247 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2248 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2249 | } else { |
| 2250 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
Jean-Michel Trivi | b7fdce6 | 2017-06-27 19:38:42 -0700 | [diff] [blame] | 2251 | stream, curStreamDevice); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2252 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2253 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2254 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2255 | //FIXME: workaround for truncated touch sounds |
| 2256 | // delayed volume change for system stream to be removed when the problem is |
| 2257 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2258 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2259 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2260 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2261 | if (volStatus != NO_ERROR) { |
| 2262 | status = volStatus; |
| 2263 | } |
| 2264 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2265 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2266 | } |
| 2267 | return status; |
| 2268 | } |
| 2269 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2270 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2271 | int *index, |
| 2272 | audio_devices_t device) |
| 2273 | { |
| 2274 | if (index == NULL) { |
| 2275 | return BAD_VALUE; |
| 2276 | } |
| 2277 | if (!audio_is_output_device(device)) { |
| 2278 | return BAD_VALUE; |
| 2279 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2280 | // 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] | 2281 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2282 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2283 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2284 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2285 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2286 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2287 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2288 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2289 | return NO_ERROR; |
| 2290 | } |
| 2291 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2292 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2293 | { |
| 2294 | // select one output among several suitable for global effects. |
| 2295 | // The priority is as follows: |
| 2296 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2297 | // AudioFlinger will invalidate the track and the offloaded output |
| 2298 | // will be closed causing the effect to be moved to a PCM output. |
| 2299 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2300 | // 3: The primary output |
| 2301 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2302 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2303 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2304 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2305 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2306 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2307 | if (outputs.size() == 0) { |
| 2308 | return AUDIO_IO_HANDLE_NONE; |
| 2309 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2310 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2311 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2312 | bool activeOnly = true; |
| 2313 | |
| 2314 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2315 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2316 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2317 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2318 | |
| 2319 | for (size_t i = 0; i < outputs.size(); i++) { |
| 2320 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
| 2321 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2322 | continue; |
| 2323 | } |
| 2324 | ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x", |
| 2325 | activeOnly, i, desc->mFlags); |
| 2326 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 2327 | outputOffloaded = outputs[i]; |
| 2328 | } |
| 2329 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 2330 | outputDeepBuffer = outputs[i]; |
| 2331 | } |
| 2332 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
| 2333 | outputPrimary = outputs[i]; |
| 2334 | } |
| 2335 | } |
| 2336 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2337 | output = outputOffloaded; |
| 2338 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2339 | output = outputDeepBuffer; |
| 2340 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2341 | output = outputPrimary; |
| 2342 | } else { |
| 2343 | output = outputs[0]; |
| 2344 | } |
| 2345 | activeOnly = false; |
| 2346 | } |
| 2347 | |
| 2348 | if (output != mMusicEffectOutput) { |
| 2349 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2350 | mMusicEffectOutput = output; |
| 2351 | } |
| 2352 | |
| 2353 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2354 | return output; |
| 2355 | } |
| 2356 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2357 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2358 | { |
| 2359 | return selectOutputForMusicEffects(); |
| 2360 | } |
| 2361 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2362 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2363 | audio_io_handle_t io, |
| 2364 | uint32_t strategy, |
| 2365 | int session, |
| 2366 | int id) |
| 2367 | { |
| 2368 | ssize_t index = mOutputs.indexOfKey(io); |
| 2369 | if (index < 0) { |
| 2370 | index = mInputs.indexOfKey(io); |
| 2371 | if (index < 0) { |
| 2372 | ALOGW("registerEffect() unknown io %d", io); |
| 2373 | return INVALID_OPERATION; |
| 2374 | } |
| 2375 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2376 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2379 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2380 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2381 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2382 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2383 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2384 | continue; |
| 2385 | } |
| 2386 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2387 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2388 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2392 | { |
| 2393 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2394 | } |
| 2395 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2396 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2397 | { |
| 2398 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2399 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2400 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2401 | return true; |
| 2402 | } |
| 2403 | } |
| 2404 | return false; |
| 2405 | } |
| 2406 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2407 | // Register a list of custom mixes with their attributes and format. |
| 2408 | // When a mix is registered, corresponding input and output profiles are |
| 2409 | // added to the remote submix hw module. The profile contains only the |
| 2410 | // parameters (sampling rate, format...) specified by the mix. |
| 2411 | // The corresponding input remote submix device is also connected. |
| 2412 | // |
| 2413 | // When a remote submix device is connected, the address is checked to select the |
| 2414 | // appropriate profile and the corresponding input or output stream is opened. |
| 2415 | // |
| 2416 | // When capture starts, getInputForAttr() will: |
| 2417 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2418 | // - 2 if none found, getDeviceForInputSource() will: |
| 2419 | // - 2.1 look for a mix matching the attributes source |
| 2420 | // - 2.2 if none found, default to device selection by policy rules |
| 2421 | // At this time, the corresponding output remote submix device is also connected |
| 2422 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2423 | // after AudioTracks are invalidated |
| 2424 | // |
| 2425 | // When playback starts, getOutputForAttr() will: |
| 2426 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2427 | // - 2 if none found, look for a mix matching the attributes usage |
| 2428 | // - 3 if none found, default to device and output selection by policy rules. |
| 2429 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2430 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2431 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2432 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2433 | status_t res = NO_ERROR; |
| 2434 | |
| 2435 | sp<HwModule> rSubmixModule; |
| 2436 | // examine each mix's route type |
| 2437 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2438 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2439 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2440 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2441 | break; |
| 2442 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2443 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2444 | // Loop back through "remote submix" |
| 2445 | if (rSubmixModule == 0) { |
| 2446 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2447 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2448 | && mHwModules[j]->mHandle != 0) { |
| 2449 | rSubmixModule = mHwModules[j]; |
| 2450 | break; |
| 2451 | } |
| 2452 | } |
| 2453 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2454 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2455 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2456 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2457 | if (rSubmixModule == 0) { |
| 2458 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2459 | res = INVALID_OPERATION; |
| 2460 | break; |
| 2461 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2462 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2463 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2464 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2465 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2466 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2467 | res = INVALID_OPERATION; |
| 2468 | break; |
| 2469 | } |
| 2470 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2471 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2472 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2473 | // stereo and let audio flinger do the channel conversion if needed. |
| 2474 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2475 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2476 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2477 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2478 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2479 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2480 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2481 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2482 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2483 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2484 | address.string(), "remote-submix"); |
| 2485 | } else { |
| 2486 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2487 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2488 | address.string(), "remote-submix"); |
| 2489 | } |
| 2490 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2491 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2492 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2493 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2494 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2495 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2496 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2497 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2498 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2499 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2500 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2501 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2502 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2503 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2504 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2505 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2506 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2507 | } else { |
| 2508 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2509 | } |
| 2510 | break; |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | if (res != NO_ERROR) { |
| 2515 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2516 | i, device, address.string()); |
| 2517 | res = INVALID_OPERATION; |
| 2518 | break; |
| 2519 | } else if (!foundOutput) { |
| 2520 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2521 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2522 | res = INVALID_OPERATION; |
| 2523 | break; |
| 2524 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2525 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2526 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2527 | if (res != NO_ERROR) { |
| 2528 | unregisterPolicyMixes(mixes); |
| 2529 | } |
| 2530 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2534 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2535 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2536 | status_t res = NO_ERROR; |
| 2537 | sp<HwModule> rSubmixModule; |
| 2538 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2539 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2540 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2541 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2542 | if (rSubmixModule == 0) { |
| 2543 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2544 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2545 | && mHwModules[j]->mHandle != 0) { |
| 2546 | rSubmixModule = mHwModules[j]; |
| 2547 | break; |
| 2548 | } |
| 2549 | } |
| 2550 | } |
| 2551 | if (rSubmixModule == 0) { |
| 2552 | res = INVALID_OPERATION; |
| 2553 | continue; |
| 2554 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2555 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2556 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2557 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2558 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2559 | res = INVALID_OPERATION; |
| 2560 | continue; |
| 2561 | } |
| 2562 | |
| 2563 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2564 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2565 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2566 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2567 | address.string(), "remote-submix"); |
| 2568 | } |
| 2569 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2570 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2571 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2572 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2573 | address.string(), "remote-submix"); |
| 2574 | } |
| 2575 | rSubmixModule->removeOutputProfile(address); |
| 2576 | rSubmixModule->removeInputProfile(address); |
| 2577 | |
| 2578 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2579 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2580 | res = INVALID_OPERATION; |
| 2581 | continue; |
| 2582 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2583 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2584 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2585 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2586 | } |
| 2587 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2588 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2589 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2590 | { |
| 2591 | const size_t SIZE = 256; |
| 2592 | char buffer[SIZE]; |
| 2593 | String8 result; |
| 2594 | |
| 2595 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2596 | result.append(buffer); |
| 2597 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2598 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2599 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2600 | result.append(buffer); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2601 | std::string stateLiteral; |
| 2602 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
| 2603 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2604 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2605 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2606 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2607 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2608 | snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2609 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2610 | snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2611 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2612 | snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2613 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2614 | snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2615 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2616 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2617 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2618 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2619 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2620 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2621 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2622 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2623 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2624 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2625 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2626 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2627 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2628 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2629 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2630 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2631 | mHwModules.dump(fd); |
| 2632 | mOutputs.dump(fd); |
| 2633 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2634 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2635 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2636 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2637 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2638 | |
| 2639 | return NO_ERROR; |
| 2640 | } |
| 2641 | |
| 2642 | // This function checks for the parameters which can be offloaded. |
| 2643 | // This can be enhanced depending on the capability of the DSP and policy |
| 2644 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2645 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2646 | { |
| 2647 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2648 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2649 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2650 | offloadInfo.format, |
| 2651 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2652 | offloadInfo.has_video); |
| 2653 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2654 | if (mMasterMono) { |
| 2655 | return false; // no offloading if mono is set. |
| 2656 | } |
| 2657 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2658 | // Check if offload has been disabled |
| 2659 | char propValue[PROPERTY_VALUE_MAX]; |
| 2660 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2661 | if (atoi(propValue) != 0) { |
| 2662 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2663 | return false; |
| 2664 | } |
| 2665 | } |
| 2666 | |
| 2667 | // Check if stream type is music, then only allow offload as of now. |
| 2668 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2669 | { |
| 2670 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2671 | return false; |
| 2672 | } |
| 2673 | |
| 2674 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2675 | const bool allowOffloadWithVideo = |
| 2676 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2677 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2678 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2679 | return false; |
| 2680 | } |
| 2681 | |
| 2682 | //If duration is less than minimum value defined in property, return false |
| 2683 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2684 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2685 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2686 | return false; |
| 2687 | } |
| 2688 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2689 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2690 | return false; |
| 2691 | } |
| 2692 | |
| 2693 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2694 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2695 | // detects there is an active non offloadable effect. |
| 2696 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2697 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2698 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2699 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2700 | return false; |
| 2701 | } |
| 2702 | |
| 2703 | // See if there is a profile to support this. |
| 2704 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2705 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2706 | offloadInfo.sample_rate, |
| 2707 | offloadInfo.format, |
| 2708 | offloadInfo.channel_mask, |
| 2709 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2710 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2711 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2712 | } |
| 2713 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2714 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2715 | audio_port_type_t type, |
| 2716 | unsigned int *num_ports, |
| 2717 | struct audio_port *ports, |
| 2718 | unsigned int *generation) |
| 2719 | { |
| 2720 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2721 | generation == NULL) { |
| 2722 | return BAD_VALUE; |
| 2723 | } |
| 2724 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2725 | if (ports == NULL) { |
| 2726 | *num_ports = 0; |
| 2727 | } |
| 2728 | |
| 2729 | size_t portsWritten = 0; |
| 2730 | size_t portsMax = *num_ports; |
| 2731 | *num_ports = 0; |
| 2732 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2733 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2734 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2735 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2736 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2737 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2738 | continue; |
| 2739 | } |
| 2740 | if (portsWritten < portsMax) { |
| 2741 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2742 | } |
| 2743 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2744 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2745 | } |
| 2746 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2747 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2748 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2749 | continue; |
| 2750 | } |
| 2751 | if (portsWritten < portsMax) { |
| 2752 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2753 | } |
| 2754 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2755 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2759 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2760 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2761 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2762 | } |
| 2763 | *num_ports += mInputs.size(); |
| 2764 | } |
| 2765 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2766 | size_t numOutputs = 0; |
| 2767 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2768 | if (!mOutputs[i]->isDuplicated()) { |
| 2769 | numOutputs++; |
| 2770 | if (portsWritten < portsMax) { |
| 2771 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2772 | } |
| 2773 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2774 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2775 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2779 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2780 | return NO_ERROR; |
| 2781 | } |
| 2782 | |
| 2783 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2784 | { |
| 2785 | return NO_ERROR; |
| 2786 | } |
| 2787 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2788 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2789 | audio_patch_handle_t *handle, |
| 2790 | uid_t uid) |
| 2791 | { |
| 2792 | ALOGV("createAudioPatch()"); |
| 2793 | |
| 2794 | if (handle == NULL || patch == NULL) { |
| 2795 | return BAD_VALUE; |
| 2796 | } |
| 2797 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2798 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2799 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2800 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2801 | return BAD_VALUE; |
| 2802 | } |
| 2803 | // only one source per audio patch supported for now |
| 2804 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2805 | return INVALID_OPERATION; |
| 2806 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2807 | |
| 2808 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2809 | return INVALID_OPERATION; |
| 2810 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2811 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2812 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2813 | return INVALID_OPERATION; |
| 2814 | } |
| 2815 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2816 | |
| 2817 | sp<AudioPatch> patchDesc; |
| 2818 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2819 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2820 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2821 | patch->sources[0].role, |
| 2822 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2823 | #if LOG_NDEBUG == 0 |
| 2824 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2825 | 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] | 2826 | patch->sinks[i].role, |
| 2827 | patch->sinks[i].type); |
| 2828 | } |
| 2829 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2830 | |
| 2831 | if (index >= 0) { |
| 2832 | patchDesc = mAudioPatches.valueAt(index); |
| 2833 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2834 | mUidCached, patchDesc->mUid, uid); |
| 2835 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2836 | return INVALID_OPERATION; |
| 2837 | } |
| 2838 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2839 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2840 | } |
| 2841 | |
| 2842 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2843 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2844 | if (outputDesc == NULL) { |
| 2845 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2846 | return BAD_VALUE; |
| 2847 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2848 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2849 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2850 | if (patchDesc != 0) { |
| 2851 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2852 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2853 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2854 | return BAD_VALUE; |
| 2855 | } |
| 2856 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2857 | DeviceVector devices; |
| 2858 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2859 | // Only support mix to devices connection |
| 2860 | // TODO add support for mix to mix connection |
| 2861 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2862 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2863 | return INVALID_OPERATION; |
| 2864 | } |
| 2865 | sp<DeviceDescriptor> devDesc = |
| 2866 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2867 | if (devDesc == 0) { |
| 2868 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2869 | return BAD_VALUE; |
| 2870 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2871 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2872 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2873 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2874 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2875 | NULL, // updatedSamplingRate |
| 2876 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2877 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2878 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2879 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2880 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2881 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2882 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2883 | return INVALID_OPERATION; |
| 2884 | } |
| 2885 | devices.add(devDesc); |
| 2886 | } |
| 2887 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2888 | return INVALID_OPERATION; |
| 2889 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2890 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2891 | // TODO: reconfigure output format and channels here |
| 2892 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2893 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2894 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2895 | index = mAudioPatches.indexOfKey(*handle); |
| 2896 | if (index >= 0) { |
| 2897 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2898 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2899 | } |
| 2900 | patchDesc = mAudioPatches.valueAt(index); |
| 2901 | patchDesc->mUid = uid; |
| 2902 | ALOGV("createAudioPatch() success"); |
| 2903 | } else { |
| 2904 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2905 | return INVALID_OPERATION; |
| 2906 | } |
| 2907 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2908 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2909 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2910 | // only one sink supported when connecting an input device to a mix |
| 2911 | if (patch->num_sinks > 1) { |
| 2912 | return INVALID_OPERATION; |
| 2913 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2914 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2915 | if (inputDesc == NULL) { |
| 2916 | return BAD_VALUE; |
| 2917 | } |
| 2918 | if (patchDesc != 0) { |
| 2919 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2920 | return BAD_VALUE; |
| 2921 | } |
| 2922 | } |
| 2923 | sp<DeviceDescriptor> devDesc = |
| 2924 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2925 | if (devDesc == 0) { |
| 2926 | return BAD_VALUE; |
| 2927 | } |
| 2928 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2929 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2930 | devDesc->mAddress, |
| 2931 | patch->sinks[0].sample_rate, |
| 2932 | NULL, /*updatedSampleRate*/ |
| 2933 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2934 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2935 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2936 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2937 | // FIXME for the parameter type, |
| 2938 | // and the NONE |
| 2939 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2940 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2941 | return INVALID_OPERATION; |
| 2942 | } |
| 2943 | // TODO: reconfigure output format and channels here |
| 2944 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2945 | devDesc->type(), inputDesc->mIoHandle); |
| 2946 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2947 | index = mAudioPatches.indexOfKey(*handle); |
| 2948 | if (index >= 0) { |
| 2949 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2950 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2951 | } |
| 2952 | patchDesc = mAudioPatches.valueAt(index); |
| 2953 | patchDesc->mUid = uid; |
| 2954 | ALOGV("createAudioPatch() success"); |
| 2955 | } else { |
| 2956 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2957 | return INVALID_OPERATION; |
| 2958 | } |
| 2959 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2960 | // device to device connection |
| 2961 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2962 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2963 | return BAD_VALUE; |
| 2964 | } |
| 2965 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2966 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2967 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2968 | if (srcDeviceDesc == 0) { |
| 2969 | return BAD_VALUE; |
| 2970 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2971 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2972 | //update source and sink with our own data as the data passed in the patch may |
| 2973 | // be incomplete. |
| 2974 | struct audio_patch newPatch = *patch; |
| 2975 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2976 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2977 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2978 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2979 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2980 | return INVALID_OPERATION; |
| 2981 | } |
| 2982 | |
| 2983 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2984 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2985 | if (sinkDeviceDesc == 0) { |
| 2986 | return BAD_VALUE; |
| 2987 | } |
| 2988 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2989 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2990 | // create a software bridge in PatchPanel if: |
| 2991 | // - source and sink devices are on differnt HW modules OR |
| 2992 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2993 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2994 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2995 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2996 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2997 | return INVALID_OPERATION; |
| 2998 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2999 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3000 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3001 | // if the sink device is reachable via an opened output stream, request to go via |
| 3002 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 3003 | audio_io_handle_t output = selectOutput(outputs, |
| 3004 | AUDIO_OUTPUT_FLAG_NONE, |
| 3005 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3006 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 3007 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3008 | if (outputDesc->isDuplicated()) { |
| 3009 | return INVALID_OPERATION; |
| 3010 | } |
| 3011 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 3012 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 3013 | newPatch.num_sources = 2; |
| 3014 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3015 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3016 | } |
| 3017 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 3018 | |
| 3019 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3020 | if (index >= 0) { |
| 3021 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 3022 | } |
| 3023 | |
| 3024 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 3025 | &afPatchHandle, |
| 3026 | 0); |
| 3027 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 3028 | status, afPatchHandle); |
| 3029 | if (status == NO_ERROR) { |
| 3030 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3031 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3032 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 3033 | } else { |
| 3034 | patchDesc->mPatch = newPatch; |
| 3035 | } |
| 3036 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 3037 | *handle = patchDesc->mHandle; |
| 3038 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3039 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3040 | } else { |
| 3041 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3042 | status); |
| 3043 | return INVALID_OPERATION; |
| 3044 | } |
| 3045 | } else { |
| 3046 | return BAD_VALUE; |
| 3047 | } |
| 3048 | } else { |
| 3049 | return BAD_VALUE; |
| 3050 | } |
| 3051 | return NO_ERROR; |
| 3052 | } |
| 3053 | |
| 3054 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3055 | uid_t uid) |
| 3056 | { |
| 3057 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3058 | |
| 3059 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3060 | |
| 3061 | if (index < 0) { |
| 3062 | return BAD_VALUE; |
| 3063 | } |
| 3064 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3065 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3066 | mUidCached, patchDesc->mUid, uid); |
| 3067 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3068 | return INVALID_OPERATION; |
| 3069 | } |
| 3070 | |
| 3071 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3072 | patchDesc->mUid = mUidCached; |
| 3073 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3074 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3075 | if (outputDesc == NULL) { |
| 3076 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3077 | return BAD_VALUE; |
| 3078 | } |
| 3079 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3080 | setOutputDevice(outputDesc, |
| 3081 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3082 | true, |
| 3083 | 0, |
| 3084 | NULL); |
| 3085 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3086 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3087 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3088 | if (inputDesc == NULL) { |
| 3089 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3090 | return BAD_VALUE; |
| 3091 | } |
| 3092 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3093 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3094 | true, |
| 3095 | NULL); |
| 3096 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3097 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3098 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3099 | status, patchDesc->mAfPatchHandle); |
| 3100 | removeAudioPatch(patchDesc->mHandle); |
| 3101 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3102 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3103 | } else { |
| 3104 | return BAD_VALUE; |
| 3105 | } |
| 3106 | } else { |
| 3107 | return BAD_VALUE; |
| 3108 | } |
| 3109 | return NO_ERROR; |
| 3110 | } |
| 3111 | |
| 3112 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3113 | struct audio_patch *patches, |
| 3114 | unsigned int *generation) |
| 3115 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3116 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3117 | return BAD_VALUE; |
| 3118 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3119 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3120 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3121 | } |
| 3122 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3123 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3124 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3125 | ALOGV("setAudioPortConfig()"); |
| 3126 | |
| 3127 | if (config == NULL) { |
| 3128 | return BAD_VALUE; |
| 3129 | } |
| 3130 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3131 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3132 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3133 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3134 | } |
| 3135 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3136 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3137 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3138 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3139 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3140 | if (outputDesc == NULL) { |
| 3141 | return BAD_VALUE; |
| 3142 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3143 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3144 | "setAudioPortConfig() called on duplicated output %d", |
| 3145 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3146 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3147 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3148 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3149 | if (inputDesc == NULL) { |
| 3150 | return BAD_VALUE; |
| 3151 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3152 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3153 | } else { |
| 3154 | return BAD_VALUE; |
| 3155 | } |
| 3156 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3157 | sp<DeviceDescriptor> deviceDesc; |
| 3158 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3159 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3160 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3161 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3162 | } else { |
| 3163 | return BAD_VALUE; |
| 3164 | } |
| 3165 | if (deviceDesc == NULL) { |
| 3166 | return BAD_VALUE; |
| 3167 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3168 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3169 | } else { |
| 3170 | return BAD_VALUE; |
| 3171 | } |
| 3172 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3173 | struct audio_port_config backupConfig; |
| 3174 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3175 | if (status == NO_ERROR) { |
| 3176 | struct audio_port_config newConfig; |
| 3177 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3178 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3179 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3180 | if (status != NO_ERROR) { |
| 3181 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3182 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3183 | |
| 3184 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3185 | } |
| 3186 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3187 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3188 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3189 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3190 | clearAudioPatches(uid); |
| 3191 | clearSessionRoutes(uid); |
| 3192 | } |
| 3193 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3194 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3195 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3196 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3197 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3198 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3199 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3200 | } |
| 3201 | } |
| 3202 | } |
| 3203 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3204 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3205 | audio_io_handle_t ouptutToSkip) |
| 3206 | { |
| 3207 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3208 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3209 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3210 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3211 | continue; |
| 3212 | } |
| 3213 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3214 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3215 | continue; |
| 3216 | } |
| 3217 | // If the default device for this strategy is on another output mix, |
| 3218 | // invalidate all tracks in this strategy to force re connection. |
| 3219 | // Otherwise select new device on the output mix. |
| 3220 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3221 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3222 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3223 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3224 | } |
| 3225 | } |
| 3226 | } else { |
| 3227 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3228 | setOutputDevice(outputDesc, newDevice, false); |
| 3229 | } |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3234 | { |
| 3235 | // remove output routes associated with this uid |
| 3236 | SortedVector<routing_strategy> affectedStrategies; |
| 3237 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3238 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3239 | if (route->mUid == uid) { |
| 3240 | mOutputRoutes.removeItemsAt(i); |
| 3241 | if (route->mDeviceDescriptor != 0) { |
| 3242 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3243 | } |
| 3244 | } |
| 3245 | } |
| 3246 | // reroute outputs if necessary |
| 3247 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 3248 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 3249 | } |
| 3250 | |
| 3251 | // remove input routes associated with this uid |
| 3252 | SortedVector<audio_source_t> affectedSources; |
| 3253 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3254 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3255 | if (route->mUid == uid) { |
| 3256 | mInputRoutes.removeItemsAt(i); |
| 3257 | if (route->mDeviceDescriptor != 0) { |
| 3258 | affectedSources.add(route->mSource); |
| 3259 | } |
| 3260 | } |
| 3261 | } |
| 3262 | // reroute inputs if necessary |
| 3263 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3264 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3265 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3266 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3267 | inputsToClose.add(inputDesc->mIoHandle); |
| 3268 | } |
| 3269 | } |
| 3270 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 3271 | closeInput(inputsToClose[i]); |
| 3272 | } |
| 3273 | } |
| 3274 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3275 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3276 | { |
| 3277 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3278 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3279 | if (sourceDesc->mUid == uid) { |
| 3280 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3281 | } |
| 3282 | } |
| 3283 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3284 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3285 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3286 | audio_io_handle_t *ioHandle, |
| 3287 | audio_devices_t *device) |
| 3288 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3289 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3290 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3291 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3292 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3293 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3296 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3297 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3298 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3299 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3300 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3301 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3302 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3303 | return BAD_VALUE; |
| 3304 | } |
| 3305 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3306 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3307 | |
| 3308 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3309 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3310 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3311 | return INVALID_OPERATION; |
| 3312 | } |
| 3313 | |
| 3314 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3315 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3316 | String8(source->ext.device.address)); |
| 3317 | if (srcDeviceDesc == 0) { |
| 3318 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3319 | return BAD_VALUE; |
| 3320 | } |
| 3321 | sp<AudioSourceDescriptor> sourceDesc = |
| 3322 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3323 | |
| 3324 | struct audio_patch dummyPatch; |
| 3325 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3326 | sourceDesc->mPatchDesc = patchDesc; |
| 3327 | |
| 3328 | status_t status = connectAudioSource(sourceDesc); |
| 3329 | if (status == NO_ERROR) { |
| 3330 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3331 | *handle = sourceDesc->getHandle(); |
| 3332 | } |
| 3333 | return status; |
| 3334 | } |
| 3335 | |
| 3336 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3337 | { |
| 3338 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3339 | |
| 3340 | // make sure we only have one patch per source. |
| 3341 | disconnectAudioSource(sourceDesc); |
| 3342 | |
| 3343 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3344 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3345 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3346 | |
| 3347 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3348 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3349 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3350 | |
| 3351 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3352 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3353 | |
| 3354 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3355 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3356 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3357 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3358 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3359 | // create patch between src device and output device |
| 3360 | // create Hwoutput and add to mHwOutputs |
| 3361 | } else { |
| 3362 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3363 | audio_io_handle_t output = |
| 3364 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3365 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3366 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3367 | return INVALID_OPERATION; |
| 3368 | } |
| 3369 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3370 | if (outputDesc->isDuplicated()) { |
| 3371 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3372 | return INVALID_OPERATION; |
| 3373 | } |
| 3374 | // create a special patch with no sink and two sources: |
| 3375 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3376 | // be connected as well as the stream type for volume control |
| 3377 | // - the sink is defined by whatever output device is currently selected for the output |
| 3378 | // though which this patch is routed. |
| 3379 | patch->num_sinks = 0; |
| 3380 | patch->num_sources = 2; |
| 3381 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3382 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3383 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3384 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3385 | &afPatchHandle, |
| 3386 | 0); |
| 3387 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3388 | status, afPatchHandle); |
| 3389 | if (status != NO_ERROR) { |
| 3390 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3391 | __FUNCTION__, status); |
| 3392 | return INVALID_OPERATION; |
| 3393 | } |
| 3394 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3395 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3396 | |
| 3397 | if (status != NO_ERROR) { |
| 3398 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3399 | return status; |
| 3400 | } |
| 3401 | sourceDesc->mSwOutput = outputDesc; |
| 3402 | if (delayMs != 0) { |
| 3403 | usleep(delayMs * 1000); |
| 3404 | } |
| 3405 | } |
| 3406 | |
| 3407 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3408 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3409 | |
| 3410 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3411 | } |
| 3412 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3413 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3414 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3415 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3416 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3417 | if (sourceDesc == 0) { |
| 3418 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3419 | return BAD_VALUE; |
| 3420 | } |
| 3421 | status_t status = disconnectAudioSource(sourceDesc); |
| 3422 | |
| 3423 | mAudioSources.removeItem(handle); |
| 3424 | return status; |
| 3425 | } |
| 3426 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3427 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3428 | { |
| 3429 | if (mMasterMono == mono) { |
| 3430 | return NO_ERROR; |
| 3431 | } |
| 3432 | mMasterMono = mono; |
| 3433 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3434 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3435 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3436 | // |
| 3437 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3438 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3439 | // play back offloaded. |
| 3440 | if (mMasterMono) { |
| 3441 | Vector<audio_io_handle_t> offloaded; |
| 3442 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3443 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3444 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3445 | offloaded.push(desc->mIoHandle); |
| 3446 | } |
| 3447 | } |
| 3448 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3449 | closeOutput(offloaded[i]); |
| 3450 | } |
| 3451 | } |
| 3452 | // update master mono for all remaining outputs |
| 3453 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3454 | updateMono(mOutputs.keyAt(i)); |
| 3455 | } |
| 3456 | return NO_ERROR; |
| 3457 | } |
| 3458 | |
| 3459 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3460 | { |
| 3461 | *mono = mMasterMono; |
| 3462 | return NO_ERROR; |
| 3463 | } |
| 3464 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3465 | float AudioPolicyManager::getStreamVolumeDB( |
| 3466 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3467 | { |
| 3468 | return computeVolume(stream, index, device); |
| 3469 | } |
| 3470 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3471 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3472 | { |
| 3473 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3474 | |
| 3475 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3476 | if (patchDesc == 0) { |
| 3477 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3478 | sourceDesc->mPatchDesc->mHandle); |
| 3479 | return BAD_VALUE; |
| 3480 | } |
| 3481 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3482 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3483 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3484 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3485 | if (swOutputDesc != 0) { |
| 3486 | stopSource(swOutputDesc, stream, false); |
| 3487 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3488 | } else { |
| 3489 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3490 | if (hwOutputDesc != 0) { |
| 3491 | // release patch between src device and output device |
| 3492 | // close Hwoutput and remove from mHwOutputs |
| 3493 | } else { |
| 3494 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3495 | } |
| 3496 | } |
| 3497 | return NO_ERROR; |
| 3498 | } |
| 3499 | |
| 3500 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3501 | audio_io_handle_t output, routing_strategy strategy) |
| 3502 | { |
| 3503 | sp<AudioSourceDescriptor> source; |
| 3504 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3505 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3506 | routing_strategy sourceStrategy = |
| 3507 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3508 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3509 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3510 | source = sourceDesc; |
| 3511 | break; |
| 3512 | } |
| 3513 | } |
| 3514 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3515 | } |
| 3516 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3517 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3518 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3519 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3520 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3521 | { |
| 3522 | return android_atomic_inc(&mAudioPortGeneration); |
| 3523 | } |
| 3524 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3525 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3526 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3527 | static const char *kConfigLocationList[] = |
| 3528 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3529 | static const int kConfigLocationListSize = |
| 3530 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3531 | |
| 3532 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3533 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
| 3534 | status_t ret; |
| 3535 | |
| 3536 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3537 | PolicySerializer serializer; |
| 3538 | snprintf(audioPolicyXmlConfigFile, |
| 3539 | sizeof(audioPolicyXmlConfigFile), |
| 3540 | "%s/%s", |
| 3541 | kConfigLocationList[i], |
| 3542 | AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3543 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3544 | if (ret == NO_ERROR) { |
| 3545 | break; |
| 3546 | } |
| 3547 | } |
| 3548 | return ret; |
| 3549 | } |
| 3550 | #endif |
| 3551 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3552 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3553 | : |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3554 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3555 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3556 | mAudioPortGeneration(1), |
| 3557 | mBeaconMuteRefCount(0), |
| 3558 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3559 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3560 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3561 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame] | 3562 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3563 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3564 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3565 | mUidCached = getuid(); |
| 3566 | mpClientInterface = clientInterface; |
| 3567 | |
| 3568 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3569 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3570 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3571 | bool speakerDrcEnabled = false; |
| 3572 | |
| 3573 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3574 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3575 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3576 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3577 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3578 | if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3579 | #else |
| 3580 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3581 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3582 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3583 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3584 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3585 | #endif |
| 3586 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3587 | config.setDefault(); |
| 3588 | } |
| 3589 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3590 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3591 | |
| 3592 | // 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] | 3593 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3594 | if (!engineInstance) { |
| 3595 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3596 | return; |
| 3597 | } |
| 3598 | // Retrieve the Policy Manager Interface |
| 3599 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3600 | if (mEngine == NULL) { |
| 3601 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3602 | return; |
| 3603 | } |
| 3604 | mEngine->setObserver(this); |
| 3605 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3606 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3607 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3608 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3609 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3610 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3611 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3612 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3613 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3614 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3615 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3616 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3617 | continue; |
| 3618 | } |
| 3619 | // open all output streams needed to access attached devices |
| 3620 | // except for direct output streams that are only opened when they are actually |
| 3621 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3622 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3623 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3624 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3625 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3626 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3627 | if (!outProfile->hasSupportedDevices()) { |
| 3628 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3629 | continue; |
| 3630 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3631 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3632 | mTtsOutputAvailable = true; |
| 3633 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3634 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3635 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3636 | continue; |
| 3637 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3638 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3639 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3640 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3641 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3642 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3643 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3644 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3645 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3646 | if ((profileType & outputDeviceTypes) == 0) { |
| 3647 | continue; |
| 3648 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3649 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3650 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3651 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3652 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3653 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3654 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3655 | |
| 3656 | outputDesc->mDevice = profileType; |
| 3657 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3658 | config.sample_rate = outputDesc->mSamplingRate; |
| 3659 | config.channel_mask = outputDesc->mChannelMask; |
| 3660 | config.format = outputDesc->mFormat; |
| 3661 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3662 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3663 | &output, |
| 3664 | &config, |
| 3665 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3666 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3667 | &outputDesc->mLatency, |
| 3668 | outputDesc->mFlags); |
| 3669 | |
| 3670 | if (status != NO_ERROR) { |
| 3671 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3672 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3673 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3674 | } else { |
| 3675 | outputDesc->mSamplingRate = config.sample_rate; |
| 3676 | outputDesc->mChannelMask = config.channel_mask; |
| 3677 | outputDesc->mFormat = config.format; |
| 3678 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3679 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3680 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3681 | // give a valid ID to an attached device once confirmed it is reachable |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3682 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3683 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3684 | } |
| 3685 | } |
| 3686 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3687 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3688 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3689 | } |
| 3690 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3691 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3692 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3693 | true, |
| 3694 | 0, |
| 3695 | NULL, |
| 3696 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3697 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3698 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3699 | // open input streams needed to access attached devices to validate |
| 3700 | // mAvailableInputDevices list |
| 3701 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3702 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3703 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3704 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3705 | if (!inProfile->hasSupportedDevices()) { |
| 3706 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3707 | continue; |
| 3708 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3709 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3710 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3711 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3712 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3713 | if ((profileType & inputDeviceTypes) == 0) { |
| 3714 | continue; |
| 3715 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3716 | sp<AudioInputDescriptor> inputDesc = |
| 3717 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3718 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3719 | inputDesc->mDevice = profileType; |
| 3720 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3721 | // find the address |
| 3722 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3723 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3724 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3725 | : String8(""); |
| 3726 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3727 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3728 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3729 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3730 | config.sample_rate = inputDesc->mSamplingRate; |
| 3731 | config.channel_mask = inputDesc->mChannelMask; |
| 3732 | config.format = inputDesc->mFormat; |
| 3733 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3734 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3735 | &input, |
| 3736 | &config, |
| 3737 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3738 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3739 | AUDIO_SOURCE_MIC, |
| 3740 | AUDIO_INPUT_FLAG_NONE); |
| 3741 | |
| 3742 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3743 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3744 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3745 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3746 | // give a valid ID to an attached device once confirmed it is reachable |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 3747 | if (index >= 0) { |
| 3748 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3749 | if (!devDesc->isAttached()) { |
| 3750 | devDesc->attach(mHwModules[i]); |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 3751 | devDesc->importAudioPort(inProfile, true); |
Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 3752 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3753 | } |
| 3754 | } |
| 3755 | mpClientInterface->closeInput(input); |
| 3756 | } else { |
| 3757 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3758 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3759 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3760 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3761 | } |
| 3762 | } |
| 3763 | // make sure all attached devices have been allocated a unique ID |
| 3764 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3765 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3766 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3767 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3768 | continue; |
| 3769 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3770 | // The device is now validated and can be appended to the available devices of the engine |
| 3771 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3772 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3773 | i++; |
| 3774 | } |
| 3775 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3776 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3777 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3778 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3779 | continue; |
| 3780 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3781 | // The device is now validated and can be appended to the available devices of the engine |
| 3782 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3783 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3784 | i++; |
| 3785 | } |
| 3786 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3787 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3788 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3789 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3790 | |
| 3791 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3792 | |
| 3793 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3796 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3797 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3798 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3799 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3800 | } |
| 3801 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3802 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3803 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3804 | mAvailableOutputDevices.clear(); |
| 3805 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3806 | mOutputs.clear(); |
| 3807 | mInputs.clear(); |
| 3808 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3809 | } |
| 3810 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3811 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3812 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3813 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3814 | } |
| 3815 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3816 | // --- |
| 3817 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3818 | void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3819 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3820 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3821 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3822 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3823 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3824 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3825 | } |
| 3826 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3827 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3828 | { |
| 3829 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3830 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3831 | } |
| 3832 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3833 | void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3834 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3835 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3836 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3837 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3838 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3839 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3840 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3841 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3842 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3843 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3844 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3845 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3846 | if (devDesc != 0) { |
| 3847 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3848 | desc->mIoHandle, address.string()); |
| 3849 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3850 | } |
| 3851 | } |
| 3852 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3853 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3854 | audio_policy_dev_state_t state, |
| 3855 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3856 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3857 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3858 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3859 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3860 | |
| 3861 | if (audio_device_is_digital(device)) { |
| 3862 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3863 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3864 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3865 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3866 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3867 | // first list already open outputs that can be routed to this device |
| 3868 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3869 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3870 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3871 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3872 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3873 | outputs.add(mOutputs.keyAt(i)); |
| 3874 | } else { |
| 3875 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3876 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3877 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3878 | } |
| 3879 | } |
| 3880 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3881 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3882 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3883 | { |
| 3884 | if (mHwModules[i]->mHandle == 0) { |
| 3885 | continue; |
| 3886 | } |
| 3887 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3888 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3889 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3890 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3891 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3892 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3893 | profiles.add(profile); |
| 3894 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3895 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3896 | } |
| 3897 | } |
| 3898 | } |
| 3899 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3900 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3901 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3902 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3903 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3904 | return BAD_VALUE; |
| 3905 | } |
| 3906 | |
| 3907 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3908 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3909 | 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] | 3910 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3911 | |
| 3912 | // nothing to do if one output is already opened for this profile |
| 3913 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3914 | for (j = 0; j < outputs.size(); j++) { |
| 3915 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3916 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3917 | // matching profile: save the sample rates, format and channel masks supported |
| 3918 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3919 | if (audio_device_is_digital(device)) { |
| 3920 | devDesc->importAudioPort(profile); |
| 3921 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3922 | break; |
| 3923 | } |
| 3924 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3925 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3926 | continue; |
| 3927 | } |
| 3928 | |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 3929 | ALOGV("opening output for device %08x with params %s profile %p name %s", |
| 3930 | device, address.string(), profile.get(), profile->getName().string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3931 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3932 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3933 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3934 | config.sample_rate = desc->mSamplingRate; |
| 3935 | config.channel_mask = desc->mChannelMask; |
| 3936 | config.format = desc->mFormat; |
| 3937 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3938 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3939 | config.offload_info.format = desc->mFormat; |
| 3940 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3941 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3942 | &output, |
| 3943 | &config, |
| 3944 | &desc->mDevice, |
| 3945 | address, |
| 3946 | &desc->mLatency, |
| 3947 | desc->mFlags); |
| 3948 | if (status == NO_ERROR) { |
| 3949 | desc->mSamplingRate = config.sample_rate; |
| 3950 | desc->mChannelMask = config.channel_mask; |
| 3951 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3952 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3953 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3954 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3955 | char *param = audio_device_address_to_parameter(device, address); |
| 3956 | mpClientInterface->setParameters(output, String8(param)); |
| 3957 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3958 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3959 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3960 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3961 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3962 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3963 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3964 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3965 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3966 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3967 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3968 | config.offload_info.sample_rate = config.sample_rate; |
| 3969 | config.offload_info.channel_mask = config.channel_mask; |
| 3970 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3971 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3972 | &output, |
| 3973 | &config, |
| 3974 | &desc->mDevice, |
| 3975 | address, |
| 3976 | &desc->mLatency, |
| 3977 | desc->mFlags); |
| 3978 | if (status == NO_ERROR) { |
| 3979 | desc->mSamplingRate = config.sample_rate; |
| 3980 | desc->mChannelMask = config.channel_mask; |
| 3981 | desc->mFormat = config.format; |
| 3982 | } else { |
| 3983 | output = AUDIO_IO_HANDLE_NONE; |
| 3984 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3985 | } |
| 3986 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3987 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3988 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3989 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3990 | sp<AudioPolicyMix> policyMix; |
| 3991 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3992 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3993 | address.string()); |
| 3994 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3995 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3996 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3997 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3998 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 3999 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4000 | // no duplicated output for direct outputs and |
| 4001 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4002 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4003 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4004 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4005 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4007 | //TODO: configure audio effect output stage here |
| 4008 | |
| 4009 | // open a duplicating output thread for the new output and the primary output |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4010 | duplicatedOutput = |
| 4011 | mpClientInterface->openDuplicateOutput(output, |
| 4012 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4013 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4014 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4015 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4016 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4017 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 4018 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4019 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 4020 | dupOutputDesc->mFormat = desc->mFormat; |
| 4021 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 4022 | dupOutputDesc->mLatency = desc->mLatency; |
| 4023 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4024 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4025 | } else { |
| 4026 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4027 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4028 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4029 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4030 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4031 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4032 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4033 | } |
| 4034 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4035 | } else { |
| 4036 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4037 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4038 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4039 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4040 | profiles.removeAt(profile_index); |
| 4041 | profile_index--; |
| 4042 | } else { |
| 4043 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4044 | // Load digital format info only for digital devices |
| 4045 | if (audio_device_is_digital(device)) { |
| 4046 | devDesc->importAudioPort(profile); |
| 4047 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4048 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4049 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4050 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4051 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4052 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4053 | NULL/*patch handle*/, address.string()); |
| 4054 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4055 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | if (profiles.isEmpty()) { |
| 4060 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4061 | return BAD_VALUE; |
| 4062 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4063 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4064 | // check if one opened output is not needed any more after disconnecting one device |
| 4065 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4066 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4067 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4068 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4069 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4070 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4071 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4072 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4073 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4074 | mOutputs.keyAt(i)); |
| 4075 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4076 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4077 | } |
| 4078 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4079 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4080 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4081 | { |
| 4082 | if (mHwModules[i]->mHandle == 0) { |
| 4083 | continue; |
| 4084 | } |
| 4085 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 4086 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4087 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4088 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4089 | ALOGV("checkOutputsForDevice(): " |
| 4090 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4091 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4092 | } |
| 4093 | } |
| 4094 | } |
| 4095 | } |
| 4096 | return NO_ERROR; |
| 4097 | } |
| 4098 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4099 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4100 | audio_policy_dev_state_t state, |
| 4101 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4102 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4103 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4104 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4105 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4106 | |
| 4107 | if (audio_device_is_digital(device)) { |
| 4108 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4109 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4110 | } |
| 4111 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4112 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4113 | // first list already open inputs that can be routed to this device |
| 4114 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4115 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4116 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4117 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4118 | inputs.add(mInputs.keyAt(input_index)); |
| 4119 | } |
| 4120 | } |
| 4121 | |
| 4122 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4123 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4124 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 4125 | { |
| 4126 | if (mHwModules[module_idx]->mHandle == 0) { |
| 4127 | continue; |
| 4128 | } |
| 4129 | for (size_t profile_index = 0; |
| 4130 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 4131 | profile_index++) |
| 4132 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4133 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 4134 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4135 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4136 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4137 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4138 | profiles.add(profile); |
| 4139 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 4140 | profile_index, module_idx); |
| 4141 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | |
| 4146 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4147 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4148 | return BAD_VALUE; |
| 4149 | } |
| 4150 | |
| 4151 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4152 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4153 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4154 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4155 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4156 | // nothing to do if one input is already opened for this profile |
| 4157 | size_t input_index; |
| 4158 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4159 | desc = mInputs.valueAt(input_index); |
| 4160 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4161 | if (audio_device_is_digital(device)) { |
| 4162 | devDesc->importAudioPort(profile); |
| 4163 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4164 | break; |
| 4165 | } |
| 4166 | } |
| 4167 | if (input_index != mInputs.size()) { |
| 4168 | continue; |
| 4169 | } |
| 4170 | |
| 4171 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 4172 | desc = new AudioInputDescriptor(profile); |
| 4173 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4174 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4175 | config.sample_rate = desc->mSamplingRate; |
| 4176 | config.channel_mask = desc->mChannelMask; |
| 4177 | config.format = desc->mFormat; |
| 4178 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 83efe1c | 2017-07-09 16:51:08 -0700 | [diff] [blame] | 4179 | |
| 4180 | ALOGV("opening inputput for device %08x with params %s profile %p name %s", |
| 4181 | desc->mDevice, address.string(), profile.get(), profile->getName().string()); |
| 4182 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4183 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4184 | &input, |
| 4185 | &config, |
| 4186 | &desc->mDevice, |
| 4187 | address, |
| 4188 | AUDIO_SOURCE_MIC, |
| 4189 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4190 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4191 | if (status == NO_ERROR) { |
| 4192 | desc->mSamplingRate = config.sample_rate; |
| 4193 | desc->mChannelMask = config.channel_mask; |
| 4194 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4195 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4196 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4197 | char *param = audio_device_address_to_parameter(device, address); |
| 4198 | mpClientInterface->setParameters(input, String8(param)); |
| 4199 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4200 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4201 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4202 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4203 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 4204 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4205 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4206 | } |
| 4207 | |
| 4208 | if (input != 0) { |
| 4209 | addInput(input, desc); |
| 4210 | } |
| 4211 | } // endif input != 0 |
| 4212 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4213 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4214 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4215 | profiles.removeAt(profile_index); |
| 4216 | profile_index--; |
| 4217 | } else { |
| 4218 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4219 | if (audio_device_is_digital(device)) { |
| 4220 | devDesc->importAudioPort(profile); |
| 4221 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4222 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4223 | } |
| 4224 | } // end scan profiles |
| 4225 | |
| 4226 | if (profiles.isEmpty()) { |
| 4227 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4228 | return BAD_VALUE; |
| 4229 | } |
| 4230 | } else { |
| 4231 | // Disconnect |
| 4232 | // check if one opened input is not needed any more after disconnecting one device |
| 4233 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4234 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4235 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4236 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4237 | mInputs.keyAt(input_index)); |
| 4238 | inputs.add(mInputs.keyAt(input_index)); |
| 4239 | } |
| 4240 | } |
| 4241 | // Clear any profiles associated with the disconnected device. |
| 4242 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4243 | if (mHwModules[module_index]->mHandle == 0) { |
| 4244 | continue; |
| 4245 | } |
| 4246 | for (size_t profile_index = 0; |
| 4247 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4248 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4249 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4250 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4251 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4252 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4253 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4254 | } |
| 4255 | } |
| 4256 | } |
| 4257 | } // end disconnect |
| 4258 | |
| 4259 | return NO_ERROR; |
| 4260 | } |
| 4261 | |
| 4262 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4263 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4264 | { |
| 4265 | ALOGV("closeOutput(%d)", output); |
| 4266 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4267 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4268 | if (outputDesc == NULL) { |
| 4269 | ALOGW("closeOutput() unknown output %d", output); |
| 4270 | return; |
| 4271 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4272 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4273 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4274 | // look for duplicated outputs connected to the output being removed. |
| 4275 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4276 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4277 | if (dupOutputDesc->isDuplicated() && |
| 4278 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4279 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4280 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4281 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4282 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4283 | } else { |
| 4284 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4285 | } |
| 4286 | // As all active tracks on duplicated output will be deleted, |
| 4287 | // and as they were also referenced on the other output, the reference |
| 4288 | // count for their stream type must be adjusted accordingly on |
| 4289 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4290 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4291 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4292 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4293 | } |
| 4294 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4295 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4296 | |
| 4297 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4298 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | } |
| 4300 | } |
| 4301 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4302 | nextAudioPortGeneration(); |
| 4303 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4304 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4305 | if (index >= 0) { |
| 4306 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4307 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4308 | mAudioPatches.removeItemsAt(index); |
| 4309 | mpClientInterface->onAudioPatchListUpdate(); |
| 4310 | } |
| 4311 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | AudioParameter param; |
| 4313 | param.add(String8("closing"), String8("true")); |
| 4314 | mpClientInterface->setParameters(output, param.toString()); |
| 4315 | |
| 4316 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4317 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4318 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4322 | { |
| 4323 | ALOGV("closeInput(%d)", input); |
| 4324 | |
| 4325 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4326 | if (inputDesc == NULL) { |
| 4327 | ALOGW("closeInput() unknown input %d", input); |
| 4328 | return; |
| 4329 | } |
| 4330 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4331 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4332 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4333 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4334 | if (index >= 0) { |
| 4335 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4336 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4337 | mAudioPatches.removeItemsAt(index); |
| 4338 | mpClientInterface->onAudioPatchListUpdate(); |
| 4339 | } |
| 4340 | |
| 4341 | mpClientInterface->closeInput(input); |
| 4342 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4343 | } |
| 4344 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4345 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4346 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4347 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4348 | { |
| 4349 | SortedVector<audio_io_handle_t> outputs; |
| 4350 | |
| 4351 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4352 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4353 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4354 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4355 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4356 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4357 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4358 | outputs.add(openOutputs.keyAt(i)); |
| 4359 | } |
| 4360 | } |
| 4361 | return outputs; |
| 4362 | } |
| 4363 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4364 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4365 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4366 | { |
| 4367 | if (outputs1.size() != outputs2.size()) { |
| 4368 | return false; |
| 4369 | } |
| 4370 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4371 | if (outputs1[i] != outputs2[i]) { |
| 4372 | return false; |
| 4373 | } |
| 4374 | } |
| 4375 | return true; |
| 4376 | } |
| 4377 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4378 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4379 | { |
| 4380 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4381 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4382 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4383 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4384 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4385 | // also take into account external policy-related changes: add all outputs which are |
| 4386 | // associated with policies in the "before" and "after" output vectors |
| 4387 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4388 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4389 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4390 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4391 | srcOutputs.add(desc->mIoHandle); |
| 4392 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4393 | } |
| 4394 | } |
| 4395 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4396 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4397 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4398 | dstOutputs.add(desc->mIoHandle); |
| 4399 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4400 | } |
| 4401 | } |
| 4402 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4403 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4404 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4405 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4406 | // mute strategy while moving tracks from one output to another |
| 4407 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4408 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4409 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4410 | setStrategyMute(strategy, true, desc); |
| 4411 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4412 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4413 | sp<AudioSourceDescriptor> source = |
| 4414 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4415 | if (source != 0){ |
| 4416 | connectAudioSource(source); |
| 4417 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | // Move effects associated to this strategy from previous output to new output |
| 4421 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4422 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4423 | } |
| 4424 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4425 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4426 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4427 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | } |
| 4432 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4433 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4434 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4435 | 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] | 4436 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4437 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4438 | 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] | 4439 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4440 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4441 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4442 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4443 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4444 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4445 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4446 | } |
| 4447 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4448 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4449 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4450 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4451 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4452 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4453 | return; |
| 4454 | } |
| 4455 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4456 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4457 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4458 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4459 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4460 | |
| 4461 | // if suspended, restore A2DP output if: |
| 4462 | // ((SCO device is NOT connected) || |
| 4463 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4464 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4465 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4466 | // if not suspended, suspend A2DP output if: |
| 4467 | // (SCO device is connected) && |
| 4468 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4469 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4470 | // |
| 4471 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4472 | if (!isScoConnected || |
| 4473 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4474 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4475 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4476 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4477 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4478 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4479 | |
| 4480 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4481 | mA2dpSuspended = false; |
| 4482 | } |
| 4483 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4484 | if (isScoConnected && |
| 4485 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4486 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4487 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4488 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4489 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4490 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4491 | |
| 4492 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4493 | mA2dpSuspended = true; |
| 4494 | } |
| 4495 | } |
| 4496 | } |
| 4497 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4498 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4499 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4500 | { |
| 4501 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4502 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4503 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4504 | if (index >= 0) { |
| 4505 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4506 | if (patchDesc->mUid != mUidCached) { |
| 4507 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4508 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4509 | return outputDesc->device(); |
| 4510 | } |
| 4511 | } |
| 4512 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4513 | // 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] | 4514 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4515 | // use device for strategy enforced audible |
| 4516 | // 2: we are in call or the strategy phone is active on the output: |
| 4517 | // use device for strategy phone |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4518 | // 3: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4519 | // use device for strategy sonification |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4520 | // 4: the strategy for enforced audible is active but not enforced on the output: |
| 4521 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4522 | // 5: the strategy accessibility is active on the output: |
| 4523 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4524 | // 6: the strategy "respectful" sonification is active on the output: |
| 4525 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4526 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4527 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4528 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4529 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4530 | // 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] | 4531 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4532 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4533 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4534 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4535 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4536 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4537 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4538 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4539 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Jean-Michel Trivi | 178683b | 2017-08-30 18:07:06 -0700 | [diff] [blame] | 4540 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
| 4541 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4542 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4543 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4544 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4545 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4546 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4547 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4548 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4549 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4550 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4551 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4552 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4553 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4554 | } |
| 4555 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4556 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4557 | return device; |
| 4558 | } |
| 4559 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4560 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4561 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4562 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4563 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4564 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4565 | if (index >= 0) { |
| 4566 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4567 | if (patchDesc->mUid != mUidCached) { |
| 4568 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4569 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4570 | return inputDesc->mDevice; |
| 4571 | } |
| 4572 | } |
| 4573 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4574 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
| 4575 | if (isInCall()) { |
| 4576 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4577 | } else if (source != AUDIO_SOURCE_DEFAULT) { |
| 4578 | device = getDeviceAndMixForInputSource(source); |
| 4579 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4580 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4581 | return device; |
| 4582 | } |
| 4583 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4584 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4585 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4586 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4587 | } |
| 4588 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4589 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4590 | return (uint32_t)getStrategy(stream); |
| 4591 | } |
| 4592 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4593 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4594 | // By checking the range of stream before calling getStrategy, we avoid |
| 4595 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4596 | // 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] | 4597 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4598 | return AUDIO_DEVICE_NONE; |
| 4599 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4600 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4601 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4602 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4603 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4604 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4605 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4606 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4607 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4608 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4609 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4610 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4611 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4612 | curDevices |= outputDesc->device(); |
| 4613 | } |
| 4614 | } |
| 4615 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4616 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4617 | |
| 4618 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4619 | and doesn't really need to.*/ |
| 4620 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4621 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4622 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4623 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | return devices; |
| 4625 | } |
| 4626 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4627 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4628 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4629 | 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] | 4630 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4633 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4634 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4635 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4636 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4637 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4638 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4639 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4640 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4641 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4642 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4643 | } |
| 4644 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4645 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4647 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4648 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4649 | updateDevicesAndOutputs(); |
| 4650 | break; |
| 4651 | default: |
| 4652 | break; |
| 4653 | } |
| 4654 | } |
| 4655 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4656 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4657 | |
| 4658 | // skip beacon mute management if a dedicated TTS output is available |
| 4659 | if (mTtsOutputAvailable) { |
| 4660 | return 0; |
| 4661 | } |
| 4662 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4663 | switch(event) { |
| 4664 | case STARTING_OUTPUT: |
| 4665 | mBeaconMuteRefCount++; |
| 4666 | break; |
| 4667 | case STOPPING_OUTPUT: |
| 4668 | if (mBeaconMuteRefCount > 0) { |
| 4669 | mBeaconMuteRefCount--; |
| 4670 | } |
| 4671 | break; |
| 4672 | case STARTING_BEACON: |
| 4673 | mBeaconPlayingRefCount++; |
| 4674 | break; |
| 4675 | case STOPPING_BEACON: |
| 4676 | if (mBeaconPlayingRefCount > 0) { |
| 4677 | mBeaconPlayingRefCount--; |
| 4678 | } |
| 4679 | break; |
| 4680 | } |
| 4681 | |
| 4682 | if (mBeaconMuteRefCount > 0) { |
| 4683 | // any playback causes beacon to be muted |
| 4684 | return setBeaconMute(true); |
| 4685 | } else { |
| 4686 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4687 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4688 | } |
| 4689 | } |
| 4690 | |
| 4691 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4692 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4693 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4694 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4695 | if (mBeaconMuted != mute) { |
| 4696 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4697 | ALOGV("\t muting %d", mute); |
| 4698 | uint32_t maxLatency = 0; |
| 4699 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4700 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4701 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4702 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4703 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4704 | const uint32_t latency = desc->latency() * 2; |
| 4705 | if (latency > maxLatency) { |
| 4706 | maxLatency = latency; |
| 4707 | } |
| 4708 | } |
| 4709 | mBeaconMuted = mute; |
| 4710 | return maxLatency; |
| 4711 | } |
| 4712 | return 0; |
| 4713 | } |
| 4714 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4715 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4716 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4717 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4718 | // Routing |
| 4719 | // see if we have an explicit route |
| 4720 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4721 | // (getStrategy(stream)). |
| 4722 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 4723 | // and activity count is non-zero and the device in the route descriptor is available |
| 4724 | // then select this device. |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4725 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4726 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4727 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 4728 | if ((routeStrategy == strategy) && route->isActive() && |
| 4729 | (mAvailableOutputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4730 | return route->mDeviceDescriptor->type(); |
| 4731 | } |
| 4732 | } |
| 4733 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4734 | if (fromCache) { |
| 4735 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4736 | strategy, mDeviceForStrategy[strategy]); |
| 4737 | return mDeviceForStrategy[strategy]; |
| 4738 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4739 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4740 | } |
| 4741 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4742 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4743 | { |
| 4744 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4745 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4746 | } |
| 4747 | mPreviousOutputs = mOutputs; |
| 4748 | } |
| 4749 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4750 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4751 | audio_devices_t prevDevice, |
| 4752 | uint32_t delayMs) |
| 4753 | { |
| 4754 | // mute/unmute strategies using an incompatible device combination |
| 4755 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4756 | // if unmuting, unmute only after the specified delay |
| 4757 | if (outputDesc->isDuplicated()) { |
| 4758 | return 0; |
| 4759 | } |
| 4760 | |
| 4761 | uint32_t muteWaitMs = 0; |
| 4762 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4763 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4764 | |
| 4765 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4766 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4767 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4768 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4769 | bool doMute = false; |
| 4770 | |
| 4771 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4772 | doMute = true; |
| 4773 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4774 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4775 | doMute = true; |
| 4776 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4777 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4778 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4779 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4780 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4781 | // skip output if it does not share any device with current output |
| 4782 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4783 | == AUDIO_DEVICE_NONE) { |
| 4784 | continue; |
| 4785 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4786 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4787 | mute ? "muting" : "unmuting", i, curDevice); |
| 4788 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4789 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4790 | if (mute) { |
| 4791 | // FIXME: should not need to double latency if volume could be applied |
| 4792 | // immediately by the audioflinger mixer. We must account for the delay |
| 4793 | // between now and the next time the audioflinger thread for this output |
| 4794 | // will process a buffer (which corresponds to one buffer size, |
| 4795 | // usually 1/2 or 1/4 of the latency). |
| 4796 | if (muteWaitMs < desc->latency() * 2) { |
| 4797 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | } |
| 4803 | } |
| 4804 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4805 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4806 | // different per device volumes |
| 4807 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4808 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4809 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4810 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4811 | if (muteWaitMs < tempMuteWaitMs) { |
| 4812 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4813 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4814 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4815 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4816 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4817 | // make sure that we do not start the temporary mute period too early in case of |
| 4818 | // delayed device change |
| 4819 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4820 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4821 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4822 | } |
| 4823 | } |
| 4824 | } |
| 4825 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4826 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4827 | if (muteWaitMs > delayMs) { |
| 4828 | muteWaitMs -= delayMs; |
| 4829 | usleep(muteWaitMs * 1000); |
| 4830 | return muteWaitMs; |
| 4831 | } |
| 4832 | return 0; |
| 4833 | } |
| 4834 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4835 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4836 | audio_devices_t device, |
| 4837 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4838 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4839 | audio_patch_handle_t *patchHandle, |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 4840 | const char *address, |
| 4841 | bool requiresMuteCheck) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4842 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4843 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4844 | AudioParameter param; |
| 4845 | uint32_t muteWaitMs; |
| 4846 | |
| 4847 | if (outputDesc->isDuplicated()) { |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 4848 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs, |
| 4849 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
| 4850 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs, |
| 4851 | nullptr /* patchHandle */, nullptr /* address */, requiresMuteCheck); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4852 | return muteWaitMs; |
| 4853 | } |
| 4854 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4855 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4856 | if ((device != AUDIO_DEVICE_NONE) && |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 4857 | ((device & outputDesc->supportedDevices()) == AUDIO_DEVICE_NONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4858 | return 0; |
| 4859 | } |
| 4860 | |
| 4861 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4862 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4863 | |
| 4864 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4865 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4866 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4867 | |
| 4868 | if (device != AUDIO_DEVICE_NONE) { |
| 4869 | outputDesc->mDevice = device; |
| 4870 | } |
Andy Hung | 7c7124e | 2017-10-20 12:03:34 -0700 | [diff] [blame] | 4871 | |
| 4872 | // if the outputs are not materially active, there is no need to mute. |
| 4873 | if (requiresMuteCheck) { |
| 4874 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4875 | } else { |
| 4876 | ALOGV("%s: suppressing checkDeviceMuteStrategies", __func__); |
| 4877 | muteWaitMs = 0; |
| 4878 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4879 | |
| 4880 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4881 | // the requested device is AUDIO_DEVICE_NONE |
| 4882 | // OR the requested device is the same as current device |
| 4883 | // AND force is not specified |
| 4884 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4885 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4886 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4887 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4888 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4889 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4890 | return muteWaitMs; |
| 4891 | } |
| 4892 | |
| 4893 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4894 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4895 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4896 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4897 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4898 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4899 | DeviceVector deviceList; |
| 4900 | if ((address == NULL) || (strlen(address) == 0)) { |
| 4901 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 4902 | } else { |
| 4903 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 4904 | } |
| 4905 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4906 | if (!deviceList.isEmpty()) { |
| 4907 | struct audio_patch patch; |
| 4908 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4909 | patch.num_sources = 1; |
| 4910 | patch.num_sinks = 0; |
| 4911 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4912 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4913 | patch.num_sinks++; |
| 4914 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4915 | ssize_t index; |
| 4916 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4917 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4918 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4919 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4920 | } |
| 4921 | sp< AudioPatch> patchDesc; |
| 4922 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4923 | if (index >= 0) { |
| 4924 | patchDesc = mAudioPatches.valueAt(index); |
| 4925 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4926 | } |
| 4927 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4928 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4929 | &afPatchHandle, |
| 4930 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4931 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4932 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4933 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4934 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4935 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4936 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4937 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4938 | } else { |
| 4939 | patchDesc->mPatch = patch; |
| 4940 | } |
| 4941 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4942 | if (patchHandle) { |
| 4943 | *patchHandle = patchDesc->mHandle; |
| 4944 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4945 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4946 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4947 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4948 | } |
| 4949 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4950 | |
| 4951 | // inform all input as well |
| 4952 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4953 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4954 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4955 | AudioParameter inputCmd = AudioParameter(); |
| 4956 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4957 | inputDescriptor->mIoHandle, device); |
| 4958 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4959 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4960 | inputCmd.toString(), |
| 4961 | delayMs); |
| 4962 | } |
| 4963 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4964 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4965 | |
| 4966 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4967 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4968 | |
| 4969 | return muteWaitMs; |
| 4970 | } |
| 4971 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4972 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4973 | int delayMs, |
| 4974 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4975 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4976 | ssize_t index; |
| 4977 | if (patchHandle) { |
| 4978 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4979 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4980 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4981 | } |
| 4982 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4983 | return INVALID_OPERATION; |
| 4984 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4985 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4986 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4987 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4988 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4989 | removeAudioPatch(patchDesc->mHandle); |
| 4990 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4991 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4992 | return status; |
| 4993 | } |
| 4994 | |
| 4995 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4996 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4997 | bool force, |
| 4998 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4999 | { |
| 5000 | status_t status = NO_ERROR; |
| 5001 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5002 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5003 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5004 | inputDesc->mDevice = device; |
| 5005 | |
| 5006 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 5007 | if (!deviceList.isEmpty()) { |
| 5008 | struct audio_patch patch; |
| 5009 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5010 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5011 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5012 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 5013 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5014 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5015 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5016 | patch.num_sinks = 1; |
| 5017 | //only one input device for now |
| 5018 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5019 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5020 | ssize_t index; |
| 5021 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5022 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5023 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5024 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5025 | } |
| 5026 | sp< AudioPatch> patchDesc; |
| 5027 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5028 | if (index >= 0) { |
| 5029 | patchDesc = mAudioPatches.valueAt(index); |
| 5030 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5031 | } |
| 5032 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5033 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5034 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5035 | 0); |
| 5036 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5037 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5038 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5039 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5040 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5041 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5042 | } else { |
| 5043 | patchDesc->mPatch = patch; |
| 5044 | } |
| 5045 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5046 | if (patchHandle) { |
| 5047 | *patchHandle = patchDesc->mHandle; |
| 5048 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5049 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5050 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5051 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5052 | } |
| 5053 | } |
| 5054 | } |
| 5055 | return status; |
| 5056 | } |
| 5057 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5058 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5059 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5060 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5061 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5062 | ssize_t index; |
| 5063 | if (patchHandle) { |
| 5064 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5065 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5066 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5067 | } |
| 5068 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5069 | return INVALID_OPERATION; |
| 5070 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5071 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5072 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5073 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5074 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5075 | removeAudioPatch(patchDesc->mHandle); |
| 5076 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5077 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5078 | return status; |
| 5079 | } |
| 5080 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5081 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5082 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5083 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5084 | audio_format_t& format, |
| 5085 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5086 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5087 | { |
| 5088 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5089 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5090 | // |
| 5091 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5092 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5093 | |
| 5094 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 5095 | { |
| 5096 | if (mHwModules[i]->mHandle == 0) { |
| 5097 | continue; |
| 5098 | } |
| 5099 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 5100 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5101 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5102 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5103 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5104 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5105 | format, |
| 5106 | &format /*updatedFormat*/, |
| 5107 | channelMask, |
| 5108 | &channelMask /*updatedChannelMask*/, |
| 5109 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5110 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5111 | return profile; |
| 5112 | } |
| 5113 | } |
| 5114 | } |
| 5115 | return NULL; |
| 5116 | } |
| 5117 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5118 | |
| 5119 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5120 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5121 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5122 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5123 | audio_devices_t selectedDeviceFromMix = |
| 5124 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5125 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5126 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5127 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5128 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5129 | return getDeviceForInputSource(inputSource); |
| 5130 | } |
| 5131 | |
| 5132 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5133 | { |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5134 | // Routing |
| 5135 | // Scan the whole RouteMap to see if we have an explicit route: |
| 5136 | // if the input source in the RouteMap is the same as the argument above, |
| 5137 | // and activity count is non-zero and the device in the route descriptor is available |
| 5138 | // then select this device. |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5139 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5140 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | ad2e7b9 | 2017-09-14 20:06:42 -0700 | [diff] [blame] | 5141 | if ((inputSource == route->mSource) && route->isActive() && |
| 5142 | (mAvailableInputDevices.indexOf(route->mDeviceDescriptor) >= 0)) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5143 | return route->mDeviceDescriptor->type(); |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5148 | } |
| 5149 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5150 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5151 | int index, |
| 5152 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5153 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5154 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5155 | |
| 5156 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5157 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5158 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5159 | // the ringtone volume |
| 5160 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5161 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5162 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5163 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5164 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5165 | } |
| 5166 | |
Jean-Michel Trivi | 719a987 | 2017-08-05 13:51:35 -0700 | [diff] [blame] | 5167 | // in-call: always cap earpiece volume by voice volume + some low headroom |
| 5168 | if ((stream != AUDIO_STREAM_VOICE_CALL) && (device & AUDIO_DEVICE_OUT_EARPIECE) && isInCall()) { |
| 5169 | switch (stream) { |
| 5170 | case AUDIO_STREAM_SYSTEM: |
| 5171 | case AUDIO_STREAM_RING: |
| 5172 | case AUDIO_STREAM_MUSIC: |
| 5173 | case AUDIO_STREAM_ALARM: |
| 5174 | case AUDIO_STREAM_NOTIFICATION: |
| 5175 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
| 5176 | case AUDIO_STREAM_DTMF: |
| 5177 | case AUDIO_STREAM_ACCESSIBILITY: { |
| 5178 | const float maxVoiceVolDb = computeVolume(AUDIO_STREAM_VOICE_CALL, index, device) |
| 5179 | + IN_CALL_EARPIECE_HEADROOM_DB; |
| 5180 | if (volumeDB > maxVoiceVolDb) { |
| 5181 | ALOGV("computeVolume() stream %d at vol=%f overriden by stream %d at vol=%f", |
| 5182 | stream, volumeDB, AUDIO_STREAM_VOICE_CALL, maxVoiceVolDb); |
| 5183 | volumeDB = maxVoiceVolDb; |
| 5184 | } |
| 5185 | } break; |
| 5186 | default: |
| 5187 | break; |
| 5188 | } |
| 5189 | } |
| 5190 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5191 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5192 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5193 | // - always attenuate notifications volume by 6dB |
| 5194 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5195 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5196 | // - if music is playing, always limit the volume to current music volume, |
| 5197 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5198 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5199 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5200 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5201 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5202 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 5203 | AUDIO_DEVICE_OUT_USB_HEADSET)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5205 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5206 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5208 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5209 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5210 | // when the phone is ringing we must consider that music could have been paused just before |
| 5211 | // by the music application and behave as if music was active if the last music track was |
| 5212 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5213 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5214 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5215 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5216 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5217 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5218 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5219 | musicDevice), |
| 5220 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5221 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5222 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5223 | if (volumeDB > minVolDB) { |
| 5224 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5225 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5226 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5227 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5228 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5229 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5230 | // intended to be played |
| 5231 | if ((volumeDB > -96.0f) && |
| 5232 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5233 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5234 | stream, device, |
| 5235 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5236 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5237 | } |
| 5238 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5239 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5240 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5241 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5242 | } |
| 5243 | } |
| 5244 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5245 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | } |
| 5247 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5248 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5249 | int index, |
| 5250 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5251 | audio_devices_t device, |
| 5252 | int delayMs, |
| 5253 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5254 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5255 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5256 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5257 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5258 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5259 | return NO_ERROR; |
| 5260 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5261 | audio_policy_forced_cfg_t forceUseForComm = |
| 5262 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5263 | // 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] | 5264 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5265 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5266 | 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] | 5267 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5268 | return INVALID_OPERATION; |
| 5269 | } |
| 5270 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5271 | if (device == AUDIO_DEVICE_NONE) { |
| 5272 | device = outputDesc->device(); |
| 5273 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5274 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5275 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5276 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5277 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5278 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5279 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5280 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5281 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5282 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5283 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5284 | float voiceVolume; |
| 5285 | // 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] | 5286 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5287 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5288 | } else { |
| 5289 | voiceVolume = 1.0; |
| 5290 | } |
| 5291 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5292 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5293 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5294 | mLastVoiceVolume = voiceVolume; |
| 5295 | } |
| 5296 | } |
| 5297 | |
| 5298 | return NO_ERROR; |
| 5299 | } |
| 5300 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5301 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5302 | audio_devices_t device, |
| 5303 | int delayMs, |
| 5304 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5305 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5306 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5307 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5308 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5309 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5310 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5311 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5312 | device, |
| 5313 | delayMs, |
| 5314 | force); |
| 5315 | } |
| 5316 | } |
| 5317 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5318 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5319 | bool on, |
| 5320 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5321 | int delayMs, |
| 5322 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5323 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5324 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5325 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5326 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5327 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5328 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5329 | } |
| 5330 | } |
| 5331 | } |
| 5332 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5333 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5334 | bool on, |
| 5335 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5336 | int delayMs, |
| 5337 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5338 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5339 | if (device == AUDIO_DEVICE_NONE) { |
| 5340 | device = outputDesc->device(); |
| 5341 | } |
| 5342 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5343 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5344 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5345 | |
| 5346 | if (on) { |
| 5347 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5348 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5349 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5350 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5351 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5352 | } |
| 5353 | } |
| 5354 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5355 | outputDesc->mMuteCount[stream]++; |
| 5356 | } else { |
| 5357 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5358 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5359 | return; |
| 5360 | } |
| 5361 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5362 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5363 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5364 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5365 | device, |
| 5366 | delayMs); |
| 5367 | } |
| 5368 | } |
| 5369 | } |
| 5370 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5371 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5372 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5373 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5374 | if(!hasPrimaryOutput()) { |
| 5375 | return; |
| 5376 | } |
| 5377 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5378 | // if the stream pertains to sonification strategy and we are in call we must |
| 5379 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5380 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5381 | // interfere with the device used for phone strategy |
| 5382 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5383 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5384 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5385 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5386 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5387 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5388 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5389 | stream, starting, outputDesc->mDevice, stateChange); |
| 5390 | if (outputDesc->mRefCount[stream]) { |
| 5391 | int muteCount = 1; |
| 5392 | if (stateChange) { |
| 5393 | muteCount = outputDesc->mRefCount[stream]; |
| 5394 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5395 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5396 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5397 | for (int i = 0; i < muteCount; i++) { |
| 5398 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5399 | } |
| 5400 | } else { |
| 5401 | ALOGV("handleIncallSonification() high visibility"); |
| 5402 | if (outputDesc->device() & |
| 5403 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5404 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5405 | for (int i = 0; i < muteCount; i++) { |
| 5406 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5407 | } |
| 5408 | } |
| 5409 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5410 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5411 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5412 | } else { |
| 5413 | mpClientInterface->stopTone(); |
| 5414 | } |
| 5415 | } |
| 5416 | } |
| 5417 | } |
| 5418 | } |
| 5419 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5420 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5421 | { |
| 5422 | // flags to stream type mapping |
| 5423 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5424 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5425 | } |
| 5426 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5427 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5428 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5429 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5430 | return AUDIO_STREAM_TTS; |
| 5431 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5432 | |
| 5433 | // usage to stream type mapping |
| 5434 | switch (attr->usage) { |
| 5435 | case AUDIO_USAGE_MEDIA: |
| 5436 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5437 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5438 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5439 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5440 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5441 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5442 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5443 | return AUDIO_STREAM_SYSTEM; |
| 5444 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5445 | return AUDIO_STREAM_VOICE_CALL; |
| 5446 | |
| 5447 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5448 | return AUDIO_STREAM_DTMF; |
| 5449 | |
| 5450 | case AUDIO_USAGE_ALARM: |
| 5451 | return AUDIO_STREAM_ALARM; |
| 5452 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5453 | return AUDIO_STREAM_RING; |
| 5454 | |
| 5455 | case AUDIO_USAGE_NOTIFICATION: |
| 5456 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5457 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5458 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5459 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5460 | return AUDIO_STREAM_NOTIFICATION; |
| 5461 | |
| 5462 | case AUDIO_USAGE_UNKNOWN: |
| 5463 | default: |
| 5464 | return AUDIO_STREAM_MUSIC; |
| 5465 | } |
| 5466 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5467 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5468 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5469 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5470 | // has flags that map to a strategy? |
| 5471 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5472 | return true; |
| 5473 | } |
| 5474 | |
| 5475 | // has known usage? |
| 5476 | switch (paa->usage) { |
| 5477 | case AUDIO_USAGE_UNKNOWN: |
| 5478 | case AUDIO_USAGE_MEDIA: |
| 5479 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5480 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5481 | case AUDIO_USAGE_ALARM: |
| 5482 | case AUDIO_USAGE_NOTIFICATION: |
| 5483 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5484 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5485 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5486 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5487 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5488 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5489 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5490 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5491 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5492 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5493 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5494 | break; |
| 5495 | default: |
| 5496 | return false; |
| 5497 | } |
| 5498 | return true; |
| 5499 | } |
| 5500 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5501 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5502 | routing_strategy strategy, uint32_t inPastMs, |
| 5503 | nsecs_t sysTime) const |
| 5504 | { |
| 5505 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5506 | sysTime = systemTime(); |
| 5507 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5508 | 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] | 5509 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5510 | (NUM_STRATEGIES == strategy)) && |
| 5511 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5512 | return true; |
| 5513 | } |
| 5514 | } |
| 5515 | return false; |
| 5516 | } |
| 5517 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5518 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5519 | { |
| 5520 | return mEngine->getForceUse(usage); |
| 5521 | } |
| 5522 | |
| 5523 | bool AudioPolicyManager::isInCall() |
| 5524 | { |
| 5525 | return isStateInCall(mEngine->getPhoneState()); |
| 5526 | } |
| 5527 | |
| 5528 | bool AudioPolicyManager::isStateInCall(int state) |
| 5529 | { |
| 5530 | return is_state_in_call(state); |
| 5531 | } |
| 5532 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5533 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5534 | { |
| 5535 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5536 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5537 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5538 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5539 | stopAudioSource(sourceDesc->getHandle()); |
| 5540 | } |
| 5541 | } |
| 5542 | |
| 5543 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5544 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5545 | bool release = false; |
| 5546 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5547 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5548 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5549 | source->ext.device.type == deviceDesc->type()) { |
| 5550 | release = true; |
| 5551 | } |
| 5552 | } |
| 5553 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5554 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5555 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5556 | sink->ext.device.type == deviceDesc->type()) { |
| 5557 | release = true; |
| 5558 | } |
| 5559 | } |
| 5560 | if (release) { |
| 5561 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5562 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5563 | } |
| 5564 | } |
| 5565 | } |
| 5566 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5567 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5568 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5569 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5570 | // TODO Set this based on Config properties. |
| 5571 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5572 | |
| 5573 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5574 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5575 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5576 | |
| 5577 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5578 | bool supportsAC3 = false; |
| 5579 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5580 | bool supportsIEC61937 = false; |
| 5581 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5582 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5583 | switch (format) { |
| 5584 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5585 | supportsAC3 = true; |
| 5586 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5587 | case AUDIO_FORMAT_E_AC3: |
| 5588 | case AUDIO_FORMAT_DTS: |
| 5589 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5590 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5591 | break; |
| 5592 | case AUDIO_FORMAT_IEC61937: |
| 5593 | supportsIEC61937 = true; |
| 5594 | break; |
| 5595 | default: |
| 5596 | break; |
| 5597 | } |
| 5598 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5599 | |
| 5600 | // Modify formats based on surround preferences. |
| 5601 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5602 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5603 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5604 | // Remove surround sound related formats. |
| 5605 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5606 | audio_format_t format = formats[formatIndex]; |
| 5607 | switch(format) { |
| 5608 | case AUDIO_FORMAT_AC3: |
| 5609 | case AUDIO_FORMAT_E_AC3: |
| 5610 | case AUDIO_FORMAT_DTS: |
| 5611 | case AUDIO_FORMAT_DTS_HD: |
| 5612 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5613 | formats.removeAt(formatIndex); |
| 5614 | break; |
| 5615 | default: |
| 5616 | formatIndex++; // keep it |
| 5617 | break; |
| 5618 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5619 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5620 | supportsAC3 = false; |
| 5621 | supportsOtherSurround = false; |
| 5622 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5623 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5624 | } else { // AUTO or ALWAYS |
| 5625 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5626 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5627 | && !supportsAC3) { |
| 5628 | formats.add(AUDIO_FORMAT_AC3); |
| 5629 | supportsAC3 = true; |
| 5630 | } |
| 5631 | |
| 5632 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5633 | // This assumes that if any of these formats are reported by the HAL |
| 5634 | // then the report is valid and should not be modified. |
| 5635 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5636 | && !supportsOtherSurround) { |
| 5637 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5638 | formats.add(AUDIO_FORMAT_DTS); |
| 5639 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5640 | supportsOtherSurround = true; |
| 5641 | } |
| 5642 | |
| 5643 | // Add support for IEC61937 if any raw surround supported. |
| 5644 | // The HAL could do this but add it here, just in case. |
| 5645 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5646 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5647 | supportsIEC61937 = true; |
| 5648 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5649 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | // Modify the list of channel masks supported. |
| 5653 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5654 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5655 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5656 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5657 | |
| 5658 | // If NEVER, then remove support for channelMasks > stereo. |
| 5659 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5660 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5661 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5662 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5663 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5664 | channelMasks.removeAt(maskIndex); |
| 5665 | } else { |
| 5666 | maskIndex++; |
| 5667 | } |
| 5668 | } |
| 5669 | // If ALWAYS, then make sure we at least support 5.1 |
| 5670 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5671 | bool supports5dot1 = false; |
| 5672 | // Are there any channel masks that can be considered "surround"? |
| 5673 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5674 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5675 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5676 | supports5dot1 = true; |
| 5677 | break; |
| 5678 | } |
| 5679 | } |
| 5680 | // If not then add 5.1 support. |
| 5681 | if (!supports5dot1) { |
| 5682 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5683 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5684 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5685 | } |
| 5686 | } |
| 5687 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5688 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5689 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5690 | AudioProfileVector &profiles) |
| 5691 | { |
| 5692 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5693 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5694 | // Format MUST be checked first to update the list of AudioProfile |
| 5695 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5696 | reply = mpClientInterface->getParameters( |
| 5697 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5698 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5699 | AudioParameter repliedParameters(reply); |
| 5700 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5701 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5702 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5703 | return; |
| 5704 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5705 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5706 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5707 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5708 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5709 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5710 | } |
| 5711 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5712 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5713 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5714 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5715 | ChannelsVector channelMasks; |
| 5716 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5717 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5718 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5719 | |
| 5720 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5721 | reply = mpClientInterface->getParameters( |
| 5722 | ioHandle, |
| 5723 | requestedParameters.toString() + ";" + |
| 5724 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5725 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5726 | AudioParameter repliedParameters(reply); |
| 5727 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5728 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5729 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5730 | } |
| 5731 | } |
| 5732 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5733 | reply = mpClientInterface->getParameters(ioHandle, |
| 5734 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5735 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5736 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5737 | AudioParameter repliedParameters(reply); |
| 5738 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5739 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5740 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5741 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5742 | filterSurroundChannelMasks(&channelMasks); |
| 5743 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5744 | } |
| 5745 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5746 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5747 | } |
| 5748 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5749 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5750 | }; // namespace android |