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 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- |
| 58 | // AudioPolicyInterface implementation |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 61 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 62 | audio_policy_dev_state_t state, |
| 63 | const char *device_address, |
| 64 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 65 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 66 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 67 | } |
| 68 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 69 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 70 | audio_policy_dev_state_t state, |
| 71 | const String8 &device_address) |
| 72 | { |
| 73 | AudioParameter param(device_address); |
| 74 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 75 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 76 | param.addInt(key, device); |
| 77 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 78 | } |
| 79 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 80 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 81 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 82 | const char *device_address, |
| 83 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 84 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 85 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 86 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | |
| 88 | // connect/disconnect only 1 device at a time |
| 89 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 90 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 91 | sp<DeviceDescriptor> devDesc = |
| 92 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 93 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 94 | // handle output devices |
| 95 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 96 | SortedVector <audio_io_handle_t> outputs; |
| 97 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 99 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 100 | // save a copy of the opened output descriptors before any output is opened or closed |
| 101 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 102 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 103 | switch (state) |
| 104 | { |
| 105 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 106 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 107 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 108 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 109 | return INVALID_OPERATION; |
| 110 | } |
| 111 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 112 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 113 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 114 | index = mAvailableOutputDevices.add(devDesc); |
| 115 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 116 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 117 | if (module == 0) { |
| 118 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 119 | device); |
| 120 | mAvailableOutputDevices.remove(devDesc); |
| 121 | return INVALID_OPERATION; |
| 122 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 123 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 124 | } else { |
| 125 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 126 | } |
| 127 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 128 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 129 | // parameters on newly connected devices (instead of opening the outputs...) |
| 130 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 131 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 132 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 133 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 134 | |
| 135 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 136 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 137 | return INVALID_OPERATION; |
| 138 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 139 | // Propagate device availability to Engine |
| 140 | mEngine->setDeviceConnectionState(devDesc, state); |
| 141 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 142 | // outputs should never be empty here |
| 143 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 144 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 145 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 146 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 147 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 148 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 149 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 150 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 151 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 153 | return INVALID_OPERATION; |
| 154 | } |
| 155 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 156 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 157 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 158 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 159 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 160 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 163 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 164 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 165 | |
| 166 | // Propagate device availability to Engine |
| 167 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 168 | } break; |
| 169 | |
| 170 | default: |
| 171 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 172 | return BAD_VALUE; |
| 173 | } |
| 174 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 175 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 176 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 177 | checkA2dpSuspend(); |
| 178 | checkOutputForAllStrategies(); |
| 179 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 180 | if (!outputs.isEmpty()) { |
| 181 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 182 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | // close unused outputs after device disconnection or direct outputs that have been |
| 184 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 185 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 186 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 187 | (desc->mDirectOpenCount == 0))) { |
| 188 | closeOutput(outputs[i]); |
| 189 | } |
| 190 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 191 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 192 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 196 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 197 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 198 | updateCallRouting(newDevice); |
| 199 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 201 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 202 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 203 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 204 | // do not force device change on duplicated output because if device is 0, it will |
| 205 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 206 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 207 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 208 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 209 | // always force when disconnecting (a non-duplicated device) |
| 210 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 211 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 212 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 215 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 216 | cleanUpForDevice(devDesc); |
| 217 | } |
| 218 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 219 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 220 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 221 | } // end if is output device |
| 222 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 | // handle input devices |
| 224 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 225 | SortedVector <audio_io_handle_t> inputs; |
| 226 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 227 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | switch (state) |
| 229 | { |
| 230 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 231 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 232 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 234 | return INVALID_OPERATION; |
| 235 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 236 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 237 | if (module == NULL) { |
| 238 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 239 | device); |
| 240 | return INVALID_OPERATION; |
| 241 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 242 | |
| 243 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 244 | // parameters on newly connected devices (instead of opening the inputs...) |
| 245 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 246 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 247 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 248 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 249 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 250 | return INVALID_OPERATION; |
| 251 | } |
| 252 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 253 | index = mAvailableInputDevices.add(devDesc); |
| 254 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 255 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 256 | } else { |
| 257 | return NO_MEMORY; |
| 258 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 259 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 260 | // Propagate device availability to Engine |
| 261 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 262 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | |
| 264 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 265 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 266 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 268 | return INVALID_OPERATION; |
| 269 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 270 | |
| 271 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 272 | |
| 273 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 274 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 275 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 276 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 277 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 278 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 279 | // Propagate device availability to Engine |
| 280 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 281 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 282 | |
| 283 | default: |
| 284 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 285 | return BAD_VALUE; |
| 286 | } |
| 287 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 288 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 289 | // As the input device list can impact the output device selection, update |
| 290 | // getDeviceForStrategy() cache |
| 291 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 293 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 294 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 295 | updateCallRouting(newDevice); |
| 296 | } |
| 297 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 298 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 299 | cleanUpForDevice(devDesc); |
| 300 | } |
| 301 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 302 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 304 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | |
| 306 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 307 | return BAD_VALUE; |
| 308 | } |
| 309 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 310 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 311 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 313 | sp<DeviceDescriptor> devDesc = |
| 314 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 315 | (strlen(device_address) != 0)/*matchAddress*/); |
| 316 | |
| 317 | if (devDesc == 0) { |
| 318 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 319 | device, device_address); |
| 320 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 321 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 322 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 323 | DeviceVector *deviceVector; |
| 324 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 325 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 326 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 327 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 328 | deviceVector = &mAvailableInputDevices; |
| 329 | } else { |
| 330 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 331 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 333 | |
| 334 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 335 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 338 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 339 | const char *device_address, |
| 340 | const char *device_name) |
| 341 | { |
| 342 | status_t status; |
| 343 | |
| 344 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 345 | device, device_address, device_name); |
| 346 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 347 | // connect/disconnect only 1 device at a time |
| 348 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 349 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 350 | // Check if the device is currently connected |
| 351 | sp<DeviceDescriptor> devDesc = |
| 352 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 353 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 354 | if (index < 0) { |
| 355 | // Nothing to do: device is not connected |
| 356 | return NO_ERROR; |
| 357 | } |
| 358 | |
| 359 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 360 | // This will force reading again the device configuration |
| 361 | status = setDeviceConnectionState(device, |
| 362 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 363 | device_address, device_name); |
| 364 | if (status != NO_ERROR) { |
| 365 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 366 | status); |
| 367 | return status; |
| 368 | } |
| 369 | |
| 370 | status = setDeviceConnectionState(device, |
| 371 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 372 | device_address, device_name); |
| 373 | if (status != NO_ERROR) { |
| 374 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 375 | status); |
| 376 | return status; |
| 377 | } |
| 378 | |
| 379 | return NO_ERROR; |
| 380 | } |
| 381 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 382 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 383 | { |
| 384 | bool createTxPatch = false; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 385 | status_t status; |
| 386 | audio_patch_handle_t afPatchHandle; |
| 387 | DeviceVector deviceList; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 388 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 389 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 390 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 391 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 392 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 393 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 394 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 395 | |
| 396 | // release existing RX patch if any |
| 397 | if (mCallRxPatch != 0) { |
| 398 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 399 | mCallRxPatch.clear(); |
| 400 | } |
| 401 | // release TX patch if any |
| 402 | if (mCallTxPatch != 0) { |
| 403 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 404 | mCallTxPatch.clear(); |
| 405 | } |
| 406 | |
| 407 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 408 | // via setOutputDevice() on primary output. |
| 409 | // Otherwise, create two audio patches for TX and RX path. |
| 410 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 411 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 412 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 413 | // of it due to legacy implementation. If not, create a patch. |
| 414 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 415 | == AUDIO_DEVICE_NONE) { |
| 416 | createTxPatch = true; |
| 417 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 418 | } else { // create RX path audio patch |
| 419 | struct audio_patch patch; |
| 420 | |
| 421 | patch.num_sources = 1; |
| 422 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 423 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 424 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 425 | "updateCallRouting() selected device not in output device list"); |
| 426 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 427 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 428 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 429 | "updateCallRouting() no telephony RX device"); |
| 430 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 431 | |
| 432 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 433 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 434 | |
| 435 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 436 | SortedVector<audio_io_handle_t> outputs = |
| 437 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 438 | audio_io_handle_t output = selectOutput(outputs, |
| 439 | AUDIO_OUTPUT_FLAG_NONE, |
| 440 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 442 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 443 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 444 | "updateCallRouting() RX device output is duplicated"); |
| 445 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 446 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 447 | patch.num_sources = 2; |
| 448 | } |
| 449 | |
| 450 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 451 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 453 | status); |
| 454 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 455 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 456 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 457 | mCallRxPatch->mUid = mUidCached; |
| 458 | } |
| 459 | createTxPatch = true; |
| 460 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 461 | if (createTxPatch) { // create TX path audio patch |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 462 | struct audio_patch patch; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 463 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 464 | patch.num_sources = 1; |
| 465 | patch.num_sinks = 1; |
| 466 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 467 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 468 | "updateCallRouting() selected device not in input device list"); |
| 469 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 470 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 471 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 472 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 473 | "updateCallRouting() no telephony TX device"); |
| 474 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 475 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 476 | |
| 477 | SortedVector<audio_io_handle_t> outputs = |
| 478 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 479 | audio_io_handle_t output = selectOutput(outputs, |
| 480 | AUDIO_OUTPUT_FLAG_NONE, |
| 481 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 482 | // request to reuse existing output stream if one is already opened to reach the TX |
| 483 | // path output device |
| 484 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 485 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 486 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 487 | "updateCallRouting() RX device output is duplicated"); |
| 488 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 489 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 490 | patch.num_sources = 2; |
| 491 | } |
| 492 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 493 | // terminate active capture if on the same HW module as the call TX source device |
| 494 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 495 | // call TX device but this information is not in the audio patch and logic here must be |
| 496 | // symmetric to the one in startInput() |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 497 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 498 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 499 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 500 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 501 | AudioSessionCollection activeSessions = |
| 502 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 503 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 504 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 505 | stopInput(activeDesc->mIoHandle, activeSession); |
| 506 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 507 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 511 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 512 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 513 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 514 | status); |
| 515 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 516 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 517 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 518 | mCallTxPatch->mUid = mUidCached; |
| 519 | } |
| 520 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 521 | |
| 522 | return muteWaitMs; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 525 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 526 | { |
| 527 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 528 | // store previous phone state for management of sonification strategy below |
| 529 | int oldState = mEngine->getPhoneState(); |
| 530 | |
| 531 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 532 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 533 | return; |
| 534 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 535 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | // if leaving call state, handle special case of active streams |
| 537 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 538 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 539 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 540 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 541 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 542 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 543 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 544 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 545 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 546 | } |
| 547 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 548 | /** |
| 549 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 550 | * routing command. |
| 551 | */ |
| 552 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 553 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 | |
| 555 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | checkA2dpSuspend(); |
| 557 | checkOutputForAllStrategies(); |
| 558 | updateDevicesAndOutputs(); |
| 559 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | int delayMs = 0; |
| 561 | if (isStateInCall(state)) { |
| 562 | nsecs_t sysTime = systemTime(); |
| 563 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 564 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | // mute media and sonification strategies and delay device switch by the largest |
| 566 | // latency of any output where either strategy is active. |
| 567 | // 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] | 568 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 569 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 570 | sysTime) || |
| 571 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 572 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 573 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 574 | (delayMs < (int)desc->latency()*2)) { |
| 575 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 576 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 577 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 578 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 580 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 581 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 583 | } |
| 584 | } |
| 585 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 586 | if (hasPrimaryOutput()) { |
| 587 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 588 | // the device returned is not necessarily reachable via this output |
| 589 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 590 | // force routing command to audio hardware when ending call |
| 591 | // even if no device change is needed |
| 592 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 593 | rxDevice = mPrimaryOutput->device(); |
| 594 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 595 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 596 | if (state == AUDIO_MODE_IN_CALL) { |
| 597 | updateCallRouting(rxDevice, delayMs); |
| 598 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 599 | if (mCallRxPatch != 0) { |
| 600 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 601 | mCallRxPatch.clear(); |
| 602 | } |
| 603 | if (mCallTxPatch != 0) { |
| 604 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 605 | mCallTxPatch.clear(); |
| 606 | } |
| 607 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 608 | } else { |
| 609 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 610 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 611 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 612 | // if entering in call state, handle special case of active streams |
| 613 | // pertaining to sonification strategy see handleIncallSonification() |
| 614 | if (isStateInCall(state)) { |
| 615 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 616 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 617 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 618 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 619 | |
| 620 | // force reevaluating accessibility routing when call starts |
| 621 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | // 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] | 625 | if (state == AUDIO_MODE_RINGTONE && |
| 626 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | mLimitRingtoneVolume = true; |
| 628 | } else { |
| 629 | mLimitRingtoneVolume = false; |
| 630 | } |
| 631 | } |
| 632 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 633 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 634 | return mEngine->getPhoneState(); |
| 635 | } |
| 636 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 637 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 638 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 639 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 640 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 641 | if (config == mEngine->getForceUse(usage)) { |
| 642 | return; |
| 643 | } |
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 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 646 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 647 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 648 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 649 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 650 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 651 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 652 | |
| 653 | // check for device and output changes triggered by new force usage |
| 654 | checkA2dpSuspend(); |
| 655 | checkOutputForAllStrategies(); |
| 656 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 657 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 658 | //FIXME: workaround for truncated touch sounds |
| 659 | // to be removed when the problem is handled by system UI |
| 660 | uint32_t delayMs = 0; |
| 661 | uint32_t waitMs = 0; |
| 662 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 663 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 664 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 665 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 666 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 667 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 668 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 669 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 670 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 671 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 672 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 673 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 674 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 675 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 676 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 677 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 681 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 682 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 683 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 684 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 685 | // 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] | 686 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 687 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 688 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 689 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 690 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 691 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 692 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 695 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 696 | { |
| 697 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 698 | } |
| 699 | |
| 700 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 701 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 702 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 703 | audio_devices_t device, |
| 704 | uint32_t samplingRate, |
| 705 | audio_format_t format, |
| 706 | audio_channel_mask_t channelMask, |
| 707 | audio_output_flags_t flags) |
| 708 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 709 | // only retain flags that will drive the direct output profile selection |
| 710 | // if explicitly requested |
| 711 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 712 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 713 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 714 | flags = |
| 715 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 716 | |
| 717 | sp<IOProfile> profile; |
| 718 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 719 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 720 | if (mHwModules[i]->mHandle == 0) { |
| 721 | continue; |
| 722 | } |
| 723 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 724 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 725 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 726 | samplingRate, NULL /*updatedSamplingRate*/, |
| 727 | format, NULL /*updatedFormat*/, |
| 728 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 729 | flags)) { |
| 730 | continue; |
| 731 | } |
| 732 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 733 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 734 | continue; |
| 735 | } |
| 736 | // 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] | 737 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 738 | continue; |
| 739 | } |
| 740 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 741 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 742 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 743 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 746 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 749 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 750 | uint32_t samplingRate, |
| 751 | audio_format_t format, |
| 752 | audio_channel_mask_t channelMask, |
| 753 | audio_output_flags_t flags, |
| 754 | const audio_offload_info_t *offloadInfo) |
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*/); |
| 758 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 759 | device, stream, samplingRate, format, channelMask, flags); |
| 760 | |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 761 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, stream, samplingRate, format, |
| 762 | channelMask, flags, offloadInfo); |
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 | #ifdef AUDIO_POLICY_TEST |
| 873 | if (mCurOutput != 0) { |
| 874 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 875 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 876 | |
| 877 | if (mTestOutputs[mCurOutput] == 0) { |
| 878 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 879 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 880 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 881 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 882 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 883 | outputDesc->mFlags = |
| 884 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 885 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 886 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 887 | config.sample_rate = mTestSamplingRate; |
| 888 | config.channel_mask = mTestChannels; |
| 889 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 890 | if (offloadInfo != NULL) { |
| 891 | config.offload_info = *offloadInfo; |
| 892 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 893 | status = mpClientInterface->openOutput(0, |
| 894 | &mTestOutputs[mCurOutput], |
| 895 | &config, |
| 896 | &outputDesc->mDevice, |
| 897 | String8(""), |
| 898 | &outputDesc->mLatency, |
| 899 | outputDesc->mFlags); |
| 900 | if (status == NO_ERROR) { |
| 901 | outputDesc->mSamplingRate = config.sample_rate; |
| 902 | outputDesc->mFormat = config.format; |
| 903 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 904 | AudioParameter outputCmd = AudioParameter(); |
| 905 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 906 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 907 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 908 | } |
| 909 | } |
| 910 | return mTestOutputs[mCurOutput]; |
| 911 | } |
| 912 | #endif //AUDIO_POLICY_TEST |
| 913 | |
| 914 | // open a direct output if required by specified parameters |
| 915 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 916 | // and all common behaviors are driven by checking only the direct flag |
| 917 | // this should normally be set appropriately in the policy configuration file |
| 918 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 919 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 920 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 921 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 922 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 923 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 924 | // only allow deep buffering for music stream type |
| 925 | if (stream != AUDIO_STREAM_MUSIC) { |
| 926 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 927 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 928 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 929 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 930 | // use DEEP_BUFFER as default output for music stream type |
| 931 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 932 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 933 | if (stream == AUDIO_STREAM_TTS) { |
| 934 | flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 935 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
| 936 | getPhoneState() == AUDIO_MODE_IN_COMMUNICATION && |
| 937 | audio_is_linear_pcm(format)) { |
| 938 | flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
| 939 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 940 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 941 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 942 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 943 | sp<IOProfile> profile; |
| 944 | |
| 945 | // skip direct output selection if the request can obviously be attached to a mixed output |
Eric Laurent | c260784 | 2014-09-29 09:43:03 -0700 | [diff] [blame] | 946 | // and not explicitly requested |
| 947 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 948 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 949 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 950 | goto non_direct_output; |
| 951 | } |
| 952 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 953 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 954 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 955 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 956 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 957 | // This may prevent offloading in rare situations where effects are left active by apps |
| 958 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 959 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 960 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 961 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | profile = getProfileForDirectOutput(device, |
| 963 | samplingRate, |
| 964 | format, |
| 965 | channelMask, |
| 966 | (audio_output_flags_t)flags); |
| 967 | } |
| 968 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 969 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 970 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 971 | |
| 972 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 973 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 975 | outputDesc = desc; |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 976 | // reuse direct output if currently open by the same client |
| 977 | // and configured with same parameters |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 978 | if ((samplingRate == outputDesc->mSamplingRate) && |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 979 | audio_formats_match(format, outputDesc->mFormat) && |
| 980 | (channelMask == outputDesc->mChannelMask)) { |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 981 | if (session == outputDesc->mDirectClientSession) { |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 982 | outputDesc->mDirectOpenCount++; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 983 | ALOGV("getOutput() reusing direct output %d for session %d", |
| 984 | mOutputs.keyAt(i), session); |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 985 | return mOutputs.keyAt(i); |
| 986 | } else { |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 987 | ALOGV("getOutput() do not reuse direct output because current client (%d) " |
| 988 | "is not the same as requesting client (%d)", |
| 989 | outputDesc->mDirectClientSession, session); |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 990 | goto non_direct_output; |
| 991 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | } |
| 995 | // close direct output if currently open and configured with different parameters |
| 996 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 997 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 998 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 999 | |
| 1000 | // if the selected profile is offloaded and no offload info was specified, |
| 1001 | // create a default one |
| 1002 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1003 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1004 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 1005 | defaultOffloadInfo.sample_rate = samplingRate; |
| 1006 | defaultOffloadInfo.channel_mask = channelMask; |
| 1007 | defaultOffloadInfo.format = format; |
| 1008 | defaultOffloadInfo.stream_type = stream; |
| 1009 | defaultOffloadInfo.bit_rate = 0; |
| 1010 | defaultOffloadInfo.duration_us = -1; |
| 1011 | defaultOffloadInfo.has_video = true; // conservative |
| 1012 | defaultOffloadInfo.is_streaming = true; // likely |
| 1013 | offloadInfo = &defaultOffloadInfo; |
| 1014 | } |
| 1015 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1016 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1018 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1019 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1020 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1021 | config.sample_rate = samplingRate; |
| 1022 | config.channel_mask = channelMask; |
| 1023 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 1024 | if (offloadInfo != NULL) { |
| 1025 | config.offload_info = *offloadInfo; |
| 1026 | } |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1027 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 1028 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 1029 | : String8(""); |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1030 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1031 | &output, |
| 1032 | &config, |
| 1033 | &outputDesc->mDevice, |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1034 | address, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1035 | &outputDesc->mLatency, |
| 1036 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1037 | |
| 1038 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1039 | if (status != NO_ERROR || |
| 1040 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1041 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1042 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1043 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 1044 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1045 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1046 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1047 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1048 | mpClientInterface->closeOutput(output); |
| 1049 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1050 | // 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] | 1051 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1052 | goto non_direct_output; |
| 1053 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1054 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1055 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1056 | outputDesc->mSamplingRate = config.sample_rate; |
| 1057 | outputDesc->mChannelMask = config.channel_mask; |
| 1058 | outputDesc->mFormat = config.format; |
| 1059 | outputDesc->mRefCount[stream] = 0; |
| 1060 | outputDesc->mStopTime[stream] = 0; |
| 1061 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1062 | outputDesc->mDirectClientSession = session; |
| 1063 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1064 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1065 | mPreviousOutputs = mOutputs; |
| 1066 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1067 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | return output; |
| 1069 | } |
| 1070 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1071 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1072 | |
| 1073 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1074 | // stamps are embedded in audio data |
| 1075 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1076 | return AUDIO_IO_HANDLE_NONE; |
| 1077 | } |
| 1078 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1079 | // ignoring channel mask due to downmix capability in mixer |
| 1080 | |
| 1081 | // open a non direct output |
| 1082 | |
| 1083 | // for non direct outputs, only PCM is supported |
| 1084 | if (audio_is_linear_pcm(format)) { |
| 1085 | // get which output is suitable for the specified stream. The actual |
| 1086 | // routing change will happen when startOutput() will be called |
| 1087 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1088 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1089 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1090 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1091 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1092 | } |
| 1093 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1094 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1095 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1096 | return output; |
| 1097 | } |
| 1098 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1099 | 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] | 1100 | audio_output_flags_t flags, |
| 1101 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1102 | { |
| 1103 | // select one output among several that provide a path to a particular device or set of |
| 1104 | // devices (the list was previously build by getOutputsForDevice()). |
| 1105 | // The priority is as follows: |
| 1106 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1107 | // 2: the output with the bit depth the closest to the requested one |
| 1108 | // 3: the primary output |
| 1109 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1110 | |
| 1111 | if (outputs.size() == 0) { |
| 1112 | return 0; |
| 1113 | } |
| 1114 | if (outputs.size() == 1) { |
| 1115 | return outputs[0]; |
| 1116 | } |
| 1117 | |
| 1118 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1119 | audio_io_handle_t outputForFlags = 0; |
| 1120 | audio_io_handle_t outputForPrimary = 0; |
| 1121 | audio_io_handle_t outputForFormat = 0; |
| 1122 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1123 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1124 | |
| 1125 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1126 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1127 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1128 | // if a valid format is specified, skip output if not compatible |
| 1129 | if (format != AUDIO_FORMAT_INVALID) { |
| 1130 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1131 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1132 | continue; |
| 1133 | } |
| 1134 | } else if (!audio_is_linear_pcm(format)) { |
| 1135 | continue; |
| 1136 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1137 | if (AudioPort::isBetterFormatMatch( |
| 1138 | outputDesc->mFormat, bestFormat, format)) { |
| 1139 | outputForFormat = outputs[i]; |
| 1140 | bestFormat = outputDesc->mFormat; |
| 1141 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1144 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1145 | if (commonFlags >= maxCommonFlags) { |
| 1146 | if (commonFlags == maxCommonFlags) { |
| 1147 | if (AudioPort::isBetterFormatMatch( |
| 1148 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1149 | outputForFlags = outputs[i]; |
| 1150 | bestFormatForFlags = outputDesc->mFormat; |
| 1151 | } |
| 1152 | } else { |
| 1153 | outputForFlags = outputs[i]; |
| 1154 | maxCommonFlags = commonFlags; |
| 1155 | bestFormatForFlags = outputDesc->mFormat; |
| 1156 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1157 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1158 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1159 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1160 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1165 | if (outputForFlags != 0) { |
| 1166 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1167 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1168 | if (outputForFormat != 0) { |
| 1169 | return outputForFormat; |
| 1170 | } |
| 1171 | if (outputForPrimary != 0) { |
| 1172 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | return outputs[0]; |
| 1176 | } |
| 1177 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1178 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1179 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1180 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1181 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1182 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1183 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1184 | ssize_t index = mOutputs.indexOfKey(output); |
| 1185 | if (index < 0) { |
| 1186 | ALOGW("startOutput() unknown output %d", output); |
| 1187 | return BAD_VALUE; |
| 1188 | } |
| 1189 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1190 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1191 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1192 | // Routing? |
| 1193 | mOutputRoutes.incRouteActivity(session); |
| 1194 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1195 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1196 | AudioMix *policyMix = NULL; |
| 1197 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1198 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1199 | policyMix = outputDesc->mPolicyMix; |
| 1200 | address = policyMix->mDeviceAddress.string(); |
| 1201 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1202 | newDevice = policyMix->mDeviceType; |
| 1203 | } else { |
| 1204 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1205 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1206 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1207 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1208 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1209 | } else { |
| 1210 | newDevice = AUDIO_DEVICE_NONE; |
| 1211 | } |
| 1212 | |
| 1213 | uint32_t delayMs = 0; |
| 1214 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1215 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1216 | |
| 1217 | if (status != NO_ERROR) { |
| 1218 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1219 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1220 | } |
| 1221 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1222 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1223 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1224 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1225 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1226 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1227 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1228 | "remote-submix"); |
| 1229 | } |
| 1230 | |
| 1231 | if (delayMs != 0) { |
| 1232 | usleep(delayMs * 1000); |
| 1233 | } |
| 1234 | |
| 1235 | return status; |
| 1236 | } |
| 1237 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1238 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1239 | audio_stream_type_t stream, |
| 1240 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1241 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1242 | uint32_t *delayMs) |
| 1243 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1244 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1245 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1246 | |
| 1247 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1248 | if (stream == AUDIO_STREAM_TTS) { |
| 1249 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1250 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1251 | return INVALID_OPERATION; |
| 1252 | } else { |
| 1253 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1254 | } |
| 1255 | } else { |
| 1256 | // some playback other than beacon starts |
| 1257 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1258 | } |
| 1259 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1260 | // 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] | 1261 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1262 | bool force = !outputDesc->isActive() && |
| 1263 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1264 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1265 | // increment usage count for this stream on the requested output: |
| 1266 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1267 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1268 | outputDesc->changeRefCount(stream, 1); |
| 1269 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1270 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1271 | selectOutputForMusicEffects(); |
| 1272 | } |
| 1273 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1274 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1275 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1276 | if (device == AUDIO_DEVICE_NONE) { |
| 1277 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1278 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1279 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1280 | routing_strategy strategy = getStrategy(stream); |
| 1281 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1282 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1283 | (beaconMuteLatency > 0); |
| 1284 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1285 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1286 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1287 | if (desc != outputDesc) { |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1288 | // force a device change if any other output is: |
| 1289 | // - managed by the same hw module |
| 1290 | // - has a current device selection that differs from selected device. |
| 1291 | // - supports currently selected device |
| 1292 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1293 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1294 | // change the device currently selected by the other active output. |
| 1295 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1296 | desc->device() != device && |
| 1297 | desc->supportedDevices() & device && |
| 1298 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1299 | force = true; |
| 1300 | } |
| 1301 | // 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] | 1302 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1303 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1304 | uint32_t latency = desc->latency(); |
| 1305 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1306 | waitMs = latency; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1310 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1311 | |
| 1312 | // handle special case for sonification while in call |
| 1313 | if (isInCall()) { |
| 1314 | handleIncallSonification(stream, true, false); |
| 1315 | } |
| 1316 | |
| 1317 | // apply volume rules for current stream and device if necessary |
| 1318 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1319 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1320 | outputDesc, |
| 1321 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1322 | |
| 1323 | // update the outputs if starting an output with a stream that can affect notification |
| 1324 | // routing |
| 1325 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1326 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1327 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1328 | if (strategy == STRATEGY_SONIFICATION) { |
| 1329 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1330 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1331 | |
| 1332 | if (waitMs > muteWaitMs) { |
| 1333 | *delayMs = waitMs - muteWaitMs; |
| 1334 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1335 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1336 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1337 | return NO_ERROR; |
| 1338 | } |
| 1339 | |
| 1340 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1341 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1342 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1343 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1344 | { |
| 1345 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1346 | ssize_t index = mOutputs.indexOfKey(output); |
| 1347 | if (index < 0) { |
| 1348 | ALOGW("stopOutput() unknown output %d", output); |
| 1349 | return BAD_VALUE; |
| 1350 | } |
| 1351 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1352 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1353 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1354 | if (outputDesc->mRefCount[stream] == 1) { |
| 1355 | // Automatically disable the remote submix input when output is stopped on a |
| 1356 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1357 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1358 | outputDesc->mPolicyMix != NULL && |
| 1359 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1360 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1361 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1362 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1363 | "remote-submix"); |
| 1364 | } |
| 1365 | } |
| 1366 | |
| 1367 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1368 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1369 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1370 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1371 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1372 | |
| 1373 | if (forceDeviceUpdate) { |
| 1374 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1375 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1378 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1381 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1382 | audio_stream_type_t stream, |
| 1383 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1384 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1385 | // always handle stream stop, check which stream type is stopping |
| 1386 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1387 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1388 | // handle special case for sonification while in call |
| 1389 | if (isInCall()) { |
| 1390 | handleIncallSonification(stream, false, false); |
| 1391 | } |
| 1392 | |
| 1393 | if (outputDesc->mRefCount[stream] > 0) { |
| 1394 | // decrement usage count of this stream on the output |
| 1395 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1396 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1397 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1398 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1399 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1400 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1401 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1402 | // the track stop() command is received and at that time the audio track buffer can |
| 1403 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1404 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1405 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1406 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1407 | |
| 1408 | // force restoring the device selection on other active outputs if it differs from the |
| 1409 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1410 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1411 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1412 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1413 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1414 | desc->isActive() && |
| 1415 | outputDesc->sharesHwModuleWith(desc) && |
| 1416 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1417 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1418 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1419 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1420 | newDevice2, |
| 1421 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1422 | delayMs); |
| 1423 | // re-apply device specific volume if not done by setOutputDevice() |
| 1424 | if (!force) { |
| 1425 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1426 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1427 | } |
| 1428 | } |
| 1429 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1430 | handleNotificationRoutingForStream(stream); |
| 1431 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1432 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1433 | selectOutputForMusicEffects(); |
| 1434 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1435 | return NO_ERROR; |
| 1436 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1437 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1438 | return INVALID_OPERATION; |
| 1439 | } |
| 1440 | } |
| 1441 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1442 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1443 | audio_stream_type_t stream __unused, |
| 1444 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1445 | { |
| 1446 | ALOGV("releaseOutput() %d", output); |
| 1447 | ssize_t index = mOutputs.indexOfKey(output); |
| 1448 | if (index < 0) { |
| 1449 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | #ifdef AUDIO_POLICY_TEST |
| 1454 | int testIndex = testOutputIndex(output); |
| 1455 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1456 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1457 | if (outputDesc->isActive()) { |
| 1458 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1459 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1460 | mTestOutputs[testIndex] = 0; |
| 1461 | } |
| 1462 | return; |
| 1463 | } |
| 1464 | #endif //AUDIO_POLICY_TEST |
| 1465 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1466 | // Routing |
| 1467 | mOutputRoutes.removeRoute(session); |
| 1468 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1469 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1470 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1471 | if (desc->mDirectOpenCount <= 0) { |
| 1472 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1473 | desc->mDirectOpenCount, output); |
| 1474 | return; |
| 1475 | } |
| 1476 | if (--desc->mDirectOpenCount == 0) { |
| 1477 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1478 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1484 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1485 | audio_io_handle_t *input, |
| 1486 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1487 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1488 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1489 | audio_input_flags_t flags, |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1490 | audio_port_handle_t *selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1491 | input_type_t *inputType, |
| 1492 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1493 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1494 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1495 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1496 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1497 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1498 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1499 | *inputType = API_INPUT_INVALID; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1500 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1501 | audio_devices_t device; |
| 1502 | // handle legacy remote submix case where the address was not always specified |
| 1503 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1504 | audio_source_t inputSource = attr->source; |
| 1505 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1506 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1507 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1508 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1509 | inputSource = AUDIO_SOURCE_MIC; |
| 1510 | } |
| 1511 | halInputSource = inputSource; |
| 1512 | |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1513 | // TODO: check for existing client for this port ID |
| 1514 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1515 | *portId = AudioPort::getNextUniqueId(); |
| 1516 | } |
| 1517 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1518 | // Explicit routing? |
| 1519 | sp<DeviceDescriptor> deviceDesc; |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1520 | if (*selectedDeviceId != AUDIO_PORT_HANDLE_NONE) { |
| 1521 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1522 | if (mAvailableInputDevices[i]->getId() == *selectedDeviceId) { |
| 1523 | deviceDesc = mAvailableInputDevices[i]; |
| 1524 | break; |
| 1525 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1526 | } |
| 1527 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1528 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1529 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1530 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1531 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1532 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1533 | if (ret != NO_ERROR) { |
| 1534 | return ret; |
| 1535 | } |
| 1536 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1537 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1538 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1539 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1540 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1541 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1542 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1543 | return BAD_VALUE; |
| 1544 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1545 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1546 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1547 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1548 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1549 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1550 | // know about it and is therefore considered "legacy" |
| 1551 | *inputType = API_INPUT_LEGACY; |
| 1552 | } else { |
| 1553 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1554 | // an external policy |
| 1555 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1556 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1557 | } else if (audio_is_remote_submix_device(device)) { |
| 1558 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1559 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1560 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1561 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1562 | } else { |
| 1563 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1564 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1565 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1566 | } |
| 1567 | |
| 1568 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1569 | config->sample_rate, config->format, config->channel_mask, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1570 | policyMix); |
| 1571 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1572 | mInputRoutes.removeRoute(session); |
| 1573 | return INVALID_OPERATION; |
| 1574 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1575 | |
Eric Laurent | 2ac7694 | 2017-06-22 17:17:09 -0700 | [diff] [blame] | 1576 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
| 1577 | *selectedDeviceId = inputDevices.size() > 0 ? inputDevices.itemAt(0)->getId() |
| 1578 | : AUDIO_PORT_HANDLE_NONE; |
| 1579 | |
| 1580 | ALOGV("getInputForAttr() returns input %d type %d selectedDeviceId %d", |
| 1581 | *input, *inputType, *selectedDeviceId); |
| 1582 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1583 | return NO_ERROR; |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1588 | String8 address, |
| 1589 | audio_session_t session, |
| 1590 | uid_t uid, |
| 1591 | audio_source_t inputSource, |
| 1592 | uint32_t samplingRate, |
| 1593 | audio_format_t format, |
| 1594 | audio_channel_mask_t channelMask, |
| 1595 | audio_input_flags_t flags, |
| 1596 | AudioMix *policyMix) |
| 1597 | { |
| 1598 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1599 | audio_source_t halInputSource = inputSource; |
| 1600 | bool isSoundTrigger = false; |
| 1601 | |
| 1602 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1603 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1604 | if (index >= 0) { |
| 1605 | input = mSoundTriggerSessions.valueFor(session); |
| 1606 | isSoundTrigger = true; |
| 1607 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1608 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1609 | } else { |
| 1610 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1611 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame] | 1612 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 1613 | getPhoneState() == AUDIO_MODE_IN_COMMUNICATION && |
| 1614 | audio_is_linear_pcm(format)) { |
| 1615 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1618 | // find a compatible input profile (not necessarily identical in parameters) |
| 1619 | sp<IOProfile> profile; |
| 1620 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1621 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1622 | audio_format_t profileFormat = format; |
| 1623 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1624 | audio_input_flags_t profileFlags = flags; |
| 1625 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1626 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1627 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1628 | profileFlags); |
| 1629 | if (profile != 0) { |
| 1630 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1631 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1632 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1633 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1634 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1635 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1636 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1637 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1638 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1639 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1640 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1641 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1642 | // Pick input sampling rate if not specified by client |
| 1643 | if (samplingRate == 0) { |
| 1644 | samplingRate = profileSamplingRate; |
| 1645 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1646 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1647 | if (profile->getModuleHandle() == 0) { |
| 1648 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1649 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1652 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1653 | inputSource, |
| 1654 | format, |
| 1655 | samplingRate, |
| 1656 | channelMask, |
| 1657 | flags, |
| 1658 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1659 | isSoundTrigger, |
| 1660 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1661 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1662 | // FIXME: disable concurrent capture until UI is ready |
| 1663 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1664 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1665 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1666 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1667 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1668 | // reuse input if: |
| 1669 | // - it shares the same profile |
| 1670 | // AND |
| 1671 | // - it is not a reroute submix input |
| 1672 | // AND |
| 1673 | // - it is: not used for sound trigger |
| 1674 | // OR |
| 1675 | // used for sound trigger and all clients use the same session ID |
| 1676 | // |
| 1677 | if ((profile == desc->mProfile) && |
| 1678 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1679 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1680 | |
| 1681 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1682 | if (as != 0) { |
| 1683 | // do not allow unmatching properties on same session |
| 1684 | if (as->matches(audioSession)) { |
| 1685 | as->changeOpenCount(1); |
| 1686 | } else { |
| 1687 | ALOGW("getInputForDevice() record with different attributes" |
| 1688 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1689 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1690 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1691 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1692 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1693 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1694 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1695 | // Reuse the already opened input stream on this profile if: |
| 1696 | // - the new capture source is background OR |
| 1697 | // - the path requested configurations match OR |
| 1698 | // - the new source priority is less than the highest source priority on this input |
| 1699 | // If the input stream cannot be reused, close it before opening a new stream |
| 1700 | // on the same profile for the new client so that the requested path configuration |
| 1701 | // can be selected. |
| 1702 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1703 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1704 | desc->mChannelMask != channelMask || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1705 | !audio_formats_match(desc->mFormat, format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1706 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1707 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1708 | reusedInputDesc = desc; |
| 1709 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1710 | } else { |
| 1711 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1712 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1713 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1714 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1715 | } |
| 1716 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1717 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1718 | if (reusedInputDesc != 0) { |
| 1719 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1720 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1721 | audio_session_t currentSession = sessions.keyAt(j); |
| 1722 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1723 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1724 | } |
| 1725 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1726 | #endif |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1727 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1728 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1729 | config.sample_rate = profileSamplingRate; |
| 1730 | config.channel_mask = profileChannelMask; |
| 1731 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1732 | |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1733 | if (address == "") { |
| 1734 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
| 1735 | // the inputs vector must be of size 1, but we don't want to crash here |
| 1736 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1737 | } |
| 1738 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1739 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1740 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1741 | &config, |
| 1742 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1743 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1744 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1745 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1746 | |
| 1747 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1748 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1749 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1750 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1751 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1752 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1753 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1754 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1755 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1756 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1757 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1758 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1761 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1762 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1763 | inputDesc->mFormat = profileFormat; |
| 1764 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1765 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1766 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1767 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1768 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1769 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1770 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1771 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1772 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1773 | } |
| 1774 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1775 | //static |
| 1776 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1777 | { |
| 1778 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1779 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1780 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1781 | } |
| 1782 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1783 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1784 | const sp<AudioSession>& audioSession) |
| 1785 | { |
| 1786 | // Do not allow capture if an active voice call is using a software patch and |
| 1787 | // the call TX source device is on the same HW module. |
| 1788 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1789 | // call TX device but this information is not in the audio patch |
| 1790 | if (mCallTxPatch != 0 && |
| 1791 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1792 | return false; |
| 1793 | } |
| 1794 | |
| 1795 | // starting concurrent capture is enabled if: |
| 1796 | // 1) capturing for re-routing |
| 1797 | // 2) capturing for HOTWORD source |
| 1798 | // 3) capturing for FM TUNER source |
| 1799 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1800 | |
| 1801 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1802 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1803 | return true; |
| 1804 | } |
| 1805 | |
| 1806 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1807 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1808 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1809 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1810 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1811 | return false; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | return true; |
| 1816 | } |
| 1817 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame^] | 1818 | // FIXME: remove when concurrent capture is ready. This is a hack to work around bug b/63083537. |
| 1819 | bool AudioPolicyManager::soundTriggerSupportsConcurrentCapture() { |
| 1820 | if (!mHasComputedSoundTriggerSupportsConcurrentCapture) { |
| 1821 | bool soundTriggerSupportsConcurrentCapture = false; |
| 1822 | unsigned int numModules = 0; |
| 1823 | struct sound_trigger_module_descriptor* nModules = NULL; |
| 1824 | |
| 1825 | status_t status = SoundTrigger::listModules(nModules, &numModules); |
| 1826 | if (status == NO_ERROR && numModules != 0) { |
| 1827 | nModules = (struct sound_trigger_module_descriptor*) calloc( |
| 1828 | numModules, sizeof(struct sound_trigger_module_descriptor)); |
| 1829 | if (nModules == NULL) { |
| 1830 | // We failed to malloc the buffer, so just say no for now, and hope that we have more |
| 1831 | // ram the next time this function is called. |
| 1832 | ALOGE("Failed to allocate buffer for module descriptors"); |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
| 1836 | status = SoundTrigger::listModules(nModules, &numModules); |
| 1837 | if (status == NO_ERROR) { |
| 1838 | soundTriggerSupportsConcurrentCapture = true; |
| 1839 | for (size_t i = 0; i < numModules; ++i) { |
| 1840 | soundTriggerSupportsConcurrentCapture &= |
| 1841 | nModules[i].properties.concurrent_capture; |
| 1842 | } |
| 1843 | } |
| 1844 | free(nModules); |
| 1845 | } |
| 1846 | mSoundTriggerSupportsConcurrentCapture = soundTriggerSupportsConcurrentCapture; |
| 1847 | mHasComputedSoundTriggerSupportsConcurrentCapture = true; |
| 1848 | } |
| 1849 | return mSoundTriggerSupportsConcurrentCapture; |
| 1850 | } |
| 1851 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1852 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1853 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1854 | audio_session_t session, |
| 1855 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1856 | { |
| 1857 | ALOGV("startInput() input %d", input); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1858 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1859 | ssize_t index = mInputs.indexOfKey(input); |
| 1860 | if (index < 0) { |
| 1861 | ALOGW("startInput() unknown input %d", input); |
| 1862 | return BAD_VALUE; |
| 1863 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1864 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1865 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1866 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1867 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1868 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1869 | return BAD_VALUE; |
| 1870 | } |
| 1871 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1872 | // FIXME: disable concurrent capture until UI is ready |
| 1873 | #if 0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1874 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1875 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1876 | return INVALID_OPERATION; |
| 1877 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1878 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1879 | if (isInCall()) { |
| 1880 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1881 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1882 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1883 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1884 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1885 | #else |
| 1886 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1887 | if (mCallTxPatch != 0 && |
| 1888 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1889 | ALOGW("startInput(%d) failed: call in progress", input); |
| 1890 | return INVALID_OPERATION; |
| 1891 | } |
| 1892 | |
| 1893 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1894 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1895 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 1896 | |
| 1897 | if (is_virtual_input_device(activeDesc->mDevice)) { |
| 1898 | continue; |
| 1899 | } |
| 1900 | |
| 1901 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1902 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 1903 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1904 | if (activeDesc->hasPreemptedSession(session)) { |
| 1905 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 1906 | "other input %d already started for HOTWORD", |
| 1907 | input, activeDesc->mIoHandle); |
| 1908 | return INVALID_OPERATION; |
| 1909 | } |
| 1910 | } else { |
| 1911 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 1912 | input, activeDesc->mIoHandle); |
| 1913 | return INVALID_OPERATION; |
| 1914 | } |
| 1915 | } else { |
| 1916 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 1917 | ALOGW("startInput(%d) failed: other input %d already started", |
| 1918 | input, activeDesc->mIoHandle); |
| 1919 | return INVALID_OPERATION; |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame^] | 1924 | // We only need to check if the sound trigger session supports concurrent capture if the |
| 1925 | // input is also a sound trigger input. Otherwise, we should preempt any hotword stream |
| 1926 | // that's running. |
| 1927 | const bool allowConcurrentWithSoundTrigger = |
| 1928 | inputDesc->isSoundTrigger() ? soundTriggerSupportsConcurrentCapture() : false; |
| 1929 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1930 | // if capture is allowed, preempt currently active HOTWORD captures |
| 1931 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1932 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 1933 | |
| 1934 | if (is_virtual_input_device(activeDesc->mDevice)) { |
| 1935 | continue; |
| 1936 | } |
| 1937 | |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame^] | 1938 | if (allowConcurrentWithSoundTrigger && activeDesc->isSoundTrigger()) { |
| 1939 | continue; |
| 1940 | } |
| 1941 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1942 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1943 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1944 | AudioSessionCollection activeSessions = |
| 1945 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 1946 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1947 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; |
| 1948 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
| 1949 | sessions.add(activeSession); |
| 1950 | inputDesc->setPreemptedSessions(sessions); |
| 1951 | stopInput(activeHandle, activeSession); |
| 1952 | releaseInput(activeHandle, activeSession); |
| 1953 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 1954 | input, activeDesc->mIoHandle); |
| 1955 | } |
| 1956 | } |
| 1957 | } |
| 1958 | #endif |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1959 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1960 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1961 | // are considered for device selection |
| 1962 | audioSession->changeActiveCount(1); |
| 1963 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1964 | // Routing? |
| 1965 | mInputRoutes.incRouteActivity(session); |
| 1966 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1967 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1968 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 1969 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1970 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1971 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1972 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1973 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
| 1974 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1975 | if ((inputDesc->mPolicyMix != NULL) |
| 1976 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1977 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1978 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1979 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1980 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1981 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1982 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 1983 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1984 | SoundTrigger::setCaptureState(true); |
| 1985 | } |
| 1986 | |
| 1987 | // automatically enable the remote submix output when input is started if not |
| 1988 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1989 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1990 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1991 | String8 address = String8(""); |
| 1992 | if (inputDesc->mPolicyMix == NULL) { |
| 1993 | address = String8("0"); |
| 1994 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1995 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1996 | } |
| 1997 | if (address != "") { |
| 1998 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1999 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2000 | address, "remote-submix"); |
| 2001 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2002 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 2003 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2006 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2007 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2008 | return NO_ERROR; |
| 2009 | } |
| 2010 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2011 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 2012 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2013 | { |
| 2014 | ALOGV("stopInput() input %d", input); |
| 2015 | ssize_t index = mInputs.indexOfKey(input); |
| 2016 | if (index < 0) { |
| 2017 | ALOGW("stopInput() unknown input %d", input); |
| 2018 | return BAD_VALUE; |
| 2019 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2020 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2021 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2022 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2023 | if (index < 0) { |
| 2024 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 2025 | return BAD_VALUE; |
| 2026 | } |
| 2027 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2028 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2029 | ALOGW("stopInput() input %d already stopped", input); |
| 2030 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2033 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2034 | |
| 2035 | // Routing? |
| 2036 | mInputRoutes.decRouteActivity(session); |
| 2037 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2038 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2039 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2040 | if (inputDesc->isActive()) { |
| 2041 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 2042 | } else { |
| 2043 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 2044 | if ((inputDesc->mPolicyMix != NULL) |
| 2045 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 2046 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 2047 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2048 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 2049 | |
| 2050 | // automatically disable the remote submix output when input is stopped if not |
| 2051 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 2052 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 2053 | String8 address = String8(""); |
| 2054 | if (inputDesc->mPolicyMix == NULL) { |
| 2055 | address = String8("0"); |
| 2056 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 2057 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2058 | } |
| 2059 | if (address != "") { |
| 2060 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2061 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2062 | address, "remote-submix"); |
| 2063 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2064 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2065 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2066 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2067 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2068 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2069 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2070 | // primary HW module |
| 2071 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2072 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2073 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2074 | SoundTrigger::setCaptureState(false); |
| 2075 | } |
| 2076 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2077 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2078 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2079 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2080 | } |
| 2081 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2082 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 2083 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2084 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2085 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2086 | ALOGV("releaseInput() %d", input); |
| 2087 | ssize_t index = mInputs.indexOfKey(input); |
| 2088 | if (index < 0) { |
| 2089 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 2090 | return; |
| 2091 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2092 | |
| 2093 | // Routing |
| 2094 | mInputRoutes.removeRoute(session); |
| 2095 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2096 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 2097 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2098 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2099 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2100 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2101 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2102 | return; |
| 2103 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2104 | |
| 2105 | if (audioSession->openCount() == 0) { |
| 2106 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2107 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2108 | return; |
| 2109 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2110 | |
| 2111 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2112 | inputDesc->removeAudioSession(session); |
| 2113 | } |
| 2114 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2115 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2116 | ALOGV("releaseInput() exit > 0"); |
| 2117 | return; |
| 2118 | } |
| 2119 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2120 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2121 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2122 | ALOGV("releaseInput() exit"); |
| 2123 | } |
| 2124 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2125 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2126 | bool patchRemoved = false; |
| 2127 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2128 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2129 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2130 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2131 | if (patch_index >= 0) { |
| 2132 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2133 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2134 | mAudioPatches.removeItemsAt(patch_index); |
| 2135 | patchRemoved = true; |
| 2136 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2137 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 2138 | } |
| 2139 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2140 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2141 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2142 | |
| 2143 | if (patchRemoved) { |
| 2144 | mpClientInterface->onAudioPatchListUpdate(); |
| 2145 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2146 | } |
| 2147 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2148 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2149 | int indexMin, |
| 2150 | int indexMax) |
| 2151 | { |
| 2152 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2153 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2154 | |
| 2155 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2156 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2157 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2158 | continue; |
| 2159 | } |
| 2160 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2161 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2164 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2165 | int index, |
| 2166 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2167 | { |
| 2168 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2169 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 2170 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2171 | return BAD_VALUE; |
| 2172 | } |
| 2173 | if (!audio_is_output_device(device)) { |
| 2174 | return BAD_VALUE; |
| 2175 | } |
| 2176 | |
| 2177 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2178 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2179 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2180 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2181 | stream, device, index); |
| 2182 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2183 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2184 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2185 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2186 | continue; |
| 2187 | } |
| 2188 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2189 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2190 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2191 | // update volume on all outputs and streams matching the following: |
| 2192 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2193 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2194 | // the requested device |
| 2195 | // - For non default requested device, currently selected device on the output is either the |
| 2196 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2197 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2198 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2199 | status_t status = NO_ERROR; |
| 2200 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2201 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2202 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2203 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2204 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2205 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2206 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2207 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2208 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2209 | continue; |
| 2210 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2211 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2212 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/); |
| 2213 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2214 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2215 | continue; |
| 2216 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2217 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2218 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2219 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2220 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2221 | } else { |
| 2222 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
| 2223 | stream, Volume::getDeviceForVolume(curStreamDevice)); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2224 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2225 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2226 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2227 | //FIXME: workaround for truncated touch sounds |
| 2228 | // delayed volume change for system stream to be removed when the problem is |
| 2229 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2230 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2231 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2232 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2233 | if (volStatus != NO_ERROR) { |
| 2234 | status = volStatus; |
| 2235 | } |
| 2236 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2237 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2238 | } |
| 2239 | return status; |
| 2240 | } |
| 2241 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2242 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2243 | int *index, |
| 2244 | audio_devices_t device) |
| 2245 | { |
| 2246 | if (index == NULL) { |
| 2247 | return BAD_VALUE; |
| 2248 | } |
| 2249 | if (!audio_is_output_device(device)) { |
| 2250 | return BAD_VALUE; |
| 2251 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2252 | // 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] | 2253 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2254 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2255 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2256 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2257 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2258 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2259 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2260 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2261 | return NO_ERROR; |
| 2262 | } |
| 2263 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2264 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2265 | { |
| 2266 | // select one output among several suitable for global effects. |
| 2267 | // The priority is as follows: |
| 2268 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2269 | // AudioFlinger will invalidate the track and the offloaded output |
| 2270 | // will be closed causing the effect to be moved to a PCM output. |
| 2271 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2272 | // 3: The primary output |
| 2273 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2274 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2275 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2276 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2277 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2278 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2279 | if (outputs.size() == 0) { |
| 2280 | return AUDIO_IO_HANDLE_NONE; |
| 2281 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2282 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2283 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2284 | bool activeOnly = true; |
| 2285 | |
| 2286 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2287 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2288 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2289 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2290 | |
| 2291 | for (size_t i = 0; i < outputs.size(); i++) { |
| 2292 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
| 2293 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2294 | continue; |
| 2295 | } |
| 2296 | ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x", |
| 2297 | activeOnly, i, desc->mFlags); |
| 2298 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 2299 | outputOffloaded = outputs[i]; |
| 2300 | } |
| 2301 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 2302 | outputDeepBuffer = outputs[i]; |
| 2303 | } |
| 2304 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
| 2305 | outputPrimary = outputs[i]; |
| 2306 | } |
| 2307 | } |
| 2308 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2309 | output = outputOffloaded; |
| 2310 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2311 | output = outputDeepBuffer; |
| 2312 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2313 | output = outputPrimary; |
| 2314 | } else { |
| 2315 | output = outputs[0]; |
| 2316 | } |
| 2317 | activeOnly = false; |
| 2318 | } |
| 2319 | |
| 2320 | if (output != mMusicEffectOutput) { |
| 2321 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2322 | mMusicEffectOutput = output; |
| 2323 | } |
| 2324 | |
| 2325 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2326 | return output; |
| 2327 | } |
| 2328 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2329 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2330 | { |
| 2331 | return selectOutputForMusicEffects(); |
| 2332 | } |
| 2333 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2334 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2335 | audio_io_handle_t io, |
| 2336 | uint32_t strategy, |
| 2337 | int session, |
| 2338 | int id) |
| 2339 | { |
| 2340 | ssize_t index = mOutputs.indexOfKey(io); |
| 2341 | if (index < 0) { |
| 2342 | index = mInputs.indexOfKey(io); |
| 2343 | if (index < 0) { |
| 2344 | ALOGW("registerEffect() unknown io %d", io); |
| 2345 | return INVALID_OPERATION; |
| 2346 | } |
| 2347 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2348 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2349 | } |
| 2350 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2351 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2352 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2353 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2354 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2355 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2356 | continue; |
| 2357 | } |
| 2358 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2359 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2360 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2361 | } |
| 2362 | |
| 2363 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2364 | { |
| 2365 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2366 | } |
| 2367 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2368 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2369 | { |
| 2370 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2371 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2372 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2373 | return true; |
| 2374 | } |
| 2375 | } |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2379 | // Register a list of custom mixes with their attributes and format. |
| 2380 | // When a mix is registered, corresponding input and output profiles are |
| 2381 | // added to the remote submix hw module. The profile contains only the |
| 2382 | // parameters (sampling rate, format...) specified by the mix. |
| 2383 | // The corresponding input remote submix device is also connected. |
| 2384 | // |
| 2385 | // When a remote submix device is connected, the address is checked to select the |
| 2386 | // appropriate profile and the corresponding input or output stream is opened. |
| 2387 | // |
| 2388 | // When capture starts, getInputForAttr() will: |
| 2389 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2390 | // - 2 if none found, getDeviceForInputSource() will: |
| 2391 | // - 2.1 look for a mix matching the attributes source |
| 2392 | // - 2.2 if none found, default to device selection by policy rules |
| 2393 | // At this time, the corresponding output remote submix device is also connected |
| 2394 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2395 | // after AudioTracks are invalidated |
| 2396 | // |
| 2397 | // When playback starts, getOutputForAttr() will: |
| 2398 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2399 | // - 2 if none found, look for a mix matching the attributes usage |
| 2400 | // - 3 if none found, default to device and output selection by policy rules. |
| 2401 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2402 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2403 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2404 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2405 | status_t res = NO_ERROR; |
| 2406 | |
| 2407 | sp<HwModule> rSubmixModule; |
| 2408 | // examine each mix's route type |
| 2409 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2410 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2411 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2412 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2413 | break; |
| 2414 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2415 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2416 | // Loop back through "remote submix" |
| 2417 | if (rSubmixModule == 0) { |
| 2418 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2419 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2420 | && mHwModules[j]->mHandle != 0) { |
| 2421 | rSubmixModule = mHwModules[j]; |
| 2422 | break; |
| 2423 | } |
| 2424 | } |
| 2425 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2426 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2427 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2428 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2429 | if (rSubmixModule == 0) { |
| 2430 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2431 | res = INVALID_OPERATION; |
| 2432 | break; |
| 2433 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2434 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2435 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2436 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2437 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2438 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2439 | res = INVALID_OPERATION; |
| 2440 | break; |
| 2441 | } |
| 2442 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2443 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2444 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2445 | // stereo and let audio flinger do the channel conversion if needed. |
| 2446 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2447 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2448 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2449 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2450 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2451 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2452 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2453 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2454 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2455 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2456 | address.string(), "remote-submix"); |
| 2457 | } else { |
| 2458 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2459 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2460 | address.string(), "remote-submix"); |
| 2461 | } |
| 2462 | } 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] | 2463 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2464 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2465 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2466 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2467 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2468 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2469 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2470 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2471 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2472 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2473 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2474 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2475 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2476 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2477 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2478 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2479 | } else { |
| 2480 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2481 | } |
| 2482 | break; |
| 2483 | } |
| 2484 | } |
| 2485 | |
| 2486 | if (res != NO_ERROR) { |
| 2487 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2488 | i, device, address.string()); |
| 2489 | res = INVALID_OPERATION; |
| 2490 | break; |
| 2491 | } else if (!foundOutput) { |
| 2492 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2493 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2494 | res = INVALID_OPERATION; |
| 2495 | break; |
| 2496 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2497 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2498 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2499 | if (res != NO_ERROR) { |
| 2500 | unregisterPolicyMixes(mixes); |
| 2501 | } |
| 2502 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2506 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2507 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2508 | status_t res = NO_ERROR; |
| 2509 | sp<HwModule> rSubmixModule; |
| 2510 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2511 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2512 | 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] | 2513 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2514 | if (rSubmixModule == 0) { |
| 2515 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2516 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2517 | && mHwModules[j]->mHandle != 0) { |
| 2518 | rSubmixModule = mHwModules[j]; |
| 2519 | break; |
| 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | if (rSubmixModule == 0) { |
| 2524 | res = INVALID_OPERATION; |
| 2525 | continue; |
| 2526 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2527 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2528 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2529 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2530 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2531 | res = INVALID_OPERATION; |
| 2532 | continue; |
| 2533 | } |
| 2534 | |
| 2535 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2536 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2537 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2538 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2539 | address.string(), "remote-submix"); |
| 2540 | } |
| 2541 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2542 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2543 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2544 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2545 | address.string(), "remote-submix"); |
| 2546 | } |
| 2547 | rSubmixModule->removeOutputProfile(address); |
| 2548 | rSubmixModule->removeInputProfile(address); |
| 2549 | |
| 2550 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2551 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2552 | res = INVALID_OPERATION; |
| 2553 | continue; |
| 2554 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2555 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2556 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2557 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2558 | } |
| 2559 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2560 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2561 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2562 | { |
| 2563 | const size_t SIZE = 256; |
| 2564 | char buffer[SIZE]; |
| 2565 | String8 result; |
| 2566 | |
| 2567 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2568 | result.append(buffer); |
| 2569 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2570 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2571 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2572 | result.append(buffer); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2573 | std::string stateLiteral; |
| 2574 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
| 2575 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2576 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2577 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2578 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2579 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2580 | 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] | 2581 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2582 | 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] | 2583 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2584 | 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] | 2585 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2586 | 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] | 2587 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2588 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2589 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2590 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2591 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2592 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2593 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2594 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2595 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2596 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2597 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2598 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2599 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2600 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2601 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2602 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2603 | mHwModules.dump(fd); |
| 2604 | mOutputs.dump(fd); |
| 2605 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2606 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2607 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2608 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2609 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2610 | |
| 2611 | return NO_ERROR; |
| 2612 | } |
| 2613 | |
| 2614 | // This function checks for the parameters which can be offloaded. |
| 2615 | // This can be enhanced depending on the capability of the DSP and policy |
| 2616 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2617 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2618 | { |
| 2619 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2620 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2621 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2622 | offloadInfo.format, |
| 2623 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2624 | offloadInfo.has_video); |
| 2625 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2626 | if (mMasterMono) { |
| 2627 | return false; // no offloading if mono is set. |
| 2628 | } |
| 2629 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2630 | // Check if offload has been disabled |
| 2631 | char propValue[PROPERTY_VALUE_MAX]; |
| 2632 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2633 | if (atoi(propValue) != 0) { |
| 2634 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2635 | return false; |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | // Check if stream type is music, then only allow offload as of now. |
| 2640 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2641 | { |
| 2642 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2643 | return false; |
| 2644 | } |
| 2645 | |
| 2646 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2647 | const bool allowOffloadWithVideo = |
| 2648 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2649 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2650 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2651 | return false; |
| 2652 | } |
| 2653 | |
| 2654 | //If duration is less than minimum value defined in property, return false |
| 2655 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2656 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2657 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2658 | return false; |
| 2659 | } |
| 2660 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2661 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2662 | return false; |
| 2663 | } |
| 2664 | |
| 2665 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2666 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2667 | // detects there is an active non offloadable effect. |
| 2668 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2669 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2670 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2671 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2672 | return false; |
| 2673 | } |
| 2674 | |
| 2675 | // See if there is a profile to support this. |
| 2676 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2677 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2678 | offloadInfo.sample_rate, |
| 2679 | offloadInfo.format, |
| 2680 | offloadInfo.channel_mask, |
| 2681 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2682 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2683 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2684 | } |
| 2685 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2686 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2687 | audio_port_type_t type, |
| 2688 | unsigned int *num_ports, |
| 2689 | struct audio_port *ports, |
| 2690 | unsigned int *generation) |
| 2691 | { |
| 2692 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2693 | generation == NULL) { |
| 2694 | return BAD_VALUE; |
| 2695 | } |
| 2696 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2697 | if (ports == NULL) { |
| 2698 | *num_ports = 0; |
| 2699 | } |
| 2700 | |
| 2701 | size_t portsWritten = 0; |
| 2702 | size_t portsMax = *num_ports; |
| 2703 | *num_ports = 0; |
| 2704 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2705 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2706 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2707 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2708 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2709 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2710 | continue; |
| 2711 | } |
| 2712 | if (portsWritten < portsMax) { |
| 2713 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2714 | } |
| 2715 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2716 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2717 | } |
| 2718 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2719 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2720 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2721 | continue; |
| 2722 | } |
| 2723 | if (portsWritten < portsMax) { |
| 2724 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2725 | } |
| 2726 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2727 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2728 | } |
| 2729 | } |
| 2730 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2731 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2732 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2733 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2734 | } |
| 2735 | *num_ports += mInputs.size(); |
| 2736 | } |
| 2737 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2738 | size_t numOutputs = 0; |
| 2739 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2740 | if (!mOutputs[i]->isDuplicated()) { |
| 2741 | numOutputs++; |
| 2742 | if (portsWritten < portsMax) { |
| 2743 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2744 | } |
| 2745 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2746 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2747 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2748 | } |
| 2749 | } |
| 2750 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2751 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2752 | return NO_ERROR; |
| 2753 | } |
| 2754 | |
| 2755 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2756 | { |
| 2757 | return NO_ERROR; |
| 2758 | } |
| 2759 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2760 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2761 | audio_patch_handle_t *handle, |
| 2762 | uid_t uid) |
| 2763 | { |
| 2764 | ALOGV("createAudioPatch()"); |
| 2765 | |
| 2766 | if (handle == NULL || patch == NULL) { |
| 2767 | return BAD_VALUE; |
| 2768 | } |
| 2769 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2770 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2771 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2772 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2773 | return BAD_VALUE; |
| 2774 | } |
| 2775 | // only one source per audio patch supported for now |
| 2776 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2777 | return INVALID_OPERATION; |
| 2778 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2779 | |
| 2780 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2781 | return INVALID_OPERATION; |
| 2782 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2783 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2784 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2785 | return INVALID_OPERATION; |
| 2786 | } |
| 2787 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2788 | |
| 2789 | sp<AudioPatch> patchDesc; |
| 2790 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2791 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2792 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2793 | patch->sources[0].role, |
| 2794 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2795 | #if LOG_NDEBUG == 0 |
| 2796 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2797 | 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] | 2798 | patch->sinks[i].role, |
| 2799 | patch->sinks[i].type); |
| 2800 | } |
| 2801 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2802 | |
| 2803 | if (index >= 0) { |
| 2804 | patchDesc = mAudioPatches.valueAt(index); |
| 2805 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2806 | mUidCached, patchDesc->mUid, uid); |
| 2807 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2808 | return INVALID_OPERATION; |
| 2809 | } |
| 2810 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2811 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2815 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2816 | if (outputDesc == NULL) { |
| 2817 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2818 | return BAD_VALUE; |
| 2819 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2820 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2821 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2822 | if (patchDesc != 0) { |
| 2823 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2824 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2825 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2826 | return BAD_VALUE; |
| 2827 | } |
| 2828 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2829 | DeviceVector devices; |
| 2830 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2831 | // Only support mix to devices connection |
| 2832 | // TODO add support for mix to mix connection |
| 2833 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2834 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2835 | return INVALID_OPERATION; |
| 2836 | } |
| 2837 | sp<DeviceDescriptor> devDesc = |
| 2838 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2839 | if (devDesc == 0) { |
| 2840 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2841 | return BAD_VALUE; |
| 2842 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2843 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2844 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2845 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2846 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2847 | NULL, // updatedSamplingRate |
| 2848 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2849 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2850 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2851 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2852 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2853 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2854 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2855 | return INVALID_OPERATION; |
| 2856 | } |
| 2857 | devices.add(devDesc); |
| 2858 | } |
| 2859 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2860 | return INVALID_OPERATION; |
| 2861 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2862 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2863 | // TODO: reconfigure output format and channels here |
| 2864 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2865 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2866 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2867 | index = mAudioPatches.indexOfKey(*handle); |
| 2868 | if (index >= 0) { |
| 2869 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2870 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2871 | } |
| 2872 | patchDesc = mAudioPatches.valueAt(index); |
| 2873 | patchDesc->mUid = uid; |
| 2874 | ALOGV("createAudioPatch() success"); |
| 2875 | } else { |
| 2876 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2877 | return INVALID_OPERATION; |
| 2878 | } |
| 2879 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2880 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2881 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2882 | // only one sink supported when connecting an input device to a mix |
| 2883 | if (patch->num_sinks > 1) { |
| 2884 | return INVALID_OPERATION; |
| 2885 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2886 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2887 | if (inputDesc == NULL) { |
| 2888 | return BAD_VALUE; |
| 2889 | } |
| 2890 | if (patchDesc != 0) { |
| 2891 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2892 | return BAD_VALUE; |
| 2893 | } |
| 2894 | } |
| 2895 | sp<DeviceDescriptor> devDesc = |
| 2896 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2897 | if (devDesc == 0) { |
| 2898 | return BAD_VALUE; |
| 2899 | } |
| 2900 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2901 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2902 | devDesc->mAddress, |
| 2903 | patch->sinks[0].sample_rate, |
| 2904 | NULL, /*updatedSampleRate*/ |
| 2905 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2906 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2907 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2908 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2909 | // FIXME for the parameter type, |
| 2910 | // and the NONE |
| 2911 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2912 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2913 | return INVALID_OPERATION; |
| 2914 | } |
| 2915 | // TODO: reconfigure output format and channels here |
| 2916 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2917 | devDesc->type(), inputDesc->mIoHandle); |
| 2918 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2919 | index = mAudioPatches.indexOfKey(*handle); |
| 2920 | if (index >= 0) { |
| 2921 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2922 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2923 | } |
| 2924 | patchDesc = mAudioPatches.valueAt(index); |
| 2925 | patchDesc->mUid = uid; |
| 2926 | ALOGV("createAudioPatch() success"); |
| 2927 | } else { |
| 2928 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2929 | return INVALID_OPERATION; |
| 2930 | } |
| 2931 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2932 | // device to device connection |
| 2933 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2934 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2935 | return BAD_VALUE; |
| 2936 | } |
| 2937 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2938 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2939 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2940 | if (srcDeviceDesc == 0) { |
| 2941 | return BAD_VALUE; |
| 2942 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2943 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2944 | //update source and sink with our own data as the data passed in the patch may |
| 2945 | // be incomplete. |
| 2946 | struct audio_patch newPatch = *patch; |
| 2947 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2948 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2949 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2950 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2951 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2952 | return INVALID_OPERATION; |
| 2953 | } |
| 2954 | |
| 2955 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2956 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2957 | if (sinkDeviceDesc == 0) { |
| 2958 | return BAD_VALUE; |
| 2959 | } |
| 2960 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2961 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2962 | // create a software bridge in PatchPanel if: |
| 2963 | // - source and sink devices are on differnt HW modules OR |
| 2964 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2965 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2966 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2967 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2968 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2969 | return INVALID_OPERATION; |
| 2970 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2971 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2972 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2973 | // if the sink device is reachable via an opened output stream, request to go via |
| 2974 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2975 | audio_io_handle_t output = selectOutput(outputs, |
| 2976 | AUDIO_OUTPUT_FLAG_NONE, |
| 2977 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2978 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2979 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2980 | if (outputDesc->isDuplicated()) { |
| 2981 | return INVALID_OPERATION; |
| 2982 | } |
| 2983 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2984 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2985 | newPatch.num_sources = 2; |
| 2986 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2987 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2988 | } |
| 2989 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2990 | |
| 2991 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2992 | if (index >= 0) { |
| 2993 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2994 | } |
| 2995 | |
| 2996 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2997 | &afPatchHandle, |
| 2998 | 0); |
| 2999 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 3000 | status, afPatchHandle); |
| 3001 | if (status == NO_ERROR) { |
| 3002 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3003 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3004 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 3005 | } else { |
| 3006 | patchDesc->mPatch = newPatch; |
| 3007 | } |
| 3008 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 3009 | *handle = patchDesc->mHandle; |
| 3010 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3011 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3012 | } else { |
| 3013 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 3014 | status); |
| 3015 | return INVALID_OPERATION; |
| 3016 | } |
| 3017 | } else { |
| 3018 | return BAD_VALUE; |
| 3019 | } |
| 3020 | } else { |
| 3021 | return BAD_VALUE; |
| 3022 | } |
| 3023 | return NO_ERROR; |
| 3024 | } |
| 3025 | |
| 3026 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 3027 | uid_t uid) |
| 3028 | { |
| 3029 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 3030 | |
| 3031 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 3032 | |
| 3033 | if (index < 0) { |
| 3034 | return BAD_VALUE; |
| 3035 | } |
| 3036 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3037 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 3038 | mUidCached, patchDesc->mUid, uid); |
| 3039 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 3040 | return INVALID_OPERATION; |
| 3041 | } |
| 3042 | |
| 3043 | struct audio_patch *patch = &patchDesc->mPatch; |
| 3044 | patchDesc->mUid = mUidCached; |
| 3045 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3046 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3047 | if (outputDesc == NULL) { |
| 3048 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 3049 | return BAD_VALUE; |
| 3050 | } |
| 3051 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3052 | setOutputDevice(outputDesc, |
| 3053 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3054 | true, |
| 3055 | 0, |
| 3056 | NULL); |
| 3057 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3058 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3059 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3060 | if (inputDesc == NULL) { |
| 3061 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3062 | return BAD_VALUE; |
| 3063 | } |
| 3064 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3065 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3066 | true, |
| 3067 | NULL); |
| 3068 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3069 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3070 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3071 | status, patchDesc->mAfPatchHandle); |
| 3072 | removeAudioPatch(patchDesc->mHandle); |
| 3073 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3074 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3075 | } else { |
| 3076 | return BAD_VALUE; |
| 3077 | } |
| 3078 | } else { |
| 3079 | return BAD_VALUE; |
| 3080 | } |
| 3081 | return NO_ERROR; |
| 3082 | } |
| 3083 | |
| 3084 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3085 | struct audio_patch *patches, |
| 3086 | unsigned int *generation) |
| 3087 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3088 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3089 | return BAD_VALUE; |
| 3090 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3091 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3092 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3093 | } |
| 3094 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3095 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3096 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3097 | ALOGV("setAudioPortConfig()"); |
| 3098 | |
| 3099 | if (config == NULL) { |
| 3100 | return BAD_VALUE; |
| 3101 | } |
| 3102 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3103 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3104 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3105 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3108 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3109 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3110 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3111 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3112 | if (outputDesc == NULL) { |
| 3113 | return BAD_VALUE; |
| 3114 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3115 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3116 | "setAudioPortConfig() called on duplicated output %d", |
| 3117 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3118 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3119 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3120 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3121 | if (inputDesc == NULL) { |
| 3122 | return BAD_VALUE; |
| 3123 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3124 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3125 | } else { |
| 3126 | return BAD_VALUE; |
| 3127 | } |
| 3128 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3129 | sp<DeviceDescriptor> deviceDesc; |
| 3130 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3131 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3132 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3133 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3134 | } else { |
| 3135 | return BAD_VALUE; |
| 3136 | } |
| 3137 | if (deviceDesc == NULL) { |
| 3138 | return BAD_VALUE; |
| 3139 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3140 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3141 | } else { |
| 3142 | return BAD_VALUE; |
| 3143 | } |
| 3144 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3145 | struct audio_port_config backupConfig; |
| 3146 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3147 | if (status == NO_ERROR) { |
| 3148 | struct audio_port_config newConfig; |
| 3149 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3150 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3151 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3152 | if (status != NO_ERROR) { |
| 3153 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3154 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3155 | |
| 3156 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3159 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3160 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3161 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3162 | clearAudioPatches(uid); |
| 3163 | clearSessionRoutes(uid); |
| 3164 | } |
| 3165 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3166 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3167 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3168 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3169 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3170 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3171 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3172 | } |
| 3173 | } |
| 3174 | } |
| 3175 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3176 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3177 | audio_io_handle_t ouptutToSkip) |
| 3178 | { |
| 3179 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3180 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3181 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3182 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3183 | continue; |
| 3184 | } |
| 3185 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3186 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3187 | continue; |
| 3188 | } |
| 3189 | // If the default device for this strategy is on another output mix, |
| 3190 | // invalidate all tracks in this strategy to force re connection. |
| 3191 | // Otherwise select new device on the output mix. |
| 3192 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3193 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3194 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3195 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3196 | } |
| 3197 | } |
| 3198 | } else { |
| 3199 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3200 | setOutputDevice(outputDesc, newDevice, false); |
| 3201 | } |
| 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3206 | { |
| 3207 | // remove output routes associated with this uid |
| 3208 | SortedVector<routing_strategy> affectedStrategies; |
| 3209 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3210 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3211 | if (route->mUid == uid) { |
| 3212 | mOutputRoutes.removeItemsAt(i); |
| 3213 | if (route->mDeviceDescriptor != 0) { |
| 3214 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3215 | } |
| 3216 | } |
| 3217 | } |
| 3218 | // reroute outputs if necessary |
| 3219 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 3220 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 3221 | } |
| 3222 | |
| 3223 | // remove input routes associated with this uid |
| 3224 | SortedVector<audio_source_t> affectedSources; |
| 3225 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3226 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3227 | if (route->mUid == uid) { |
| 3228 | mInputRoutes.removeItemsAt(i); |
| 3229 | if (route->mDeviceDescriptor != 0) { |
| 3230 | affectedSources.add(route->mSource); |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | // reroute inputs if necessary |
| 3235 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3236 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3237 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3238 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3239 | inputsToClose.add(inputDesc->mIoHandle); |
| 3240 | } |
| 3241 | } |
| 3242 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 3243 | closeInput(inputsToClose[i]); |
| 3244 | } |
| 3245 | } |
| 3246 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3247 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3248 | { |
| 3249 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3250 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3251 | if (sourceDesc->mUid == uid) { |
| 3252 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3253 | } |
| 3254 | } |
| 3255 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3256 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3257 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3258 | audio_io_handle_t *ioHandle, |
| 3259 | audio_devices_t *device) |
| 3260 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3261 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3262 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3263 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3264 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3265 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3268 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3269 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3270 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3271 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3272 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3273 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3274 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3275 | return BAD_VALUE; |
| 3276 | } |
| 3277 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3278 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3279 | |
| 3280 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3281 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3282 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3283 | return INVALID_OPERATION; |
| 3284 | } |
| 3285 | |
| 3286 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3287 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3288 | String8(source->ext.device.address)); |
| 3289 | if (srcDeviceDesc == 0) { |
| 3290 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3291 | return BAD_VALUE; |
| 3292 | } |
| 3293 | sp<AudioSourceDescriptor> sourceDesc = |
| 3294 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3295 | |
| 3296 | struct audio_patch dummyPatch; |
| 3297 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3298 | sourceDesc->mPatchDesc = patchDesc; |
| 3299 | |
| 3300 | status_t status = connectAudioSource(sourceDesc); |
| 3301 | if (status == NO_ERROR) { |
| 3302 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3303 | *handle = sourceDesc->getHandle(); |
| 3304 | } |
| 3305 | return status; |
| 3306 | } |
| 3307 | |
| 3308 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3309 | { |
| 3310 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3311 | |
| 3312 | // make sure we only have one patch per source. |
| 3313 | disconnectAudioSource(sourceDesc); |
| 3314 | |
| 3315 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3316 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3317 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3318 | |
| 3319 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3320 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3321 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3322 | |
| 3323 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3324 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3325 | |
| 3326 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3327 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3328 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3329 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3330 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3331 | // create patch between src device and output device |
| 3332 | // create Hwoutput and add to mHwOutputs |
| 3333 | } else { |
| 3334 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3335 | audio_io_handle_t output = |
| 3336 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3337 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3338 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3339 | return INVALID_OPERATION; |
| 3340 | } |
| 3341 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3342 | if (outputDesc->isDuplicated()) { |
| 3343 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3344 | return INVALID_OPERATION; |
| 3345 | } |
| 3346 | // create a special patch with no sink and two sources: |
| 3347 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3348 | // be connected as well as the stream type for volume control |
| 3349 | // - the sink is defined by whatever output device is currently selected for the output |
| 3350 | // though which this patch is routed. |
| 3351 | patch->num_sinks = 0; |
| 3352 | patch->num_sources = 2; |
| 3353 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3354 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3355 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3356 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3357 | &afPatchHandle, |
| 3358 | 0); |
| 3359 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3360 | status, afPatchHandle); |
| 3361 | if (status != NO_ERROR) { |
| 3362 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3363 | __FUNCTION__, status); |
| 3364 | return INVALID_OPERATION; |
| 3365 | } |
| 3366 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3367 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3368 | |
| 3369 | if (status != NO_ERROR) { |
| 3370 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3371 | return status; |
| 3372 | } |
| 3373 | sourceDesc->mSwOutput = outputDesc; |
| 3374 | if (delayMs != 0) { |
| 3375 | usleep(delayMs * 1000); |
| 3376 | } |
| 3377 | } |
| 3378 | |
| 3379 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3380 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3381 | |
| 3382 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3383 | } |
| 3384 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3385 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3386 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3387 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3388 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3389 | if (sourceDesc == 0) { |
| 3390 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3391 | return BAD_VALUE; |
| 3392 | } |
| 3393 | status_t status = disconnectAudioSource(sourceDesc); |
| 3394 | |
| 3395 | mAudioSources.removeItem(handle); |
| 3396 | return status; |
| 3397 | } |
| 3398 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3399 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3400 | { |
| 3401 | if (mMasterMono == mono) { |
| 3402 | return NO_ERROR; |
| 3403 | } |
| 3404 | mMasterMono = mono; |
| 3405 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3406 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3407 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3408 | // |
| 3409 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3410 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3411 | // play back offloaded. |
| 3412 | if (mMasterMono) { |
| 3413 | Vector<audio_io_handle_t> offloaded; |
| 3414 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3415 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3416 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3417 | offloaded.push(desc->mIoHandle); |
| 3418 | } |
| 3419 | } |
| 3420 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3421 | closeOutput(offloaded[i]); |
| 3422 | } |
| 3423 | } |
| 3424 | // update master mono for all remaining outputs |
| 3425 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3426 | updateMono(mOutputs.keyAt(i)); |
| 3427 | } |
| 3428 | return NO_ERROR; |
| 3429 | } |
| 3430 | |
| 3431 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3432 | { |
| 3433 | *mono = mMasterMono; |
| 3434 | return NO_ERROR; |
| 3435 | } |
| 3436 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3437 | float AudioPolicyManager::getStreamVolumeDB( |
| 3438 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3439 | { |
| 3440 | return computeVolume(stream, index, device); |
| 3441 | } |
| 3442 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3443 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3444 | { |
| 3445 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3446 | |
| 3447 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3448 | if (patchDesc == 0) { |
| 3449 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3450 | sourceDesc->mPatchDesc->mHandle); |
| 3451 | return BAD_VALUE; |
| 3452 | } |
| 3453 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3454 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3455 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3456 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3457 | if (swOutputDesc != 0) { |
| 3458 | stopSource(swOutputDesc, stream, false); |
| 3459 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3460 | } else { |
| 3461 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3462 | if (hwOutputDesc != 0) { |
| 3463 | // release patch between src device and output device |
| 3464 | // close Hwoutput and remove from mHwOutputs |
| 3465 | } else { |
| 3466 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3467 | } |
| 3468 | } |
| 3469 | return NO_ERROR; |
| 3470 | } |
| 3471 | |
| 3472 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3473 | audio_io_handle_t output, routing_strategy strategy) |
| 3474 | { |
| 3475 | sp<AudioSourceDescriptor> source; |
| 3476 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3477 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3478 | routing_strategy sourceStrategy = |
| 3479 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3480 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3481 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3482 | source = sourceDesc; |
| 3483 | break; |
| 3484 | } |
| 3485 | } |
| 3486 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3487 | } |
| 3488 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3489 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3490 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3491 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3492 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3493 | { |
| 3494 | return android_atomic_inc(&mAudioPortGeneration); |
| 3495 | } |
| 3496 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3497 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3498 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3499 | static const char *kConfigLocationList[] = |
| 3500 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3501 | static const int kConfigLocationListSize = |
| 3502 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3503 | |
| 3504 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3505 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
| 3506 | status_t ret; |
| 3507 | |
| 3508 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3509 | PolicySerializer serializer; |
| 3510 | snprintf(audioPolicyXmlConfigFile, |
| 3511 | sizeof(audioPolicyXmlConfigFile), |
| 3512 | "%s/%s", |
| 3513 | kConfigLocationList[i], |
| 3514 | AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3515 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3516 | if (ret == NO_ERROR) { |
| 3517 | break; |
| 3518 | } |
| 3519 | } |
| 3520 | return ret; |
| 3521 | } |
| 3522 | #endif |
| 3523 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3524 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3525 | : |
| 3526 | #ifdef AUDIO_POLICY_TEST |
| 3527 | Thread(false), |
| 3528 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3529 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3530 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3531 | mAudioPortGeneration(1), |
| 3532 | mBeaconMuteRefCount(0), |
| 3533 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3534 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3535 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3536 | mMasterMono(false), |
Chris Thornton | 2b86464 | 2017-06-27 21:26:07 -0700 | [diff] [blame^] | 3537 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE), |
| 3538 | mHasComputedSoundTriggerSupportsConcurrentCapture(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3539 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3540 | mUidCached = getuid(); |
| 3541 | mpClientInterface = clientInterface; |
| 3542 | |
| 3543 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3544 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3545 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3546 | bool speakerDrcEnabled = false; |
| 3547 | |
| 3548 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3549 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3550 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3551 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3552 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3553 | if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3554 | #else |
| 3555 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3556 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3557 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3558 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3559 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3560 | #endif |
| 3561 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3562 | config.setDefault(); |
| 3563 | } |
| 3564 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3565 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3566 | |
| 3567 | // 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] | 3568 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3569 | if (!engineInstance) { |
| 3570 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3571 | return; |
| 3572 | } |
| 3573 | // Retrieve the Policy Manager Interface |
| 3574 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3575 | if (mEngine == NULL) { |
| 3576 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3577 | return; |
| 3578 | } |
| 3579 | mEngine->setObserver(this); |
| 3580 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3581 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3582 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3583 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3584 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3585 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3586 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3587 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3588 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3589 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3590 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3591 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3592 | continue; |
| 3593 | } |
| 3594 | // open all output streams needed to access attached devices |
| 3595 | // except for direct output streams that are only opened when they are actually |
| 3596 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3597 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3598 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3599 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3600 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3601 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3602 | if (!outProfile->hasSupportedDevices()) { |
| 3603 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3604 | continue; |
| 3605 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3606 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3607 | mTtsOutputAvailable = true; |
| 3608 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3609 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3610 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3611 | continue; |
| 3612 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3613 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3614 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3615 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3616 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3617 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3618 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3619 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3620 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3621 | if ((profileType & outputDeviceTypes) == 0) { |
| 3622 | continue; |
| 3623 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3624 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3625 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3626 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3627 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3628 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3629 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3630 | |
| 3631 | outputDesc->mDevice = profileType; |
| 3632 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3633 | config.sample_rate = outputDesc->mSamplingRate; |
| 3634 | config.channel_mask = outputDesc->mChannelMask; |
| 3635 | config.format = outputDesc->mFormat; |
| 3636 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3637 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3638 | &output, |
| 3639 | &config, |
| 3640 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3641 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3642 | &outputDesc->mLatency, |
| 3643 | outputDesc->mFlags); |
| 3644 | |
| 3645 | if (status != NO_ERROR) { |
| 3646 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3647 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3648 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3649 | } else { |
| 3650 | outputDesc->mSamplingRate = config.sample_rate; |
| 3651 | outputDesc->mChannelMask = config.channel_mask; |
| 3652 | outputDesc->mFormat = config.format; |
| 3653 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3654 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3655 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3656 | // 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] | 3657 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3658 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3659 | } |
| 3660 | } |
| 3661 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3662 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3663 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3664 | } |
| 3665 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3666 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3667 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3668 | true, |
| 3669 | 0, |
| 3670 | NULL, |
| 3671 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3672 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3673 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3674 | // open input streams needed to access attached devices to validate |
| 3675 | // mAvailableInputDevices list |
| 3676 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3677 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3678 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3679 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3680 | if (!inProfile->hasSupportedDevices()) { |
| 3681 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3682 | continue; |
| 3683 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3684 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3685 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3686 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3687 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3688 | if ((profileType & inputDeviceTypes) == 0) { |
| 3689 | continue; |
| 3690 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3691 | sp<AudioInputDescriptor> inputDesc = |
| 3692 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3693 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3694 | inputDesc->mDevice = profileType; |
| 3695 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3696 | // find the address |
| 3697 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3698 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3699 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3700 | : String8(""); |
| 3701 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3702 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3703 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3704 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3705 | config.sample_rate = inputDesc->mSamplingRate; |
| 3706 | config.channel_mask = inputDesc->mChannelMask; |
| 3707 | config.format = inputDesc->mFormat; |
| 3708 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3709 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3710 | &input, |
| 3711 | &config, |
| 3712 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3713 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3714 | AUDIO_SOURCE_MIC, |
| 3715 | AUDIO_INPUT_FLAG_NONE); |
| 3716 | |
| 3717 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3718 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3719 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3720 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3721 | // 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] | 3722 | if (index >= 0) { |
| 3723 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3724 | if (!devDesc->isAttached()) { |
| 3725 | devDesc->attach(mHwModules[i]); |
| 3726 | devDesc->importAudioPort(inProfile); |
| 3727 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3728 | } |
| 3729 | } |
| 3730 | mpClientInterface->closeInput(input); |
| 3731 | } else { |
| 3732 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3733 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3734 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3735 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | // make sure all attached devices have been allocated a unique ID |
| 3739 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3740 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3741 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3742 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3743 | continue; |
| 3744 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3745 | // The device is now validated and can be appended to the available devices of the engine |
| 3746 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3747 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3748 | i++; |
| 3749 | } |
| 3750 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3751 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3752 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3753 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3754 | continue; |
| 3755 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3756 | // The device is now validated and can be appended to the available devices of the engine |
| 3757 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3758 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3759 | i++; |
| 3760 | } |
| 3761 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3762 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3763 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3764 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3765 | |
| 3766 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3767 | |
| 3768 | updateDevicesAndOutputs(); |
| 3769 | |
| 3770 | #ifdef AUDIO_POLICY_TEST |
| 3771 | if (mPrimaryOutput != 0) { |
| 3772 | AudioParameter outputCmd = AudioParameter(); |
| 3773 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3774 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3775 | |
| 3776 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3777 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3778 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3779 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3780 | mTestLatencyMs = 0; |
| 3781 | mCurOutput = 0; |
| 3782 | mDirectOutput = false; |
| 3783 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3784 | mTestOutputs[i] = 0; |
| 3785 | } |
| 3786 | |
| 3787 | const size_t SIZE = 256; |
| 3788 | char buffer[SIZE]; |
| 3789 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3790 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3791 | } |
| 3792 | #endif //AUDIO_POLICY_TEST |
| 3793 | } |
| 3794 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3795 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3796 | { |
| 3797 | #ifdef AUDIO_POLICY_TEST |
| 3798 | exit(); |
| 3799 | #endif //AUDIO_POLICY_TEST |
| 3800 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3801 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3802 | } |
| 3803 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3804 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3805 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3806 | mAvailableOutputDevices.clear(); |
| 3807 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3808 | mOutputs.clear(); |
| 3809 | mInputs.clear(); |
| 3810 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3811 | } |
| 3812 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3813 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3814 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3815 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3819 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3820 | { |
| 3821 | ALOGV("entering threadLoop()"); |
| 3822 | while (!exitPending()) |
| 3823 | { |
| 3824 | String8 command; |
| 3825 | int valueInt; |
| 3826 | String8 value; |
| 3827 | |
| 3828 | Mutex::Autolock _l(mLock); |
| 3829 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3830 | |
| 3831 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3832 | AudioParameter param = AudioParameter(command); |
| 3833 | |
| 3834 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3835 | valueInt != 0) { |
| 3836 | ALOGV("Test command %s received", command.string()); |
| 3837 | String8 target; |
| 3838 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3839 | target = "Manager"; |
| 3840 | } |
| 3841 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3842 | param.remove(String8("test_cmd_policy_output")); |
| 3843 | mCurOutput = valueInt; |
| 3844 | } |
| 3845 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3846 | param.remove(String8("test_cmd_policy_direct")); |
| 3847 | if (value == "false") { |
| 3848 | mDirectOutput = false; |
| 3849 | } else if (value == "true") { |
| 3850 | mDirectOutput = true; |
| 3851 | } |
| 3852 | } |
| 3853 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3854 | param.remove(String8("test_cmd_policy_input")); |
| 3855 | mTestInput = valueInt; |
| 3856 | } |
| 3857 | |
| 3858 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3859 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3860 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3861 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3862 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3863 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3864 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3865 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3866 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3867 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3868 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3869 | if (target == "Manager") { |
| 3870 | mTestFormat = format; |
| 3871 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3872 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3873 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3874 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3875 | } |
| 3876 | } |
| 3877 | } |
| 3878 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3879 | param.remove(String8("test_cmd_policy_channels")); |
| 3880 | int channels = 0; |
| 3881 | |
| 3882 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3883 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3884 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3885 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3886 | } |
| 3887 | if (channels != 0) { |
| 3888 | if (target == "Manager") { |
| 3889 | mTestChannels = channels; |
| 3890 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3891 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3892 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3893 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3894 | } |
| 3895 | } |
| 3896 | } |
| 3897 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3898 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3899 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3900 | int samplingRate = valueInt; |
| 3901 | if (target == "Manager") { |
| 3902 | mTestSamplingRate = samplingRate; |
| 3903 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3904 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3905 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3906 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3912 | param.remove(String8("test_cmd_policy_reopen")); |
| 3913 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3914 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3915 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3916 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3917 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3918 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3919 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3920 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3921 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3922 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3923 | config.sample_rate = outputDesc->mSamplingRate; |
| 3924 | config.channel_mask = outputDesc->mChannelMask; |
| 3925 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3926 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3927 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3928 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3929 | &config, |
| 3930 | &outputDesc->mDevice, |
| 3931 | String8(""), |
| 3932 | &outputDesc->mLatency, |
| 3933 | outputDesc->mFlags); |
| 3934 | if (status != NO_ERROR) { |
| 3935 | ALOGE("Failed to reopen hardware output stream, " |
| 3936 | "samplingRate: %d, format %d, channels %d", |
| 3937 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3938 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3939 | outputDesc->mSamplingRate = config.sample_rate; |
| 3940 | outputDesc->mChannelMask = config.channel_mask; |
| 3941 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3942 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3943 | AudioParameter outputCmd = AudioParameter(); |
| 3944 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3945 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3946 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | |
| 3951 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3952 | } |
| 3953 | } |
| 3954 | return false; |
| 3955 | } |
| 3956 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3957 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3958 | { |
| 3959 | { |
| 3960 | AutoMutex _l(mLock); |
| 3961 | requestExit(); |
| 3962 | mWaitWorkCV.signal(); |
| 3963 | } |
| 3964 | requestExitAndWait(); |
| 3965 | } |
| 3966 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3967 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3968 | { |
| 3969 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3970 | if (output == mTestOutputs[i]) return i; |
| 3971 | } |
| 3972 | return 0; |
| 3973 | } |
| 3974 | #endif //AUDIO_POLICY_TEST |
| 3975 | |
| 3976 | // --- |
| 3977 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3978 | void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3979 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3980 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3981 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3982 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3983 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3984 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3985 | } |
| 3986 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3987 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3988 | { |
| 3989 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3990 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3991 | } |
| 3992 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3993 | void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3994 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3995 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3996 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3997 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3998 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3999 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4000 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4001 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4002 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4003 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4004 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4005 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4006 | if (devDesc != 0) { |
| 4007 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 4008 | desc->mIoHandle, address.string()); |
| 4009 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4013 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4014 | audio_policy_dev_state_t state, |
| 4015 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4016 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4017 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4018 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4019 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4020 | |
| 4021 | if (audio_device_is_digital(device)) { |
| 4022 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4023 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4024 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4025 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4026 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4027 | // first list already open outputs that can be routed to this device |
| 4028 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4029 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4030 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4031 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4032 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 4033 | outputs.add(mOutputs.keyAt(i)); |
| 4034 | } else { |
| 4035 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4036 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4037 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4038 | } |
| 4039 | } |
| 4040 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4041 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4042 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4043 | { |
| 4044 | if (mHwModules[i]->mHandle == 0) { |
| 4045 | continue; |
| 4046 | } |
| 4047 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 4048 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4049 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4050 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4051 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4052 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4053 | profiles.add(profile); |
| 4054 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 4055 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4060 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4061 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4062 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4063 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4064 | return BAD_VALUE; |
| 4065 | } |
| 4066 | |
| 4067 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4068 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4069 | 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] | 4070 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4071 | |
| 4072 | // nothing to do if one output is already opened for this profile |
| 4073 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4074 | for (j = 0; j < outputs.size(); j++) { |
| 4075 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4076 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4077 | // matching profile: save the sample rates, format and channel masks supported |
| 4078 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4079 | if (audio_device_is_digital(device)) { |
| 4080 | devDesc->importAudioPort(profile); |
| 4081 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4082 | break; |
| 4083 | } |
| 4084 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4085 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | continue; |
| 4087 | } |
| 4088 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4089 | ALOGV("opening output for device %08x with params %s profile %p", |
| 4090 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4091 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4092 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4093 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4094 | config.sample_rate = desc->mSamplingRate; |
| 4095 | config.channel_mask = desc->mChannelMask; |
| 4096 | config.format = desc->mFormat; |
| 4097 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 4098 | config.offload_info.channel_mask = desc->mChannelMask; |
| 4099 | config.offload_info.format = desc->mFormat; |
| 4100 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4101 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4102 | &output, |
| 4103 | &config, |
| 4104 | &desc->mDevice, |
| 4105 | address, |
| 4106 | &desc->mLatency, |
| 4107 | desc->mFlags); |
| 4108 | if (status == NO_ERROR) { |
| 4109 | desc->mSamplingRate = config.sample_rate; |
| 4110 | desc->mChannelMask = config.channel_mask; |
| 4111 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4112 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4113 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4114 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4115 | char *param = audio_device_address_to_parameter(device, address); |
| 4116 | mpClientInterface->setParameters(output, String8(param)); |
| 4117 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4118 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4119 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4120 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4121 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4122 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4123 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4124 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4125 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4126 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4127 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4128 | config.offload_info.sample_rate = config.sample_rate; |
| 4129 | config.offload_info.channel_mask = config.channel_mask; |
| 4130 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4131 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4132 | &output, |
| 4133 | &config, |
| 4134 | &desc->mDevice, |
| 4135 | address, |
| 4136 | &desc->mLatency, |
| 4137 | desc->mFlags); |
| 4138 | if (status == NO_ERROR) { |
| 4139 | desc->mSamplingRate = config.sample_rate; |
| 4140 | desc->mChannelMask = config.channel_mask; |
| 4141 | desc->mFormat = config.format; |
| 4142 | } else { |
| 4143 | output = AUDIO_IO_HANDLE_NONE; |
| 4144 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4145 | } |
| 4146 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4147 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4148 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4149 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4150 | sp<AudioPolicyMix> policyMix; |
| 4151 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4152 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4153 | address.string()); |
| 4154 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4155 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4156 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4157 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4158 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4159 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4160 | // no duplicated output for direct outputs and |
| 4161 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4162 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4163 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4164 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4165 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4166 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4167 | //TODO: configure audio effect output stage here |
| 4168 | |
| 4169 | // 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] | 4170 | duplicatedOutput = |
| 4171 | mpClientInterface->openDuplicateOutput(output, |
| 4172 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4173 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4174 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4175 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4176 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4177 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 4178 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4179 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 4180 | dupOutputDesc->mFormat = desc->mFormat; |
| 4181 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 4182 | dupOutputDesc->mLatency = desc->mLatency; |
| 4183 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4184 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4185 | } else { |
| 4186 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4187 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4188 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4189 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4190 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4191 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4192 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4193 | } |
| 4194 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4195 | } else { |
| 4196 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4197 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4198 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4199 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4200 | profiles.removeAt(profile_index); |
| 4201 | profile_index--; |
| 4202 | } else { |
| 4203 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4204 | // Load digital format info only for digital devices |
| 4205 | if (audio_device_is_digital(device)) { |
| 4206 | devDesc->importAudioPort(profile); |
| 4207 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4208 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4209 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4210 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4211 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4212 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4213 | NULL/*patch handle*/, address.string()); |
| 4214 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4216 | } |
| 4217 | } |
| 4218 | |
| 4219 | if (profiles.isEmpty()) { |
| 4220 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4221 | return BAD_VALUE; |
| 4222 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4223 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 | // check if one opened output is not needed any more after disconnecting one device |
| 4225 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4226 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4227 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4228 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4229 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4230 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4231 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4232 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4233 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4234 | mOutputs.keyAt(i)); |
| 4235 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4236 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4237 | } |
| 4238 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4239 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4240 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4241 | { |
| 4242 | if (mHwModules[i]->mHandle == 0) { |
| 4243 | continue; |
| 4244 | } |
| 4245 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 4246 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4247 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4248 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4249 | ALOGV("checkOutputsForDevice(): " |
| 4250 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4251 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4252 | } |
| 4253 | } |
| 4254 | } |
| 4255 | } |
| 4256 | return NO_ERROR; |
| 4257 | } |
| 4258 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4259 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4260 | audio_policy_dev_state_t state, |
| 4261 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4262 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4263 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4264 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4265 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4266 | |
| 4267 | if (audio_device_is_digital(device)) { |
| 4268 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4269 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4270 | } |
| 4271 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4272 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4273 | // first list already open inputs that can be routed to this device |
| 4274 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4275 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4276 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4277 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4278 | inputs.add(mInputs.keyAt(input_index)); |
| 4279 | } |
| 4280 | } |
| 4281 | |
| 4282 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4283 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4284 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 4285 | { |
| 4286 | if (mHwModules[module_idx]->mHandle == 0) { |
| 4287 | continue; |
| 4288 | } |
| 4289 | for (size_t profile_index = 0; |
| 4290 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 4291 | profile_index++) |
| 4292 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4293 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 4294 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4295 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4296 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4297 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4298 | profiles.add(profile); |
| 4299 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 4300 | profile_index, module_idx); |
| 4301 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4307 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4308 | return BAD_VALUE; |
| 4309 | } |
| 4310 | |
| 4311 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4312 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4313 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4314 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4315 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4316 | // nothing to do if one input is already opened for this profile |
| 4317 | size_t input_index; |
| 4318 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4319 | desc = mInputs.valueAt(input_index); |
| 4320 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4321 | if (audio_device_is_digital(device)) { |
| 4322 | devDesc->importAudioPort(profile); |
| 4323 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4324 | break; |
| 4325 | } |
| 4326 | } |
| 4327 | if (input_index != mInputs.size()) { |
| 4328 | continue; |
| 4329 | } |
| 4330 | |
| 4331 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 4332 | desc = new AudioInputDescriptor(profile); |
| 4333 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4334 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4335 | config.sample_rate = desc->mSamplingRate; |
| 4336 | config.channel_mask = desc->mChannelMask; |
| 4337 | config.format = desc->mFormat; |
| 4338 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4339 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4340 | &input, |
| 4341 | &config, |
| 4342 | &desc->mDevice, |
| 4343 | address, |
| 4344 | AUDIO_SOURCE_MIC, |
| 4345 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4346 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4347 | if (status == NO_ERROR) { |
| 4348 | desc->mSamplingRate = config.sample_rate; |
| 4349 | desc->mChannelMask = config.channel_mask; |
| 4350 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4351 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4352 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4353 | char *param = audio_device_address_to_parameter(device, address); |
| 4354 | mpClientInterface->setParameters(input, String8(param)); |
| 4355 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4356 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4357 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4358 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4359 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 4360 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4361 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4362 | } |
| 4363 | |
| 4364 | if (input != 0) { |
| 4365 | addInput(input, desc); |
| 4366 | } |
| 4367 | } // endif input != 0 |
| 4368 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4369 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4370 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4371 | profiles.removeAt(profile_index); |
| 4372 | profile_index--; |
| 4373 | } else { |
| 4374 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4375 | if (audio_device_is_digital(device)) { |
| 4376 | devDesc->importAudioPort(profile); |
| 4377 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4378 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4379 | } |
| 4380 | } // end scan profiles |
| 4381 | |
| 4382 | if (profiles.isEmpty()) { |
| 4383 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4384 | return BAD_VALUE; |
| 4385 | } |
| 4386 | } else { |
| 4387 | // Disconnect |
| 4388 | // check if one opened input is not needed any more after disconnecting one device |
| 4389 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4390 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4391 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4392 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4393 | mInputs.keyAt(input_index)); |
| 4394 | inputs.add(mInputs.keyAt(input_index)); |
| 4395 | } |
| 4396 | } |
| 4397 | // Clear any profiles associated with the disconnected device. |
| 4398 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4399 | if (mHwModules[module_index]->mHandle == 0) { |
| 4400 | continue; |
| 4401 | } |
| 4402 | for (size_t profile_index = 0; |
| 4403 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4404 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4405 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4406 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4407 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4408 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4409 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4410 | } |
| 4411 | } |
| 4412 | } |
| 4413 | } // end disconnect |
| 4414 | |
| 4415 | return NO_ERROR; |
| 4416 | } |
| 4417 | |
| 4418 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4419 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4420 | { |
| 4421 | ALOGV("closeOutput(%d)", output); |
| 4422 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4423 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4424 | if (outputDesc == NULL) { |
| 4425 | ALOGW("closeOutput() unknown output %d", output); |
| 4426 | return; |
| 4427 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4428 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4429 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4430 | // look for duplicated outputs connected to the output being removed. |
| 4431 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4432 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4433 | if (dupOutputDesc->isDuplicated() && |
| 4434 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4435 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4436 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4437 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4438 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4439 | } else { |
| 4440 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4441 | } |
| 4442 | // As all active tracks on duplicated output will be deleted, |
| 4443 | // and as they were also referenced on the other output, the reference |
| 4444 | // count for their stream type must be adjusted accordingly on |
| 4445 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4446 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4447 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4448 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4449 | } |
| 4450 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4451 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4452 | |
| 4453 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4454 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4455 | } |
| 4456 | } |
| 4457 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4458 | nextAudioPortGeneration(); |
| 4459 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4460 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4461 | if (index >= 0) { |
| 4462 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4463 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4464 | mAudioPatches.removeItemsAt(index); |
| 4465 | mpClientInterface->onAudioPatchListUpdate(); |
| 4466 | } |
| 4467 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4468 | AudioParameter param; |
| 4469 | param.add(String8("closing"), String8("true")); |
| 4470 | mpClientInterface->setParameters(output, param.toString()); |
| 4471 | |
| 4472 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4473 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4474 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4478 | { |
| 4479 | ALOGV("closeInput(%d)", input); |
| 4480 | |
| 4481 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4482 | if (inputDesc == NULL) { |
| 4483 | ALOGW("closeInput() unknown input %d", input); |
| 4484 | return; |
| 4485 | } |
| 4486 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4487 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4488 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4489 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4490 | if (index >= 0) { |
| 4491 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4492 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4493 | mAudioPatches.removeItemsAt(index); |
| 4494 | mpClientInterface->onAudioPatchListUpdate(); |
| 4495 | } |
| 4496 | |
| 4497 | mpClientInterface->closeInput(input); |
| 4498 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4499 | } |
| 4500 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4501 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4502 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4503 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4504 | { |
| 4505 | SortedVector<audio_io_handle_t> outputs; |
| 4506 | |
| 4507 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4508 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4509 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4510 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4511 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4512 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4513 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4514 | outputs.add(openOutputs.keyAt(i)); |
| 4515 | } |
| 4516 | } |
| 4517 | return outputs; |
| 4518 | } |
| 4519 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4520 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4521 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4522 | { |
| 4523 | if (outputs1.size() != outputs2.size()) { |
| 4524 | return false; |
| 4525 | } |
| 4526 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4527 | if (outputs1[i] != outputs2[i]) { |
| 4528 | return false; |
| 4529 | } |
| 4530 | } |
| 4531 | return true; |
| 4532 | } |
| 4533 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4534 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4535 | { |
| 4536 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4537 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4538 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4539 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4540 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4541 | // also take into account external policy-related changes: add all outputs which are |
| 4542 | // associated with policies in the "before" and "after" output vectors |
| 4543 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4544 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4545 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4546 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4547 | srcOutputs.add(desc->mIoHandle); |
| 4548 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4549 | } |
| 4550 | } |
| 4551 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4552 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4553 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4554 | dstOutputs.add(desc->mIoHandle); |
| 4555 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4556 | } |
| 4557 | } |
| 4558 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4559 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4560 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4561 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4562 | // mute strategy while moving tracks from one output to another |
| 4563 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4564 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4565 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4566 | setStrategyMute(strategy, true, desc); |
| 4567 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4568 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4569 | sp<AudioSourceDescriptor> source = |
| 4570 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4571 | if (source != 0){ |
| 4572 | connectAudioSource(source); |
| 4573 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | // Move effects associated to this strategy from previous output to new output |
| 4577 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4578 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4579 | } |
| 4580 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4581 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4582 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4583 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4584 | } |
| 4585 | } |
| 4586 | } |
| 4587 | } |
| 4588 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4589 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4590 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4591 | 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] | 4592 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4593 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4594 | 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] | 4595 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4597 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4598 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4599 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4600 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4601 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4604 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4605 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4606 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4607 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4608 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4609 | return; |
| 4610 | } |
| 4611 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4612 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4613 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4614 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4615 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4616 | |
| 4617 | // if suspended, restore A2DP output if: |
| 4618 | // ((SCO device is NOT connected) || |
| 4619 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4620 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4621 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4622 | // if not suspended, suspend A2DP output if: |
| 4623 | // (SCO device is connected) && |
| 4624 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4625 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4626 | // |
| 4627 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4628 | if (!isScoConnected || |
| 4629 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4630 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4631 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4632 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4633 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4634 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4635 | |
| 4636 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4637 | mA2dpSuspended = false; |
| 4638 | } |
| 4639 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4640 | if (isScoConnected && |
| 4641 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4642 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4643 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4644 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4645 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4646 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4647 | |
| 4648 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4649 | mA2dpSuspended = true; |
| 4650 | } |
| 4651 | } |
| 4652 | } |
| 4653 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4654 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4655 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4656 | { |
| 4657 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4658 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4659 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4660 | if (index >= 0) { |
| 4661 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4662 | if (patchDesc->mUid != mUidCached) { |
| 4663 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4664 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4665 | return outputDesc->device(); |
| 4666 | } |
| 4667 | } |
| 4668 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4669 | // 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] | 4670 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4671 | // use device for strategy enforced audible |
| 4672 | // 2: we are in call or the strategy phone is active on the output: |
| 4673 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4674 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4675 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4676 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4677 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4678 | // 5: the strategy accessibility is active on the output: |
| 4679 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4680 | // 6: the strategy "respectful" sonification is active on the output: |
| 4681 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4682 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4683 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4684 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4685 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4686 | // 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] | 4687 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4688 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4689 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4690 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4691 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4692 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4693 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4694 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4695 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4696 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4697 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4698 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4699 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4700 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4701 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4702 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4703 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4704 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4705 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4706 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4707 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4708 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4709 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4712 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4713 | return device; |
| 4714 | } |
| 4715 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4716 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4717 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4718 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4719 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4720 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4721 | if (index >= 0) { |
| 4722 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4723 | if (patchDesc->mUid != mUidCached) { |
| 4724 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4725 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4726 | return inputDesc->mDevice; |
| 4727 | } |
| 4728 | } |
| 4729 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4730 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
| 4731 | if (isInCall()) { |
| 4732 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4733 | } else if (source != AUDIO_SOURCE_DEFAULT) { |
| 4734 | device = getDeviceAndMixForInputSource(source); |
| 4735 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4736 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4737 | return device; |
| 4738 | } |
| 4739 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4740 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4741 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4742 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4743 | } |
| 4744 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4745 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4746 | return (uint32_t)getStrategy(stream); |
| 4747 | } |
| 4748 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4749 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4750 | // By checking the range of stream before calling getStrategy, we avoid |
| 4751 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4752 | // 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] | 4753 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4754 | return AUDIO_DEVICE_NONE; |
| 4755 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4756 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4757 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4758 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4759 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4760 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4761 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4762 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4763 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4764 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4765 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4766 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4767 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4768 | curDevices |= outputDesc->device(); |
| 4769 | } |
| 4770 | } |
| 4771 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4772 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4773 | |
| 4774 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4775 | and doesn't really need to.*/ |
| 4776 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4777 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4778 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4779 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4780 | return devices; |
| 4781 | } |
| 4782 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4783 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4784 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4785 | 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] | 4786 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4787 | } |
| 4788 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4789 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4790 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4791 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4792 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4793 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4794 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4795 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4796 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4797 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4798 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4799 | } |
| 4800 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4801 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4802 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4803 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4804 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4805 | updateDevicesAndOutputs(); |
| 4806 | break; |
| 4807 | default: |
| 4808 | break; |
| 4809 | } |
| 4810 | } |
| 4811 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4812 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4813 | |
| 4814 | // skip beacon mute management if a dedicated TTS output is available |
| 4815 | if (mTtsOutputAvailable) { |
| 4816 | return 0; |
| 4817 | } |
| 4818 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4819 | switch(event) { |
| 4820 | case STARTING_OUTPUT: |
| 4821 | mBeaconMuteRefCount++; |
| 4822 | break; |
| 4823 | case STOPPING_OUTPUT: |
| 4824 | if (mBeaconMuteRefCount > 0) { |
| 4825 | mBeaconMuteRefCount--; |
| 4826 | } |
| 4827 | break; |
| 4828 | case STARTING_BEACON: |
| 4829 | mBeaconPlayingRefCount++; |
| 4830 | break; |
| 4831 | case STOPPING_BEACON: |
| 4832 | if (mBeaconPlayingRefCount > 0) { |
| 4833 | mBeaconPlayingRefCount--; |
| 4834 | } |
| 4835 | break; |
| 4836 | } |
| 4837 | |
| 4838 | if (mBeaconMuteRefCount > 0) { |
| 4839 | // any playback causes beacon to be muted |
| 4840 | return setBeaconMute(true); |
| 4841 | } else { |
| 4842 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4843 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4844 | } |
| 4845 | } |
| 4846 | |
| 4847 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4848 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4849 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4850 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4851 | if (mBeaconMuted != mute) { |
| 4852 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4853 | ALOGV("\t muting %d", mute); |
| 4854 | uint32_t maxLatency = 0; |
| 4855 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4856 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4857 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4858 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4859 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4860 | const uint32_t latency = desc->latency() * 2; |
| 4861 | if (latency > maxLatency) { |
| 4862 | maxLatency = latency; |
| 4863 | } |
| 4864 | } |
| 4865 | mBeaconMuted = mute; |
| 4866 | return maxLatency; |
| 4867 | } |
| 4868 | return 0; |
| 4869 | } |
| 4870 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4871 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4872 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4873 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4874 | // Routing |
| 4875 | // see if we have an explicit route |
| 4876 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4877 | // (getStrategy(stream)). |
| 4878 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4879 | // and activity count is non-zero |
| 4880 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4881 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4882 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4883 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4884 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4885 | return route->mDeviceDescriptor->type(); |
| 4886 | } |
| 4887 | } |
| 4888 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4889 | if (fromCache) { |
| 4890 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4891 | strategy, mDeviceForStrategy[strategy]); |
| 4892 | return mDeviceForStrategy[strategy]; |
| 4893 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4894 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4895 | } |
| 4896 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4897 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4898 | { |
| 4899 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4900 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4901 | } |
| 4902 | mPreviousOutputs = mOutputs; |
| 4903 | } |
| 4904 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4905 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4906 | audio_devices_t prevDevice, |
| 4907 | uint32_t delayMs) |
| 4908 | { |
| 4909 | // mute/unmute strategies using an incompatible device combination |
| 4910 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4911 | // if unmuting, unmute only after the specified delay |
| 4912 | if (outputDesc->isDuplicated()) { |
| 4913 | return 0; |
| 4914 | } |
| 4915 | |
| 4916 | uint32_t muteWaitMs = 0; |
| 4917 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4918 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4919 | |
| 4920 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4921 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4922 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4923 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4924 | bool doMute = false; |
| 4925 | |
| 4926 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4927 | doMute = true; |
| 4928 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4929 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4930 | doMute = true; |
| 4931 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4932 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4933 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4934 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4935 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4936 | // skip output if it does not share any device with current output |
| 4937 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4938 | == AUDIO_DEVICE_NONE) { |
| 4939 | continue; |
| 4940 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4941 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4942 | mute ? "muting" : "unmuting", i, curDevice); |
| 4943 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4944 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4945 | if (mute) { |
| 4946 | // FIXME: should not need to double latency if volume could be applied |
| 4947 | // immediately by the audioflinger mixer. We must account for the delay |
| 4948 | // between now and the next time the audioflinger thread for this output |
| 4949 | // will process a buffer (which corresponds to one buffer size, |
| 4950 | // usually 1/2 or 1/4 of the latency). |
| 4951 | if (muteWaitMs < desc->latency() * 2) { |
| 4952 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4953 | } |
| 4954 | } |
| 4955 | } |
| 4956 | } |
| 4957 | } |
| 4958 | } |
| 4959 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4960 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4961 | // different per device volumes |
| 4962 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4963 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4964 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4965 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4966 | if (muteWaitMs < tempMuteWaitMs) { |
| 4967 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4968 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4969 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4970 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4971 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4972 | // make sure that we do not start the temporary mute period too early in case of |
| 4973 | // delayed device change |
| 4974 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4975 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4976 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4977 | } |
| 4978 | } |
| 4979 | } |
| 4980 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4981 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4982 | if (muteWaitMs > delayMs) { |
| 4983 | muteWaitMs -= delayMs; |
| 4984 | usleep(muteWaitMs * 1000); |
| 4985 | return muteWaitMs; |
| 4986 | } |
| 4987 | return 0; |
| 4988 | } |
| 4989 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4990 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4991 | audio_devices_t device, |
| 4992 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4993 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4994 | audio_patch_handle_t *patchHandle, |
| 4995 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4996 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4997 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4998 | AudioParameter param; |
| 4999 | uint32_t muteWaitMs; |
| 5000 | |
| 5001 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5002 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 5003 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5004 | return muteWaitMs; |
| 5005 | } |
| 5006 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 5007 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5008 | if ((device != AUDIO_DEVICE_NONE) && |
| 5009 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5010 | return 0; |
| 5011 | } |
| 5012 | |
| 5013 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5014 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5015 | |
| 5016 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 5017 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5018 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5019 | |
| 5020 | if (device != AUDIO_DEVICE_NONE) { |
| 5021 | outputDesc->mDevice = device; |
| 5022 | } |
| 5023 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 5024 | |
| 5025 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 5026 | // the requested device is AUDIO_DEVICE_NONE |
| 5027 | // OR the requested device is the same as current device |
| 5028 | // AND force is not specified |
| 5029 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5030 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 5031 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 5032 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5033 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5034 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5035 | return muteWaitMs; |
| 5036 | } |
| 5037 | |
| 5038 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5039 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5040 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5041 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5042 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5043 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 5044 | DeviceVector deviceList; |
| 5045 | if ((address == NULL) || (strlen(address) == 0)) { |
| 5046 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 5047 | } else { |
| 5048 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 5049 | } |
| 5050 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5051 | if (!deviceList.isEmpty()) { |
| 5052 | struct audio_patch patch; |
| 5053 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 5054 | patch.num_sources = 1; |
| 5055 | patch.num_sinks = 0; |
| 5056 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 5057 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5058 | patch.num_sinks++; |
| 5059 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5060 | ssize_t index; |
| 5061 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5062 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5063 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5064 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5065 | } |
| 5066 | sp< AudioPatch> patchDesc; |
| 5067 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5068 | if (index >= 0) { |
| 5069 | patchDesc = mAudioPatches.valueAt(index); |
| 5070 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5071 | } |
| 5072 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5073 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5074 | &afPatchHandle, |
| 5075 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5076 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 5077 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5078 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5079 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5080 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5081 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5082 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5083 | } else { |
| 5084 | patchDesc->mPatch = patch; |
| 5085 | } |
| 5086 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5087 | if (patchHandle) { |
| 5088 | *patchHandle = patchDesc->mHandle; |
| 5089 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5090 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5091 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5092 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5093 | } |
| 5094 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5095 | |
| 5096 | // inform all input as well |
| 5097 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5098 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5099 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5100 | AudioParameter inputCmd = AudioParameter(); |
| 5101 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5102 | inputDescriptor->mIoHandle, device); |
| 5103 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5104 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5105 | inputCmd.toString(), |
| 5106 | delayMs); |
| 5107 | } |
| 5108 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5109 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5110 | |
| 5111 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5112 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5113 | |
| 5114 | return muteWaitMs; |
| 5115 | } |
| 5116 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5117 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5118 | int delayMs, |
| 5119 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5120 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5121 | ssize_t index; |
| 5122 | if (patchHandle) { |
| 5123 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5124 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5125 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5126 | } |
| 5127 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5128 | return INVALID_OPERATION; |
| 5129 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5130 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5131 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5132 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5133 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5134 | removeAudioPatch(patchDesc->mHandle); |
| 5135 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5136 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5137 | return status; |
| 5138 | } |
| 5139 | |
| 5140 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5141 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5142 | bool force, |
| 5143 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5144 | { |
| 5145 | status_t status = NO_ERROR; |
| 5146 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5147 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5148 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5149 | inputDesc->mDevice = device; |
| 5150 | |
| 5151 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 5152 | if (!deviceList.isEmpty()) { |
| 5153 | struct audio_patch patch; |
| 5154 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5155 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5156 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5157 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 5158 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5159 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5160 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5161 | patch.num_sinks = 1; |
| 5162 | //only one input device for now |
| 5163 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5164 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5165 | ssize_t index; |
| 5166 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5167 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5168 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5169 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5170 | } |
| 5171 | sp< AudioPatch> patchDesc; |
| 5172 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5173 | if (index >= 0) { |
| 5174 | patchDesc = mAudioPatches.valueAt(index); |
| 5175 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5176 | } |
| 5177 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5178 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5179 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5180 | 0); |
| 5181 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5182 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5183 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5184 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5185 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5186 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5187 | } else { |
| 5188 | patchDesc->mPatch = patch; |
| 5189 | } |
| 5190 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5191 | if (patchHandle) { |
| 5192 | *patchHandle = patchDesc->mHandle; |
| 5193 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5194 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5195 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5196 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5197 | } |
| 5198 | } |
| 5199 | } |
| 5200 | return status; |
| 5201 | } |
| 5202 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5203 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5204 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5205 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5206 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5207 | ssize_t index; |
| 5208 | if (patchHandle) { |
| 5209 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5210 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5211 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5212 | } |
| 5213 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5214 | return INVALID_OPERATION; |
| 5215 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5216 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5217 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5218 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5219 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5220 | removeAudioPatch(patchDesc->mHandle); |
| 5221 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5222 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5223 | return status; |
| 5224 | } |
| 5225 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5226 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5227 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5228 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5229 | audio_format_t& format, |
| 5230 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5231 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5232 | { |
| 5233 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5234 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5235 | // |
| 5236 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5237 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5238 | |
| 5239 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 5240 | { |
| 5241 | if (mHwModules[i]->mHandle == 0) { |
| 5242 | continue; |
| 5243 | } |
| 5244 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 5245 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5246 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5247 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5248 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5249 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5250 | format, |
| 5251 | &format /*updatedFormat*/, |
| 5252 | channelMask, |
| 5253 | &channelMask /*updatedChannelMask*/, |
| 5254 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5255 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5256 | return profile; |
| 5257 | } |
| 5258 | } |
| 5259 | } |
| 5260 | return NULL; |
| 5261 | } |
| 5262 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5263 | |
| 5264 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5265 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5266 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5267 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5268 | audio_devices_t selectedDeviceFromMix = |
| 5269 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5270 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5271 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5272 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5273 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5274 | return getDeviceForInputSource(inputSource); |
| 5275 | } |
| 5276 | |
| 5277 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5278 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5279 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5280 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5281 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5282 | return route->mDeviceDescriptor->type(); |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5287 | } |
| 5288 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5289 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5290 | int index, |
| 5291 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5292 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5293 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5294 | |
| 5295 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5296 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5297 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5298 | // the ringtone volume |
| 5299 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5300 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5301 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5302 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5303 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5304 | } |
| 5305 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5306 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5307 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5308 | // - always attenuate notifications volume by 6dB |
| 5309 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5310 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5311 | // - if music is playing, always limit the volume to current music volume, |
| 5312 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5313 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5314 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5315 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5316 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5317 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 5318 | AUDIO_DEVICE_OUT_USB_HEADSET)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5319 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5320 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5321 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5322 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5323 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5324 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5325 | // when the phone is ringing we must consider that music could have been paused just before |
| 5326 | // by the music application and behave as if music was active if the last music track was |
| 5327 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5328 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5329 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5330 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5331 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5332 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5333 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5334 | musicDevice), |
| 5335 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5336 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5337 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5338 | if (volumeDB > minVolDB) { |
| 5339 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5340 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5341 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5342 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5343 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5344 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5345 | // intended to be played |
| 5346 | if ((volumeDB > -96.0f) && |
| 5347 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5348 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5349 | stream, device, |
| 5350 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5351 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5352 | } |
| 5353 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5354 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5355 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5356 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5357 | } |
| 5358 | } |
| 5359 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5360 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5361 | } |
| 5362 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5363 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5364 | int index, |
| 5365 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5366 | audio_devices_t device, |
| 5367 | int delayMs, |
| 5368 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5369 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5370 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5371 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5372 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5373 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5374 | return NO_ERROR; |
| 5375 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5376 | audio_policy_forced_cfg_t forceUseForComm = |
| 5377 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5378 | // 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] | 5379 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5380 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5381 | 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] | 5382 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5383 | return INVALID_OPERATION; |
| 5384 | } |
| 5385 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5386 | if (device == AUDIO_DEVICE_NONE) { |
| 5387 | device = outputDesc->device(); |
| 5388 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5389 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5390 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5391 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5392 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5393 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5394 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5395 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5396 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5397 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5398 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5399 | float voiceVolume; |
| 5400 | // 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] | 5401 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5402 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5403 | } else { |
| 5404 | voiceVolume = 1.0; |
| 5405 | } |
| 5406 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5407 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5408 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5409 | mLastVoiceVolume = voiceVolume; |
| 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | return NO_ERROR; |
| 5414 | } |
| 5415 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5416 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5417 | audio_devices_t device, |
| 5418 | int delayMs, |
| 5419 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5420 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5421 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5422 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5423 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5424 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5425 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5426 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5427 | device, |
| 5428 | delayMs, |
| 5429 | force); |
| 5430 | } |
| 5431 | } |
| 5432 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5433 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5434 | bool on, |
| 5435 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5436 | int delayMs, |
| 5437 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5438 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5439 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5440 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5441 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5442 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5443 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5444 | } |
| 5445 | } |
| 5446 | } |
| 5447 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5448 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5449 | bool on, |
| 5450 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5451 | int delayMs, |
| 5452 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5453 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5454 | if (device == AUDIO_DEVICE_NONE) { |
| 5455 | device = outputDesc->device(); |
| 5456 | } |
| 5457 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5458 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5459 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5460 | |
| 5461 | if (on) { |
| 5462 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5463 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5464 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5465 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5466 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5470 | outputDesc->mMuteCount[stream]++; |
| 5471 | } else { |
| 5472 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5473 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5474 | return; |
| 5475 | } |
| 5476 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5477 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5478 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5479 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5480 | device, |
| 5481 | delayMs); |
| 5482 | } |
| 5483 | } |
| 5484 | } |
| 5485 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5486 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5487 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5488 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5489 | if(!hasPrimaryOutput()) { |
| 5490 | return; |
| 5491 | } |
| 5492 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5493 | // if the stream pertains to sonification strategy and we are in call we must |
| 5494 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5495 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5496 | // interfere with the device used for phone strategy |
| 5497 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5498 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5499 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5500 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5501 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5502 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5503 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5504 | stream, starting, outputDesc->mDevice, stateChange); |
| 5505 | if (outputDesc->mRefCount[stream]) { |
| 5506 | int muteCount = 1; |
| 5507 | if (stateChange) { |
| 5508 | muteCount = outputDesc->mRefCount[stream]; |
| 5509 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5510 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5511 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5512 | for (int i = 0; i < muteCount; i++) { |
| 5513 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5514 | } |
| 5515 | } else { |
| 5516 | ALOGV("handleIncallSonification() high visibility"); |
| 5517 | if (outputDesc->device() & |
| 5518 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5519 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5520 | for (int i = 0; i < muteCount; i++) { |
| 5521 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5522 | } |
| 5523 | } |
| 5524 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5525 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5526 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5527 | } else { |
| 5528 | mpClientInterface->stopTone(); |
| 5529 | } |
| 5530 | } |
| 5531 | } |
| 5532 | } |
| 5533 | } |
| 5534 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5535 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5536 | { |
| 5537 | // flags to stream type mapping |
| 5538 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5539 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5540 | } |
| 5541 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5542 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5543 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5544 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5545 | return AUDIO_STREAM_TTS; |
| 5546 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5547 | |
| 5548 | // usage to stream type mapping |
| 5549 | switch (attr->usage) { |
| 5550 | case AUDIO_USAGE_MEDIA: |
| 5551 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5552 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5553 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5554 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5555 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5556 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5557 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5558 | return AUDIO_STREAM_SYSTEM; |
| 5559 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5560 | return AUDIO_STREAM_VOICE_CALL; |
| 5561 | |
| 5562 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5563 | return AUDIO_STREAM_DTMF; |
| 5564 | |
| 5565 | case AUDIO_USAGE_ALARM: |
| 5566 | return AUDIO_STREAM_ALARM; |
| 5567 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5568 | return AUDIO_STREAM_RING; |
| 5569 | |
| 5570 | case AUDIO_USAGE_NOTIFICATION: |
| 5571 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5572 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5573 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5574 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5575 | return AUDIO_STREAM_NOTIFICATION; |
| 5576 | |
| 5577 | case AUDIO_USAGE_UNKNOWN: |
| 5578 | default: |
| 5579 | return AUDIO_STREAM_MUSIC; |
| 5580 | } |
| 5581 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5582 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5583 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5584 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5585 | // has flags that map to a strategy? |
| 5586 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5587 | return true; |
| 5588 | } |
| 5589 | |
| 5590 | // has known usage? |
| 5591 | switch (paa->usage) { |
| 5592 | case AUDIO_USAGE_UNKNOWN: |
| 5593 | case AUDIO_USAGE_MEDIA: |
| 5594 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5595 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5596 | case AUDIO_USAGE_ALARM: |
| 5597 | case AUDIO_USAGE_NOTIFICATION: |
| 5598 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5599 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5600 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5601 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5602 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5603 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5604 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5605 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5606 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5607 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5608 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5609 | break; |
| 5610 | default: |
| 5611 | return false; |
| 5612 | } |
| 5613 | return true; |
| 5614 | } |
| 5615 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5616 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5617 | routing_strategy strategy, uint32_t inPastMs, |
| 5618 | nsecs_t sysTime) const |
| 5619 | { |
| 5620 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5621 | sysTime = systemTime(); |
| 5622 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5623 | 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] | 5624 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5625 | (NUM_STRATEGIES == strategy)) && |
| 5626 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5627 | return true; |
| 5628 | } |
| 5629 | } |
| 5630 | return false; |
| 5631 | } |
| 5632 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5633 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5634 | { |
| 5635 | return mEngine->getForceUse(usage); |
| 5636 | } |
| 5637 | |
| 5638 | bool AudioPolicyManager::isInCall() |
| 5639 | { |
| 5640 | return isStateInCall(mEngine->getPhoneState()); |
| 5641 | } |
| 5642 | |
| 5643 | bool AudioPolicyManager::isStateInCall(int state) |
| 5644 | { |
| 5645 | return is_state_in_call(state); |
| 5646 | } |
| 5647 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5648 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5649 | { |
| 5650 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5651 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5652 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5653 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5654 | stopAudioSource(sourceDesc->getHandle()); |
| 5655 | } |
| 5656 | } |
| 5657 | |
| 5658 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5659 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5660 | bool release = false; |
| 5661 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5662 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5663 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5664 | source->ext.device.type == deviceDesc->type()) { |
| 5665 | release = true; |
| 5666 | } |
| 5667 | } |
| 5668 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5669 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5670 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5671 | sink->ext.device.type == deviceDesc->type()) { |
| 5672 | release = true; |
| 5673 | } |
| 5674 | } |
| 5675 | if (release) { |
| 5676 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5677 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5678 | } |
| 5679 | } |
| 5680 | } |
| 5681 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5682 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5683 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5684 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5685 | // TODO Set this based on Config properties. |
| 5686 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5687 | |
| 5688 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5689 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5690 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5691 | |
| 5692 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5693 | bool supportsAC3 = false; |
| 5694 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5695 | bool supportsIEC61937 = false; |
| 5696 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5697 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5698 | switch (format) { |
| 5699 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5700 | supportsAC3 = true; |
| 5701 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5702 | case AUDIO_FORMAT_E_AC3: |
| 5703 | case AUDIO_FORMAT_DTS: |
| 5704 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5705 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5706 | break; |
| 5707 | case AUDIO_FORMAT_IEC61937: |
| 5708 | supportsIEC61937 = true; |
| 5709 | break; |
| 5710 | default: |
| 5711 | break; |
| 5712 | } |
| 5713 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5714 | |
| 5715 | // Modify formats based on surround preferences. |
| 5716 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5717 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5718 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5719 | // Remove surround sound related formats. |
| 5720 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5721 | audio_format_t format = formats[formatIndex]; |
| 5722 | switch(format) { |
| 5723 | case AUDIO_FORMAT_AC3: |
| 5724 | case AUDIO_FORMAT_E_AC3: |
| 5725 | case AUDIO_FORMAT_DTS: |
| 5726 | case AUDIO_FORMAT_DTS_HD: |
| 5727 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5728 | formats.removeAt(formatIndex); |
| 5729 | break; |
| 5730 | default: |
| 5731 | formatIndex++; // keep it |
| 5732 | break; |
| 5733 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5734 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5735 | supportsAC3 = false; |
| 5736 | supportsOtherSurround = false; |
| 5737 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5738 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5739 | } else { // AUTO or ALWAYS |
| 5740 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5741 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5742 | && !supportsAC3) { |
| 5743 | formats.add(AUDIO_FORMAT_AC3); |
| 5744 | supportsAC3 = true; |
| 5745 | } |
| 5746 | |
| 5747 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5748 | // This assumes that if any of these formats are reported by the HAL |
| 5749 | // then the report is valid and should not be modified. |
| 5750 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5751 | && !supportsOtherSurround) { |
| 5752 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5753 | formats.add(AUDIO_FORMAT_DTS); |
| 5754 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5755 | supportsOtherSurround = true; |
| 5756 | } |
| 5757 | |
| 5758 | // Add support for IEC61937 if any raw surround supported. |
| 5759 | // The HAL could do this but add it here, just in case. |
| 5760 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5761 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5762 | supportsIEC61937 = true; |
| 5763 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5764 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | // Modify the list of channel masks supported. |
| 5768 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5769 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5770 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5771 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5772 | |
| 5773 | // If NEVER, then remove support for channelMasks > stereo. |
| 5774 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5775 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5776 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5777 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5778 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5779 | channelMasks.removeAt(maskIndex); |
| 5780 | } else { |
| 5781 | maskIndex++; |
| 5782 | } |
| 5783 | } |
| 5784 | // If ALWAYS, then make sure we at least support 5.1 |
| 5785 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5786 | bool supports5dot1 = false; |
| 5787 | // Are there any channel masks that can be considered "surround"? |
| 5788 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5789 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5790 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5791 | supports5dot1 = true; |
| 5792 | break; |
| 5793 | } |
| 5794 | } |
| 5795 | // If not then add 5.1 support. |
| 5796 | if (!supports5dot1) { |
| 5797 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5798 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5799 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5800 | } |
| 5801 | } |
| 5802 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5803 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5804 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5805 | AudioProfileVector &profiles) |
| 5806 | { |
| 5807 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5808 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5809 | // Format MUST be checked first to update the list of AudioProfile |
| 5810 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5811 | reply = mpClientInterface->getParameters( |
| 5812 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5813 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5814 | AudioParameter repliedParameters(reply); |
| 5815 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5816 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5817 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5818 | return; |
| 5819 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5820 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5821 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5822 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5823 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5824 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5825 | } |
| 5826 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5827 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5828 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5829 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5830 | ChannelsVector channelMasks; |
| 5831 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5832 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5833 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5834 | |
| 5835 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5836 | reply = mpClientInterface->getParameters( |
| 5837 | ioHandle, |
| 5838 | requestedParameters.toString() + ";" + |
| 5839 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5840 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5841 | AudioParameter repliedParameters(reply); |
| 5842 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5843 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5844 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5845 | } |
| 5846 | } |
| 5847 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5848 | reply = mpClientInterface->getParameters(ioHandle, |
| 5849 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5850 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5851 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5852 | AudioParameter repliedParameters(reply); |
| 5853 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5854 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5855 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5856 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5857 | filterSurroundChannelMasks(&channelMasks); |
| 5858 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5859 | } |
| 5860 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5861 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5862 | } |
| 5863 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5864 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5865 | }; // namespace android |