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 | |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE "/system/etc/audio_policy_configuration.xml" |
| 28 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 31 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 32 | #include <AudioPolicyManagerInterface.h> |
| 33 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 34 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 35 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 36 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 37 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 38 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 39 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 40 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 41 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 42 | #ifndef USE_XML_AUDIO_POLICY_CONF |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 43 | #include <ConfigParsingUtils.h> |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #include <StreamDescriptor.h> |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 45 | #endif |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 47 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 48 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 49 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 50 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 52 | //FIXME: workaround for truncated touch sounds |
| 53 | // to be removed when the problem is handled by system UI |
| 54 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 55 | // ---------------------------------------------------------------------------- |
| 56 | // AudioPolicyInterface implementation |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 59 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 60 | audio_policy_dev_state_t state, |
| 61 | const char *device_address, |
| 62 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 63 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 64 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 65 | } |
| 66 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 67 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 68 | audio_policy_dev_state_t state, |
| 69 | const String8 &device_address) |
| 70 | { |
| 71 | AudioParameter param(device_address); |
| 72 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 73 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 74 | param.addInt(key, device); |
| 75 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 76 | } |
| 77 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 78 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 79 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 80 | const char *device_address, |
| 81 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 82 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 83 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 84 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 85 | |
| 86 | // connect/disconnect only 1 device at a time |
| 87 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 88 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 89 | sp<DeviceDescriptor> devDesc = |
| 90 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 91 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | // handle output devices |
| 93 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 94 | SortedVector <audio_io_handle_t> outputs; |
| 95 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 96 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 97 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 98 | // save a copy of the opened output descriptors before any output is opened or closed |
| 99 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 100 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 101 | switch (state) |
| 102 | { |
| 103 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 104 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 105 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 106 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 107 | return INVALID_OPERATION; |
| 108 | } |
| 109 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 110 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 111 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 112 | index = mAvailableOutputDevices.add(devDesc); |
| 113 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 114 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 115 | if (module == 0) { |
| 116 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 117 | device); |
| 118 | mAvailableOutputDevices.remove(devDesc); |
| 119 | return INVALID_OPERATION; |
| 120 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 121 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 122 | } else { |
| 123 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 124 | } |
| 125 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 126 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 127 | // parameters on newly connected devices (instead of opening the outputs...) |
| 128 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 129 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 130 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 131 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 132 | |
| 133 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 134 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 135 | return INVALID_OPERATION; |
| 136 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 137 | // Propagate device availability to Engine |
| 138 | mEngine->setDeviceConnectionState(devDesc, state); |
| 139 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 140 | // outputs should never be empty here |
| 141 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 142 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 143 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 144 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 145 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 146 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 147 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 148 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 149 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 150 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 151 | return INVALID_OPERATION; |
| 152 | } |
| 153 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 154 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 155 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 156 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 157 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 158 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 159 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 160 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 162 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 163 | |
| 164 | // Propagate device availability to Engine |
| 165 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 166 | } break; |
| 167 | |
| 168 | default: |
| 169 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 170 | return BAD_VALUE; |
| 171 | } |
| 172 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 173 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 174 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 175 | checkA2dpSuspend(); |
| 176 | checkOutputForAllStrategies(); |
| 177 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 178 | if (!outputs.isEmpty()) { |
| 179 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 180 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 181 | // close unused outputs after device disconnection or direct outputs that have been |
| 182 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 183 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 184 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 185 | (desc->mDirectOpenCount == 0))) { |
| 186 | closeOutput(outputs[i]); |
| 187 | } |
| 188 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 189 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 190 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 194 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 195 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 196 | updateCallRouting(newDevice); |
| 197 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 198 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 199 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 200 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 201 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 202 | // do not force device change on duplicated output because if device is 0, it will |
| 203 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 204 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 205 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 206 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 207 | // always force when disconnecting (a non-duplicated device) |
| 208 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 209 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 210 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 213 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 214 | cleanUpForDevice(devDesc); |
| 215 | } |
| 216 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 217 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 218 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 219 | } // end if is output device |
| 220 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 221 | // handle input devices |
| 222 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 223 | SortedVector <audio_io_handle_t> inputs; |
| 224 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 225 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 226 | switch (state) |
| 227 | { |
| 228 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 229 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 230 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 231 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 232 | return INVALID_OPERATION; |
| 233 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 234 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 235 | if (module == NULL) { |
| 236 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 237 | device); |
| 238 | return INVALID_OPERATION; |
| 239 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 240 | |
| 241 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 242 | // parameters on newly connected devices (instead of opening the inputs...) |
| 243 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 244 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 245 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 246 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 247 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 248 | return INVALID_OPERATION; |
| 249 | } |
| 250 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 251 | index = mAvailableInputDevices.add(devDesc); |
| 252 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 253 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 254 | } else { |
| 255 | return NO_MEMORY; |
| 256 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 257 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 258 | // Propagate device availability to Engine |
| 259 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 260 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 261 | |
| 262 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 263 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 264 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 265 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 266 | return INVALID_OPERATION; |
| 267 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 268 | |
| 269 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 270 | |
| 271 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 272 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 273 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 274 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 275 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 276 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 277 | // Propagate device availability to Engine |
| 278 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 279 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 280 | |
| 281 | default: |
| 282 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 283 | return BAD_VALUE; |
| 284 | } |
| 285 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 286 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 287 | // As the input device list can impact the output device selection, update |
| 288 | // getDeviceForStrategy() cache |
| 289 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 290 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 291 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 292 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 293 | updateCallRouting(newDevice); |
| 294 | } |
| 295 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 296 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 297 | cleanUpForDevice(devDesc); |
| 298 | } |
| 299 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 300 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 301 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 302 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | |
| 304 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 305 | return BAD_VALUE; |
| 306 | } |
| 307 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 308 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 309 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 310 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 311 | sp<DeviceDescriptor> devDesc = |
| 312 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 313 | (strlen(device_address) != 0)/*matchAddress*/); |
| 314 | |
| 315 | if (devDesc == 0) { |
| 316 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 317 | device, device_address); |
| 318 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 319 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 320 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 321 | DeviceVector *deviceVector; |
| 322 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 323 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 324 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 325 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 326 | deviceVector = &mAvailableInputDevices; |
| 327 | } else { |
| 328 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 329 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 330 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 331 | |
| 332 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 333 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 336 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 337 | const char *device_address, |
| 338 | const char *device_name) |
| 339 | { |
| 340 | status_t status; |
| 341 | |
| 342 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 343 | device, device_address, device_name); |
| 344 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 345 | // connect/disconnect only 1 device at a time |
| 346 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 347 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 348 | // Check if the device is currently connected |
| 349 | sp<DeviceDescriptor> devDesc = |
| 350 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 351 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 352 | if (index < 0) { |
| 353 | // Nothing to do: device is not connected |
| 354 | return NO_ERROR; |
| 355 | } |
| 356 | |
| 357 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 358 | // This will force reading again the device configuration |
| 359 | status = setDeviceConnectionState(device, |
| 360 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 361 | device_address, device_name); |
| 362 | if (status != NO_ERROR) { |
| 363 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 364 | status); |
| 365 | return status; |
| 366 | } |
| 367 | |
| 368 | status = setDeviceConnectionState(device, |
| 369 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 370 | device_address, device_name); |
| 371 | if (status != NO_ERROR) { |
| 372 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 373 | status); |
| 374 | return status; |
| 375 | } |
| 376 | |
| 377 | return NO_ERROR; |
| 378 | } |
| 379 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 380 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 381 | { |
| 382 | bool createTxPatch = false; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 383 | status_t status; |
| 384 | audio_patch_handle_t afPatchHandle; |
| 385 | DeviceVector deviceList; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 386 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 387 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 388 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 389 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 390 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 391 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 392 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 393 | |
| 394 | // release existing RX patch if any |
| 395 | if (mCallRxPatch != 0) { |
| 396 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 397 | mCallRxPatch.clear(); |
| 398 | } |
| 399 | // release TX patch if any |
| 400 | if (mCallTxPatch != 0) { |
| 401 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 402 | mCallTxPatch.clear(); |
| 403 | } |
| 404 | |
| 405 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 406 | // via setOutputDevice() on primary output. |
| 407 | // Otherwise, create two audio patches for TX and RX path. |
| 408 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 409 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 410 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 411 | // of it due to legacy implementation. If not, create a patch. |
| 412 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 413 | == AUDIO_DEVICE_NONE) { |
| 414 | createTxPatch = true; |
| 415 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 416 | } else { // create RX path audio patch |
| 417 | struct audio_patch patch; |
| 418 | |
| 419 | patch.num_sources = 1; |
| 420 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 421 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 422 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 423 | "updateCallRouting() selected device not in output device list"); |
| 424 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 425 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 426 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 427 | "updateCallRouting() no telephony RX device"); |
| 428 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 429 | |
| 430 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 431 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 432 | |
| 433 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 434 | SortedVector<audio_io_handle_t> outputs = |
| 435 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 436 | audio_io_handle_t output = selectOutput(outputs, |
| 437 | AUDIO_OUTPUT_FLAG_NONE, |
| 438 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 439 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 440 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 442 | "updateCallRouting() RX device output is duplicated"); |
| 443 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 444 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 445 | patch.num_sources = 2; |
| 446 | } |
| 447 | |
| 448 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 449 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 450 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 451 | status); |
| 452 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 453 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 454 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 455 | mCallRxPatch->mUid = mUidCached; |
| 456 | } |
| 457 | createTxPatch = true; |
| 458 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 459 | if (createTxPatch) { // create TX path audio patch |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 460 | struct audio_patch patch; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 461 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 462 | patch.num_sources = 1; |
| 463 | patch.num_sinks = 1; |
| 464 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 465 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 466 | "updateCallRouting() selected device not in input device list"); |
| 467 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 468 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 469 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 470 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 471 | "updateCallRouting() no telephony TX device"); |
| 472 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 473 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 474 | |
| 475 | SortedVector<audio_io_handle_t> outputs = |
| 476 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 477 | audio_io_handle_t output = selectOutput(outputs, |
| 478 | AUDIO_OUTPUT_FLAG_NONE, |
| 479 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 480 | // request to reuse existing output stream if one is already opened to reach the TX |
| 481 | // path output device |
| 482 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 483 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 484 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 485 | "updateCallRouting() RX device output is duplicated"); |
| 486 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 487 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 488 | patch.num_sources = 2; |
| 489 | } |
| 490 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 491 | // terminate active capture if on the same HW module as the call TX source device |
| 492 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 493 | // call TX device but this information is not in the audio patch and logic here must be |
| 494 | // symmetric to the one in startInput() |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 495 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 496 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 497 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 498 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 499 | AudioSessionCollection activeSessions = |
| 500 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 501 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 502 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 503 | stopInput(activeDesc->mIoHandle, activeSession); |
| 504 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 505 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 509 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 510 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 511 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 512 | status); |
| 513 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 514 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 515 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 516 | mCallTxPatch->mUid = mUidCached; |
| 517 | } |
| 518 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 519 | |
| 520 | return muteWaitMs; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 523 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 524 | { |
| 525 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 526 | // store previous phone state for management of sonification strategy below |
| 527 | int oldState = mEngine->getPhoneState(); |
| 528 | |
| 529 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 530 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | return; |
| 532 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 533 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 534 | // if leaving call state, handle special case of active streams |
| 535 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 536 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 537 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 538 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 539 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 540 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 541 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 542 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 543 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 544 | } |
| 545 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 546 | /** |
| 547 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 548 | * routing command. |
| 549 | */ |
| 550 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 551 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 552 | |
| 553 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 | checkA2dpSuspend(); |
| 555 | checkOutputForAllStrategies(); |
| 556 | updateDevicesAndOutputs(); |
| 557 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 558 | int delayMs = 0; |
| 559 | if (isStateInCall(state)) { |
| 560 | nsecs_t sysTime = systemTime(); |
| 561 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 562 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | // mute media and sonification strategies and delay device switch by the largest |
| 564 | // latency of any output where either strategy is active. |
| 565 | // 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] | 566 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 567 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 568 | sysTime) || |
| 569 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 570 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 571 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 572 | (delayMs < (int)desc->latency()*2)) { |
| 573 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 574 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 575 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 576 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 577 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 578 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 579 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 580 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 581 | } |
| 582 | } |
| 583 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 584 | if (hasPrimaryOutput()) { |
| 585 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 586 | // the device returned is not necessarily reachable via this output |
| 587 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 588 | // force routing command to audio hardware when ending call |
| 589 | // even if no device change is needed |
| 590 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 591 | rxDevice = mPrimaryOutput->device(); |
| 592 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 593 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 594 | if (state == AUDIO_MODE_IN_CALL) { |
| 595 | updateCallRouting(rxDevice, delayMs); |
| 596 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 597 | if (mCallRxPatch != 0) { |
| 598 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 599 | mCallRxPatch.clear(); |
| 600 | } |
| 601 | if (mCallTxPatch != 0) { |
| 602 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 603 | mCallTxPatch.clear(); |
| 604 | } |
| 605 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 606 | } else { |
| 607 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 608 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 609 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 610 | // if entering in call state, handle special case of active streams |
| 611 | // pertaining to sonification strategy see handleIncallSonification() |
| 612 | if (isStateInCall(state)) { |
| 613 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 614 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 615 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 617 | |
| 618 | // force reevaluating accessibility routing when call starts |
| 619 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | // 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] | 623 | if (state == AUDIO_MODE_RINGTONE && |
| 624 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 625 | mLimitRingtoneVolume = true; |
| 626 | } else { |
| 627 | mLimitRingtoneVolume = false; |
| 628 | } |
| 629 | } |
| 630 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 631 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 632 | return mEngine->getPhoneState(); |
| 633 | } |
| 634 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 635 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 636 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 637 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 638 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
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 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 641 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 642 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 643 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 644 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 645 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 646 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 647 | |
| 648 | // check for device and output changes triggered by new force usage |
| 649 | checkA2dpSuspend(); |
| 650 | checkOutputForAllStrategies(); |
| 651 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 652 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 653 | //FIXME: workaround for truncated touch sounds |
| 654 | // to be removed when the problem is handled by system UI |
| 655 | uint32_t delayMs = 0; |
| 656 | uint32_t waitMs = 0; |
| 657 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 658 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 659 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 660 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 661 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 662 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 663 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 664 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 665 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 666 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 667 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 668 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 669 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 670 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 671 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 672 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 676 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 677 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 678 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 679 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 680 | // Force new input selection if the new device can not be reached via current input |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 681 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 682 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 683 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 684 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 685 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 686 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 687 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 690 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 691 | { |
| 692 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 693 | } |
| 694 | |
| 695 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 696 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 697 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 698 | audio_devices_t device, |
| 699 | uint32_t samplingRate, |
| 700 | audio_format_t format, |
| 701 | audio_channel_mask_t channelMask, |
| 702 | audio_output_flags_t flags) |
| 703 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 704 | // only retain flags that will drive the direct output profile selection |
| 705 | // if explicitly requested |
| 706 | static const uint32_t kRelevantFlags = |
| 707 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 708 | flags = |
| 709 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 710 | |
| 711 | sp<IOProfile> profile; |
| 712 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 713 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 714 | if (mHwModules[i]->mHandle == 0) { |
| 715 | continue; |
| 716 | } |
| 717 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 718 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 719 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 720 | samplingRate, NULL /*updatedSamplingRate*/, |
| 721 | format, NULL /*updatedFormat*/, |
| 722 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 723 | flags)) { |
| 724 | continue; |
| 725 | } |
| 726 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 727 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 728 | continue; |
| 729 | } |
| 730 | // 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] | 731 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 732 | continue; |
| 733 | } |
| 734 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 735 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 736 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 737 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 738 | } |
| 739 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 740 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 743 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 744 | uint32_t samplingRate, |
| 745 | audio_format_t format, |
| 746 | audio_channel_mask_t channelMask, |
| 747 | audio_output_flags_t flags, |
| 748 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 749 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 750 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 751 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 752 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 753 | device, stream, samplingRate, format, channelMask, flags); |
| 754 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 755 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 756 | stream, samplingRate,format, channelMask, |
| 757 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 760 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 761 | audio_io_handle_t *output, |
| 762 | audio_session_t session, |
| 763 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 764 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 765 | const audio_config_t *config, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 766 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 767 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 768 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 769 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 770 | audio_attributes_t attributes; |
| 771 | if (attr != NULL) { |
| 772 | if (!isValidAttributes(attr)) { |
| 773 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 774 | attr->usage, attr->content_type, attr->flags, |
| 775 | attr->tags); |
| 776 | return BAD_VALUE; |
| 777 | } |
| 778 | attributes = *attr; |
| 779 | } else { |
| 780 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 781 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 782 | return BAD_VALUE; |
| 783 | } |
| 784 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 785 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 786 | |
| 787 | // TODO: check for existing client for this port ID |
| 788 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 789 | *portId = AudioPort::getNextUniqueId(); |
| 790 | } |
| 791 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 792 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 793 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 794 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 795 | if (!audio_has_proportional_frames(config->format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 796 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 797 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 798 | *stream = streamTypefromAttributesInt(&attributes); |
| 799 | *output = desc->mIoHandle; |
| 800 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 801 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 802 | } |
| 803 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 804 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 805 | return BAD_VALUE; |
| 806 | } |
| 807 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 808 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 809 | " session %d selectedDeviceId %d", |
| 810 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 811 | session, selectedDeviceId); |
| 812 | |
| 813 | *stream = streamTypefromAttributesInt(&attributes); |
| 814 | |
| 815 | // Explicit routing? |
| 816 | sp<DeviceDescriptor> deviceDesc; |
| 817 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 818 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 819 | deviceDesc = mAvailableOutputDevices[i]; |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 824 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 825 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 826 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 827 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 828 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 829 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 830 | } |
| 831 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 832 | 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] | 833 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 834 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 835 | *output = getOutputForDevice(device, session, *stream, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 836 | config->sample_rate, config->format, config->channel_mask, |
| 837 | flags, &config->offload_info); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 838 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 839 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 840 | return INVALID_OPERATION; |
| 841 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 842 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 843 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 847 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 848 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 849 | audio_stream_type_t stream, |
| 850 | uint32_t samplingRate, |
| 851 | audio_format_t format, |
| 852 | audio_channel_mask_t channelMask, |
| 853 | audio_output_flags_t flags, |
| 854 | const audio_offload_info_t *offloadInfo) |
| 855 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 856 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 857 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 858 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 859 | #ifdef AUDIO_POLICY_TEST |
| 860 | if (mCurOutput != 0) { |
| 861 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 862 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 863 | |
| 864 | if (mTestOutputs[mCurOutput] == 0) { |
| 865 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 866 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 867 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 868 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 869 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 870 | outputDesc->mFlags = |
| 871 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 872 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 873 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 874 | config.sample_rate = mTestSamplingRate; |
| 875 | config.channel_mask = mTestChannels; |
| 876 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 877 | if (offloadInfo != NULL) { |
| 878 | config.offload_info = *offloadInfo; |
| 879 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 880 | status = mpClientInterface->openOutput(0, |
| 881 | &mTestOutputs[mCurOutput], |
| 882 | &config, |
| 883 | &outputDesc->mDevice, |
| 884 | String8(""), |
| 885 | &outputDesc->mLatency, |
| 886 | outputDesc->mFlags); |
| 887 | if (status == NO_ERROR) { |
| 888 | outputDesc->mSamplingRate = config.sample_rate; |
| 889 | outputDesc->mFormat = config.format; |
| 890 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 891 | AudioParameter outputCmd = AudioParameter(); |
| 892 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 893 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 894 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 895 | } |
| 896 | } |
| 897 | return mTestOutputs[mCurOutput]; |
| 898 | } |
| 899 | #endif //AUDIO_POLICY_TEST |
| 900 | |
| 901 | // open a direct output if required by specified parameters |
| 902 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 903 | // and all common behaviors are driven by checking only the direct flag |
| 904 | // this should normally be set appropriately in the policy configuration file |
| 905 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 906 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 907 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 908 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 909 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 910 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 911 | // only allow deep buffering for music stream type |
| 912 | if (stream != AUDIO_STREAM_MUSIC) { |
| 913 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 914 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 915 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 916 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 917 | // use DEEP_BUFFER as default output for music stream type |
| 918 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 919 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 920 | if (stream == AUDIO_STREAM_TTS) { |
| 921 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 922 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 923 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 924 | sp<IOProfile> profile; |
| 925 | |
| 926 | // 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] | 927 | // and not explicitly requested |
| 928 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 929 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 930 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 931 | goto non_direct_output; |
| 932 | } |
| 933 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 934 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 935 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 936 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 937 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 938 | // This may prevent offloading in rare situations where effects are left active by apps |
| 939 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 940 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 941 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 942 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | profile = getProfileForDirectOutput(device, |
| 944 | samplingRate, |
| 945 | format, |
| 946 | channelMask, |
| 947 | (audio_output_flags_t)flags); |
| 948 | } |
| 949 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 950 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 951 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 952 | |
| 953 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 954 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 956 | outputDesc = desc; |
| 957 | // reuse direct output if currently open and configured with same parameters |
| 958 | if ((samplingRate == outputDesc->mSamplingRate) && |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 959 | audio_formats_match(format, outputDesc->mFormat) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 960 | (channelMask == outputDesc->mChannelMask)) { |
| 961 | outputDesc->mDirectOpenCount++; |
| 962 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 963 | return mOutputs.keyAt(i); |
| 964 | } |
| 965 | } |
| 966 | } |
| 967 | // close direct output if currently open and configured with different parameters |
| 968 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 969 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 971 | |
| 972 | // if the selected profile is offloaded and no offload info was specified, |
| 973 | // create a default one |
| 974 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 975 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 976 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 977 | defaultOffloadInfo.sample_rate = samplingRate; |
| 978 | defaultOffloadInfo.channel_mask = channelMask; |
| 979 | defaultOffloadInfo.format = format; |
| 980 | defaultOffloadInfo.stream_type = stream; |
| 981 | defaultOffloadInfo.bit_rate = 0; |
| 982 | defaultOffloadInfo.duration_us = -1; |
| 983 | defaultOffloadInfo.has_video = true; // conservative |
| 984 | defaultOffloadInfo.is_streaming = true; // likely |
| 985 | offloadInfo = &defaultOffloadInfo; |
| 986 | } |
| 987 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 988 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 990 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 991 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 992 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 993 | config.sample_rate = samplingRate; |
| 994 | config.channel_mask = channelMask; |
| 995 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 996 | if (offloadInfo != NULL) { |
| 997 | config.offload_info = *offloadInfo; |
| 998 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 999 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1000 | &output, |
| 1001 | &config, |
| 1002 | &outputDesc->mDevice, |
| 1003 | String8(""), |
| 1004 | &outputDesc->mLatency, |
| 1005 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1006 | |
| 1007 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1008 | if (status != NO_ERROR || |
| 1009 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1010 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1011 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1012 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 1013 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1014 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1015 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1016 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | mpClientInterface->closeOutput(output); |
| 1018 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1019 | // 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] | 1020 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1021 | goto non_direct_output; |
| 1022 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1023 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1024 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1025 | outputDesc->mSamplingRate = config.sample_rate; |
| 1026 | outputDesc->mChannelMask = config.channel_mask; |
| 1027 | outputDesc->mFormat = config.format; |
| 1028 | outputDesc->mRefCount[stream] = 0; |
| 1029 | outputDesc->mStopTime[stream] = 0; |
| 1030 | outputDesc->mDirectOpenCount = 1; |
| 1031 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1032 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 1033 | addOutput(output, outputDesc); |
| 1034 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1035 | if (dstOutput == output) { |
| 1036 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 1037 | } |
| 1038 | mPreviousOutputs = mOutputs; |
| 1039 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1040 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | return output; |
| 1042 | } |
| 1043 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1044 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1045 | |
| 1046 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1047 | // stamps are embedded in audio data |
| 1048 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1049 | return AUDIO_IO_HANDLE_NONE; |
| 1050 | } |
| 1051 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1052 | // ignoring channel mask due to downmix capability in mixer |
| 1053 | |
| 1054 | // open a non direct output |
| 1055 | |
| 1056 | // for non direct outputs, only PCM is supported |
| 1057 | if (audio_is_linear_pcm(format)) { |
| 1058 | // get which output is suitable for the specified stream. The actual |
| 1059 | // routing change will happen when startOutput() will be called |
| 1060 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1061 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1062 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1063 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1064 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1065 | } |
| 1066 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1067 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1068 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1069 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1070 | |
| 1071 | return output; |
| 1072 | } |
| 1073 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1074 | 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] | 1075 | audio_output_flags_t flags, |
| 1076 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1077 | { |
| 1078 | // select one output among several that provide a path to a particular device or set of |
| 1079 | // devices (the list was previously build by getOutputsForDevice()). |
| 1080 | // The priority is as follows: |
| 1081 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1082 | // 2: the output with the bit depth the closest to the requested one |
| 1083 | // 3: the primary output |
| 1084 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1085 | |
| 1086 | if (outputs.size() == 0) { |
| 1087 | return 0; |
| 1088 | } |
| 1089 | if (outputs.size() == 1) { |
| 1090 | return outputs[0]; |
| 1091 | } |
| 1092 | |
| 1093 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1094 | audio_io_handle_t outputForFlags = 0; |
| 1095 | audio_io_handle_t outputForPrimary = 0; |
| 1096 | audio_io_handle_t outputForFormat = 0; |
| 1097 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1098 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1099 | |
| 1100 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1101 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1102 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1103 | // if a valid format is specified, skip output if not compatible |
| 1104 | if (format != AUDIO_FORMAT_INVALID) { |
| 1105 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1106 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1107 | continue; |
| 1108 | } |
| 1109 | } else if (!audio_is_linear_pcm(format)) { |
| 1110 | continue; |
| 1111 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1112 | if (AudioPort::isBetterFormatMatch( |
| 1113 | outputDesc->mFormat, bestFormat, format)) { |
| 1114 | outputForFormat = outputs[i]; |
| 1115 | bestFormat = outputDesc->mFormat; |
| 1116 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1117 | } |
| 1118 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1119 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1120 | if (commonFlags >= maxCommonFlags) { |
| 1121 | if (commonFlags == maxCommonFlags) { |
| 1122 | if (AudioPort::isBetterFormatMatch( |
| 1123 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1124 | outputForFlags = outputs[i]; |
| 1125 | bestFormatForFlags = outputDesc->mFormat; |
| 1126 | } |
| 1127 | } else { |
| 1128 | outputForFlags = outputs[i]; |
| 1129 | maxCommonFlags = commonFlags; |
| 1130 | bestFormatForFlags = outputDesc->mFormat; |
| 1131 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1132 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1133 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1134 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1135 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1140 | if (outputForFlags != 0) { |
| 1141 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1142 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1143 | if (outputForFormat != 0) { |
| 1144 | return outputForFormat; |
| 1145 | } |
| 1146 | if (outputForPrimary != 0) { |
| 1147 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | return outputs[0]; |
| 1151 | } |
| 1152 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1153 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1154 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1155 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1156 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1157 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1158 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1159 | ssize_t index = mOutputs.indexOfKey(output); |
| 1160 | if (index < 0) { |
| 1161 | ALOGW("startOutput() unknown output %d", output); |
| 1162 | return BAD_VALUE; |
| 1163 | } |
| 1164 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1165 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1166 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1167 | // Routing? |
| 1168 | mOutputRoutes.incRouteActivity(session); |
| 1169 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1170 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1171 | AudioMix *policyMix = NULL; |
| 1172 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1173 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1174 | policyMix = outputDesc->mPolicyMix; |
| 1175 | address = policyMix->mDeviceAddress.string(); |
| 1176 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1177 | newDevice = policyMix->mDeviceType; |
| 1178 | } else { |
| 1179 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1180 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1181 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1182 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1183 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1184 | } else { |
| 1185 | newDevice = AUDIO_DEVICE_NONE; |
| 1186 | } |
| 1187 | |
| 1188 | uint32_t delayMs = 0; |
| 1189 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1190 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1191 | |
| 1192 | if (status != NO_ERROR) { |
| 1193 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1194 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1195 | } |
| 1196 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1197 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1198 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1199 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1200 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1201 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1202 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1203 | "remote-submix"); |
| 1204 | } |
| 1205 | |
| 1206 | if (delayMs != 0) { |
| 1207 | usleep(delayMs * 1000); |
| 1208 | } |
| 1209 | |
| 1210 | return status; |
| 1211 | } |
| 1212 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1213 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1214 | audio_stream_type_t stream, |
| 1215 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1216 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1217 | uint32_t *delayMs) |
| 1218 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1219 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1220 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1221 | |
| 1222 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1223 | if (stream == AUDIO_STREAM_TTS) { |
| 1224 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1225 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1226 | return INVALID_OPERATION; |
| 1227 | } else { |
| 1228 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1229 | } |
| 1230 | } else { |
| 1231 | // some playback other than beacon starts |
| 1232 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1233 | } |
| 1234 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1235 | // 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] | 1236 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1237 | bool force = !outputDesc->isActive() && |
| 1238 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1239 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1240 | // increment usage count for this stream on the requested output: |
| 1241 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1242 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1243 | outputDesc->changeRefCount(stream, 1); |
| 1244 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1245 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1246 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1247 | if (device == AUDIO_DEVICE_NONE) { |
| 1248 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1249 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1250 | routing_strategy strategy = getStrategy(stream); |
| 1251 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1252 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1253 | (beaconMuteLatency > 0); |
| 1254 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1255 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1256 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1257 | if (desc != outputDesc) { |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1258 | // force a device change if any other output is: |
| 1259 | // - managed by the same hw module |
| 1260 | // - has a current device selection that differs from selected device. |
| 1261 | // - supports currently selected device |
| 1262 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1263 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1264 | // change the device currently selected by the other active output. |
| 1265 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1266 | desc->device() != device && |
| 1267 | desc->supportedDevices() & device && |
| 1268 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1269 | force = true; |
| 1270 | } |
| 1271 | // 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] | 1272 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1273 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1274 | uint32_t latency = desc->latency(); |
| 1275 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1276 | waitMs = latency; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1280 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1281 | |
| 1282 | // handle special case for sonification while in call |
| 1283 | if (isInCall()) { |
| 1284 | handleIncallSonification(stream, true, false); |
| 1285 | } |
| 1286 | |
| 1287 | // apply volume rules for current stream and device if necessary |
| 1288 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1289 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1290 | outputDesc, |
| 1291 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1292 | |
| 1293 | // update the outputs if starting an output with a stream that can affect notification |
| 1294 | // routing |
| 1295 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1296 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1297 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1298 | if (strategy == STRATEGY_SONIFICATION) { |
| 1299 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1300 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1301 | |
| 1302 | if (waitMs > muteWaitMs) { |
| 1303 | *delayMs = waitMs - muteWaitMs; |
| 1304 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1305 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1306 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1307 | return NO_ERROR; |
| 1308 | } |
| 1309 | |
| 1310 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1311 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1312 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1313 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1314 | { |
| 1315 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1316 | ssize_t index = mOutputs.indexOfKey(output); |
| 1317 | if (index < 0) { |
| 1318 | ALOGW("stopOutput() unknown output %d", output); |
| 1319 | return BAD_VALUE; |
| 1320 | } |
| 1321 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1322 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1323 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1324 | if (outputDesc->mRefCount[stream] == 1) { |
| 1325 | // Automatically disable the remote submix input when output is stopped on a |
| 1326 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1327 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1328 | outputDesc->mPolicyMix != NULL && |
| 1329 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1330 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1331 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1332 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1333 | "remote-submix"); |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1338 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1339 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1340 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1341 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1342 | |
| 1343 | if (forceDeviceUpdate) { |
| 1344 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1345 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1346 | } |
| 1347 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1348 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1349 | } |
| 1350 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1351 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1352 | audio_stream_type_t stream, |
| 1353 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1354 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1355 | // always handle stream stop, check which stream type is stopping |
| 1356 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1357 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1358 | // handle special case for sonification while in call |
| 1359 | if (isInCall()) { |
| 1360 | handleIncallSonification(stream, false, false); |
| 1361 | } |
| 1362 | |
| 1363 | if (outputDesc->mRefCount[stream] > 0) { |
| 1364 | // decrement usage count of this stream on the output |
| 1365 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1366 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1367 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1368 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1369 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1370 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1371 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1372 | // the track stop() command is received and at that time the audio track buffer can |
| 1373 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1374 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1375 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1376 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1377 | |
| 1378 | // force restoring the device selection on other active outputs if it differs from the |
| 1379 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1380 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1381 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1382 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1383 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1384 | desc->isActive() && |
| 1385 | outputDesc->sharesHwModuleWith(desc) && |
| 1386 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1387 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1388 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1389 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1390 | newDevice2, |
| 1391 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1392 | delayMs); |
| 1393 | // re-apply device specific volume if not done by setOutputDevice() |
| 1394 | if (!force) { |
| 1395 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1396 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1397 | } |
| 1398 | } |
| 1399 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1400 | handleNotificationRoutingForStream(stream); |
| 1401 | } |
| 1402 | return NO_ERROR; |
| 1403 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1404 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1405 | return INVALID_OPERATION; |
| 1406 | } |
| 1407 | } |
| 1408 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1409 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1410 | audio_stream_type_t stream __unused, |
| 1411 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1412 | { |
| 1413 | ALOGV("releaseOutput() %d", output); |
| 1414 | ssize_t index = mOutputs.indexOfKey(output); |
| 1415 | if (index < 0) { |
| 1416 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1417 | return; |
| 1418 | } |
| 1419 | |
| 1420 | #ifdef AUDIO_POLICY_TEST |
| 1421 | int testIndex = testOutputIndex(output); |
| 1422 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1423 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1424 | if (outputDesc->isActive()) { |
| 1425 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1426 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1427 | mTestOutputs[testIndex] = 0; |
| 1428 | } |
| 1429 | return; |
| 1430 | } |
| 1431 | #endif //AUDIO_POLICY_TEST |
| 1432 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1433 | // Routing |
| 1434 | mOutputRoutes.removeRoute(session); |
| 1435 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1436 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1437 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1438 | if (desc->mDirectOpenCount <= 0) { |
| 1439 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1440 | desc->mDirectOpenCount, output); |
| 1441 | return; |
| 1442 | } |
| 1443 | if (--desc->mDirectOpenCount == 0) { |
| 1444 | closeOutput(output); |
| 1445 | // If effects where present on the output, audioflinger moved them to the primary |
| 1446 | // output by default: move them back to the appropriate output. |
| 1447 | audio_io_handle_t dstOutput = getOutputForEffect(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 1448 | if (hasPrimaryOutput() && dstOutput != mPrimaryOutput->mIoHandle) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1449 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, |
| 1450 | mPrimaryOutput->mIoHandle, dstOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1451 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1452 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1458 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1459 | audio_io_handle_t *input, |
| 1460 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1461 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1462 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1463 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1464 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1465 | input_type_t *inputType, |
| 1466 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1467 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1468 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1469 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1470 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1471 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1472 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1473 | *inputType = API_INPUT_INVALID; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1474 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1475 | audio_devices_t device; |
| 1476 | // handle legacy remote submix case where the address was not always specified |
| 1477 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1478 | audio_source_t inputSource = attr->source; |
| 1479 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1480 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1481 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1482 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1483 | inputSource = AUDIO_SOURCE_MIC; |
| 1484 | } |
| 1485 | halInputSource = inputSource; |
| 1486 | |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1487 | // TODO: check for existing client for this port ID |
| 1488 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1489 | *portId = AudioPort::getNextUniqueId(); |
| 1490 | } |
| 1491 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1492 | // Explicit routing? |
| 1493 | sp<DeviceDescriptor> deviceDesc; |
| 1494 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1495 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1496 | deviceDesc = mAvailableInputDevices[i]; |
| 1497 | break; |
| 1498 | } |
| 1499 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1500 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1501 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1502 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1503 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1504 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1505 | if (ret != NO_ERROR) { |
| 1506 | return ret; |
| 1507 | } |
| 1508 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1509 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1510 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1511 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1512 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1513 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1514 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1515 | return BAD_VALUE; |
| 1516 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1517 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1518 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1519 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1520 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1521 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1522 | // know about it and is therefore considered "legacy" |
| 1523 | *inputType = API_INPUT_LEGACY; |
| 1524 | } else { |
| 1525 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1526 | // an external policy |
| 1527 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1528 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1529 | } else if (audio_is_remote_submix_device(device)) { |
| 1530 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1531 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1532 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1533 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1534 | } else { |
| 1535 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1536 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1537 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
| 1540 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1541 | config->sample_rate, config->format, config->channel_mask, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1542 | policyMix); |
| 1543 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1544 | mInputRoutes.removeRoute(session); |
| 1545 | return INVALID_OPERATION; |
| 1546 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1547 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1548 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1549 | return NO_ERROR; |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1554 | String8 address, |
| 1555 | audio_session_t session, |
| 1556 | uid_t uid, |
| 1557 | audio_source_t inputSource, |
| 1558 | uint32_t samplingRate, |
| 1559 | audio_format_t format, |
| 1560 | audio_channel_mask_t channelMask, |
| 1561 | audio_input_flags_t flags, |
| 1562 | AudioMix *policyMix) |
| 1563 | { |
| 1564 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1565 | audio_source_t halInputSource = inputSource; |
| 1566 | bool isSoundTrigger = false; |
| 1567 | |
| 1568 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1569 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1570 | if (index >= 0) { |
| 1571 | input = mSoundTriggerSessions.valueFor(session); |
| 1572 | isSoundTrigger = true; |
| 1573 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1574 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1575 | } else { |
| 1576 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1580 | // find a compatible input profile (not necessarily identical in parameters) |
| 1581 | sp<IOProfile> profile; |
| 1582 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1583 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1584 | audio_format_t profileFormat = format; |
| 1585 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1586 | audio_input_flags_t profileFlags = flags; |
| 1587 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1588 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1589 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1590 | profileFlags); |
| 1591 | if (profile != 0) { |
| 1592 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1593 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1594 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1595 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1596 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1597 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1598 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1599 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1600 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1601 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1602 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1603 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1604 | // Pick input sampling rate if not specified by client |
| 1605 | if (samplingRate == 0) { |
| 1606 | samplingRate = profileSamplingRate; |
| 1607 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1608 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1609 | if (profile->getModuleHandle() == 0) { |
| 1610 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1611 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1614 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1615 | inputSource, |
| 1616 | format, |
| 1617 | samplingRate, |
| 1618 | channelMask, |
| 1619 | flags, |
| 1620 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1621 | isSoundTrigger, |
| 1622 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1623 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1624 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1625 | // reuse an open input if possible |
| 1626 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1627 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1628 | // reuse input if: |
| 1629 | // - it shares the same profile |
| 1630 | // AND |
| 1631 | // - it is not a reroute submix input |
| 1632 | // AND |
| 1633 | // - it is: not used for sound trigger |
| 1634 | // OR |
| 1635 | // used for sound trigger and all clients use the same session ID |
| 1636 | // |
| 1637 | if ((profile == desc->mProfile) && |
| 1638 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1639 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1640 | |
| 1641 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1642 | if (as != 0) { |
| 1643 | // do not allow unmatching properties on same session |
| 1644 | if (as->matches(audioSession)) { |
| 1645 | as->changeOpenCount(1); |
| 1646 | } else { |
| 1647 | ALOGW("getInputForDevice() record with different attributes" |
| 1648 | " exists for session %d", session); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1649 | break; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1650 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1651 | } else if (isSoundTrigger) { |
| 1652 | break; |
| 1653 | } |
| 1654 | // force close input if current source is now the highest priority request on this input |
| 1655 | // and current input properties are not exactly as requested. |
| 1656 | if ((desc->mSamplingRate != samplingRate || |
| 1657 | desc->mChannelMask != channelMask || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1658 | !audio_formats_match(desc->mFormat, format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1659 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
| 1660 | source_priority(inputSource))) { |
| 1661 | ALOGV("%s: ", __FUNCTION__); |
| 1662 | AudioSessionCollection sessions = desc->getAudioSessions(false /*activeOnly*/); |
| 1663 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1664 | audio_session_t currentSession = sessions.keyAt(j); |
| 1665 | stopInput(desc->mIoHandle, currentSession); |
| 1666 | releaseInput(desc->mIoHandle, currentSession); |
| 1667 | } |
| 1668 | break; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1669 | } else { |
| 1670 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1671 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1672 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1673 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1674 | } |
| 1675 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1676 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1677 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1678 | config.sample_rate = profileSamplingRate; |
| 1679 | config.channel_mask = profileChannelMask; |
| 1680 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1681 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1682 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1683 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1684 | &config, |
| 1685 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1686 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1687 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1688 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1689 | |
| 1690 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1691 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1692 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1693 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1694 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1695 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1696 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1697 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1698 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1699 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1700 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1701 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1702 | } |
| 1703 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1704 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1705 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1706 | inputDesc->mFormat = profileFormat; |
| 1707 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1708 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1709 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1710 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1711 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1712 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1713 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1714 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1715 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1718 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1719 | const sp<AudioSession>& audioSession) |
| 1720 | { |
| 1721 | // Do not allow capture if an active voice call is using a software patch and |
| 1722 | // the call TX source device is on the same HW module. |
| 1723 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1724 | // call TX device but this information is not in the audio patch |
| 1725 | if (mCallTxPatch != 0 && |
| 1726 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1727 | return false; |
| 1728 | } |
| 1729 | |
| 1730 | // starting concurrent capture is enabled if: |
| 1731 | // 1) capturing for re-routing |
| 1732 | // 2) capturing for HOTWORD source |
| 1733 | // 3) capturing for FM TUNER source |
| 1734 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1735 | |
| 1736 | if (is_virtual_input_device(inputDesc->mDevice) || |
| 1737 | audioSession->inputSource() == AUDIO_SOURCE_HOTWORD || |
| 1738 | audioSession->inputSource() == AUDIO_SOURCE_FM_TUNER) { |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1743 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1744 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; |
| 1745 | if ((activeInput->inputSource() != AUDIO_SOURCE_HOTWORD) && |
| 1746 | (activeInput->inputSource() != AUDIO_SOURCE_FM_TUNER) && |
| 1747 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1748 | return false; |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1756 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1757 | audio_session_t session, |
| 1758 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1759 | { |
| 1760 | ALOGV("startInput() input %d", input); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1761 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1762 | ssize_t index = mInputs.indexOfKey(input); |
| 1763 | if (index < 0) { |
| 1764 | ALOGW("startInput() unknown input %d", input); |
| 1765 | return BAD_VALUE; |
| 1766 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1767 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1768 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1769 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1770 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1771 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1772 | return BAD_VALUE; |
| 1773 | } |
| 1774 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1775 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1776 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1777 | return INVALID_OPERATION; |
| 1778 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1779 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1780 | if (isInCall()) { |
| 1781 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1782 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1783 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1784 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1787 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1788 | // are considered for device selection |
| 1789 | audioSession->changeActiveCount(1); |
| 1790 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1791 | // Routing? |
| 1792 | mInputRoutes.incRouteActivity(session); |
| 1793 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1794 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1795 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 1796 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1797 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1798 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1799 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1800 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
| 1801 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1802 | if ((inputDesc->mPolicyMix != NULL) |
| 1803 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1804 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1805 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1806 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1807 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1808 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1809 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 1810 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1811 | SoundTrigger::setCaptureState(true); |
| 1812 | } |
| 1813 | |
| 1814 | // automatically enable the remote submix output when input is started if not |
| 1815 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1816 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1817 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1818 | String8 address = String8(""); |
| 1819 | if (inputDesc->mPolicyMix == NULL) { |
| 1820 | address = String8("0"); |
| 1821 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1822 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1823 | } |
| 1824 | if (address != "") { |
| 1825 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1826 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1827 | address, "remote-submix"); |
| 1828 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1829 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1830 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1833 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1834 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1835 | return NO_ERROR; |
| 1836 | } |
| 1837 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1838 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1839 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1840 | { |
| 1841 | ALOGV("stopInput() input %d", input); |
| 1842 | ssize_t index = mInputs.indexOfKey(input); |
| 1843 | if (index < 0) { |
| 1844 | ALOGW("stopInput() unknown input %d", input); |
| 1845 | return BAD_VALUE; |
| 1846 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1847 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1848 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1849 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1850 | if (index < 0) { |
| 1851 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1852 | return BAD_VALUE; |
| 1853 | } |
| 1854 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1855 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1856 | ALOGW("stopInput() input %d already stopped", input); |
| 1857 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1860 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1861 | |
| 1862 | // Routing? |
| 1863 | mInputRoutes.decRouteActivity(session); |
| 1864 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1865 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1866 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1867 | if (inputDesc->isActive()) { |
| 1868 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 1869 | } else { |
| 1870 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1871 | if ((inputDesc->mPolicyMix != NULL) |
| 1872 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1873 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1874 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1875 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1876 | |
| 1877 | // automatically disable the remote submix output when input is stopped if not |
| 1878 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1879 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1880 | String8 address = String8(""); |
| 1881 | if (inputDesc->mPolicyMix == NULL) { |
| 1882 | address = String8("0"); |
| 1883 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1884 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1885 | } |
| 1886 | if (address != "") { |
| 1887 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1888 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1889 | address, "remote-submix"); |
| 1890 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1891 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1892 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1893 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1894 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1895 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1896 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 1897 | // primary HW module |
| 1898 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1899 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 1900 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1901 | SoundTrigger::setCaptureState(false); |
| 1902 | } |
| 1903 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1904 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1905 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1906 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1907 | } |
| 1908 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1909 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1910 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1911 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1912 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1913 | ALOGV("releaseInput() %d", input); |
| 1914 | ssize_t index = mInputs.indexOfKey(input); |
| 1915 | if (index < 0) { |
| 1916 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1917 | return; |
| 1918 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1919 | |
| 1920 | // Routing |
| 1921 | mInputRoutes.removeRoute(session); |
| 1922 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1923 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1924 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1925 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1926 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1927 | if (index < 0) { |
| 1928 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1929 | return; |
| 1930 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1931 | |
| 1932 | if (audioSession->openCount() == 0) { |
| 1933 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 1934 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1935 | return; |
| 1936 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1937 | |
| 1938 | if (audioSession->changeOpenCount(-1) == 0) { |
| 1939 | inputDesc->removeAudioSession(session); |
| 1940 | } |
| 1941 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1942 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1943 | ALOGV("releaseInput() exit > 0"); |
| 1944 | return; |
| 1945 | } |
| 1946 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1947 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1948 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1949 | ALOGV("releaseInput() exit"); |
| 1950 | } |
| 1951 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1952 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1953 | bool patchRemoved = false; |
| 1954 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1955 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1956 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1957 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1958 | if (patch_index >= 0) { |
| 1959 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 1960 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1961 | mAudioPatches.removeItemsAt(patch_index); |
| 1962 | patchRemoved = true; |
| 1963 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1964 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1965 | } |
| 1966 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1967 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1968 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1969 | |
| 1970 | if (patchRemoved) { |
| 1971 | mpClientInterface->onAudioPatchListUpdate(); |
| 1972 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1975 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1976 | int indexMin, |
| 1977 | int indexMax) |
| 1978 | { |
| 1979 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1980 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1981 | |
| 1982 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 1983 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 1984 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 1985 | continue; |
| 1986 | } |
| 1987 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1988 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1989 | } |
| 1990 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1991 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1992 | int index, |
| 1993 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1994 | { |
| 1995 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1996 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 1997 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1998 | return BAD_VALUE; |
| 1999 | } |
| 2000 | if (!audio_is_output_device(device)) { |
| 2001 | return BAD_VALUE; |
| 2002 | } |
| 2003 | |
| 2004 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2005 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2006 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2007 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2008 | stream, device, index); |
| 2009 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2010 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2011 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2012 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2013 | continue; |
| 2014 | } |
| 2015 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2016 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2017 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2018 | // update volume on all outputs and streams matching the following: |
| 2019 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2020 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2021 | // the requested device |
| 2022 | // - For non default requested device, currently selected device on the output is either the |
| 2023 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2024 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2025 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2026 | status_t status = NO_ERROR; |
| 2027 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2028 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2029 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2030 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2031 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2032 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2033 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2034 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2035 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2036 | continue; |
| 2037 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2038 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2039 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/); |
| 2040 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2041 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2042 | continue; |
| 2043 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2044 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2045 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2046 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2047 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2048 | } else { |
| 2049 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
| 2050 | stream, Volume::getDeviceForVolume(curStreamDevice)); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2051 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2052 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2053 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2054 | //FIXME: workaround for truncated touch sounds |
| 2055 | // delayed volume change for system stream to be removed when the problem is |
| 2056 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2057 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2058 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2059 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2060 | if (volStatus != NO_ERROR) { |
| 2061 | status = volStatus; |
| 2062 | } |
| 2063 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2064 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2065 | } |
| 2066 | return status; |
| 2067 | } |
| 2068 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2069 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2070 | int *index, |
| 2071 | audio_devices_t device) |
| 2072 | { |
| 2073 | if (index == NULL) { |
| 2074 | return BAD_VALUE; |
| 2075 | } |
| 2076 | if (!audio_is_output_device(device)) { |
| 2077 | return BAD_VALUE; |
| 2078 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2079 | // 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] | 2080 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2081 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2082 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2083 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2084 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2085 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2086 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2087 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2088 | return NO_ERROR; |
| 2089 | } |
| 2090 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2091 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2092 | const SortedVector<audio_io_handle_t>& outputs) |
| 2093 | { |
| 2094 | // select one output among several suitable for global effects. |
| 2095 | // The priority is as follows: |
| 2096 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2097 | // AudioFlinger will invalidate the track and the offloaded output |
| 2098 | // will be closed causing the effect to be moved to a PCM output. |
| 2099 | // 2: A deep buffer output |
| 2100 | // 3: the first output in the list |
| 2101 | |
| 2102 | if (outputs.size() == 0) { |
| 2103 | return 0; |
| 2104 | } |
| 2105 | |
| 2106 | audio_io_handle_t outputOffloaded = 0; |
| 2107 | audio_io_handle_t outputDeepBuffer = 0; |
| 2108 | |
| 2109 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2110 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2111 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2112 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 2113 | outputOffloaded = outputs[i]; |
| 2114 | } |
| 2115 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 2116 | outputDeepBuffer = outputs[i]; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 2121 | outputOffloaded, outputDeepBuffer); |
| 2122 | if (outputOffloaded != 0) { |
| 2123 | return outputOffloaded; |
| 2124 | } |
| 2125 | if (outputDeepBuffer != 0) { |
| 2126 | return outputDeepBuffer; |
| 2127 | } |
| 2128 | |
| 2129 | return outputs[0]; |
| 2130 | } |
| 2131 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2132 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2133 | { |
| 2134 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 2135 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2136 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2138 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 2139 | |
| 2140 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 2141 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 2142 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 2143 | |
| 2144 | return output; |
| 2145 | } |
| 2146 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2147 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2148 | audio_io_handle_t io, |
| 2149 | uint32_t strategy, |
| 2150 | int session, |
| 2151 | int id) |
| 2152 | { |
| 2153 | ssize_t index = mOutputs.indexOfKey(io); |
| 2154 | if (index < 0) { |
| 2155 | index = mInputs.indexOfKey(io); |
| 2156 | if (index < 0) { |
| 2157 | ALOGW("registerEffect() unknown io %d", io); |
| 2158 | return INVALID_OPERATION; |
| 2159 | } |
| 2160 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2161 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2162 | } |
| 2163 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2164 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2165 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2166 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2167 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2168 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2169 | continue; |
| 2170 | } |
| 2171 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2172 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2173 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
| 2176 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2177 | { |
| 2178 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2179 | } |
| 2180 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2181 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2182 | { |
| 2183 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2184 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2185 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2186 | return true; |
| 2187 | } |
| 2188 | } |
| 2189 | return false; |
| 2190 | } |
| 2191 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2192 | // Register a list of custom mixes with their attributes and format. |
| 2193 | // When a mix is registered, corresponding input and output profiles are |
| 2194 | // added to the remote submix hw module. The profile contains only the |
| 2195 | // parameters (sampling rate, format...) specified by the mix. |
| 2196 | // The corresponding input remote submix device is also connected. |
| 2197 | // |
| 2198 | // When a remote submix device is connected, the address is checked to select the |
| 2199 | // appropriate profile and the corresponding input or output stream is opened. |
| 2200 | // |
| 2201 | // When capture starts, getInputForAttr() will: |
| 2202 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2203 | // - 2 if none found, getDeviceForInputSource() will: |
| 2204 | // - 2.1 look for a mix matching the attributes source |
| 2205 | // - 2.2 if none found, default to device selection by policy rules |
| 2206 | // At this time, the corresponding output remote submix device is also connected |
| 2207 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2208 | // after AudioTracks are invalidated |
| 2209 | // |
| 2210 | // When playback starts, getOutputForAttr() will: |
| 2211 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2212 | // - 2 if none found, look for a mix matching the attributes usage |
| 2213 | // - 3 if none found, default to device and output selection by policy rules. |
| 2214 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2215 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2216 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2217 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2218 | status_t res = NO_ERROR; |
| 2219 | |
| 2220 | sp<HwModule> rSubmixModule; |
| 2221 | // examine each mix's route type |
| 2222 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2223 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2224 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2225 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2226 | break; |
| 2227 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2228 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2229 | // Loop back through "remote submix" |
| 2230 | if (rSubmixModule == 0) { |
| 2231 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2232 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2233 | && mHwModules[j]->mHandle != 0) { |
| 2234 | rSubmixModule = mHwModules[j]; |
| 2235 | break; |
| 2236 | } |
| 2237 | } |
| 2238 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2239 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2240 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2241 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2242 | if (rSubmixModule == 0) { |
| 2243 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2244 | res = INVALID_OPERATION; |
| 2245 | break; |
| 2246 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2247 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2248 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2249 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2250 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2251 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2252 | res = INVALID_OPERATION; |
| 2253 | break; |
| 2254 | } |
| 2255 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2256 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2257 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2258 | // stereo and let audio flinger do the channel conversion if needed. |
| 2259 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2260 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2261 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2262 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2263 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2264 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2265 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2266 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2267 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2268 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2269 | address.string(), "remote-submix"); |
| 2270 | } else { |
| 2271 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2272 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2273 | address.string(), "remote-submix"); |
| 2274 | } |
| 2275 | } 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] | 2276 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2277 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2278 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2279 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2280 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2281 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2282 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2283 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2284 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2285 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2286 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2287 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2288 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2289 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2290 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2291 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2292 | } else { |
| 2293 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2294 | } |
| 2295 | break; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | if (res != NO_ERROR) { |
| 2300 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2301 | i, device, address.string()); |
| 2302 | res = INVALID_OPERATION; |
| 2303 | break; |
| 2304 | } else if (!foundOutput) { |
| 2305 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2306 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2307 | res = INVALID_OPERATION; |
| 2308 | break; |
| 2309 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2310 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2311 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2312 | if (res != NO_ERROR) { |
| 2313 | unregisterPolicyMixes(mixes); |
| 2314 | } |
| 2315 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2319 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2320 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2321 | status_t res = NO_ERROR; |
| 2322 | sp<HwModule> rSubmixModule; |
| 2323 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2324 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2325 | 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] | 2326 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2327 | if (rSubmixModule == 0) { |
| 2328 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2329 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2330 | && mHwModules[j]->mHandle != 0) { |
| 2331 | rSubmixModule = mHwModules[j]; |
| 2332 | break; |
| 2333 | } |
| 2334 | } |
| 2335 | } |
| 2336 | if (rSubmixModule == 0) { |
| 2337 | res = INVALID_OPERATION; |
| 2338 | continue; |
| 2339 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2340 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2341 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2342 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2343 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2344 | res = INVALID_OPERATION; |
| 2345 | continue; |
| 2346 | } |
| 2347 | |
| 2348 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2349 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2350 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2351 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2352 | address.string(), "remote-submix"); |
| 2353 | } |
| 2354 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2355 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2356 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2357 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2358 | address.string(), "remote-submix"); |
| 2359 | } |
| 2360 | rSubmixModule->removeOutputProfile(address); |
| 2361 | rSubmixModule->removeInputProfile(address); |
| 2362 | |
| 2363 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2364 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2365 | res = INVALID_OPERATION; |
| 2366 | continue; |
| 2367 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2368 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2369 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2370 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2371 | } |
| 2372 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2373 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2374 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2375 | { |
| 2376 | const size_t SIZE = 256; |
| 2377 | char buffer[SIZE]; |
| 2378 | String8 result; |
| 2379 | |
| 2380 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2381 | result.append(buffer); |
| 2382 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2383 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2384 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2385 | result.append(buffer); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2386 | std::string stateLiteral; |
| 2387 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
| 2388 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2389 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2390 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2391 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2392 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2393 | 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] | 2394 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2395 | 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] | 2396 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2397 | 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] | 2398 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2399 | 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] | 2400 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2401 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2402 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2403 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2404 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2405 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2406 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2407 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2408 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2409 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2410 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2411 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2412 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2413 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2414 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2415 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2416 | mHwModules.dump(fd); |
| 2417 | mOutputs.dump(fd); |
| 2418 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2419 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2420 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2421 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame^] | 2422 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2423 | |
| 2424 | return NO_ERROR; |
| 2425 | } |
| 2426 | |
| 2427 | // This function checks for the parameters which can be offloaded. |
| 2428 | // This can be enhanced depending on the capability of the DSP and policy |
| 2429 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2430 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2431 | { |
| 2432 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2433 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2434 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2435 | offloadInfo.format, |
| 2436 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2437 | offloadInfo.has_video); |
| 2438 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2439 | if (mMasterMono) { |
| 2440 | return false; // no offloading if mono is set. |
| 2441 | } |
| 2442 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2443 | // Check if offload has been disabled |
| 2444 | char propValue[PROPERTY_VALUE_MAX]; |
| 2445 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2446 | if (atoi(propValue) != 0) { |
| 2447 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2448 | return false; |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | // Check if stream type is music, then only allow offload as of now. |
| 2453 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2454 | { |
| 2455 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2456 | return false; |
| 2457 | } |
| 2458 | |
| 2459 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2460 | const bool allowOffloadWithVideo = |
| 2461 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2462 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2463 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2464 | return false; |
| 2465 | } |
| 2466 | |
| 2467 | //If duration is less than minimum value defined in property, return false |
| 2468 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2469 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2470 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2471 | return false; |
| 2472 | } |
| 2473 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2474 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2475 | return false; |
| 2476 | } |
| 2477 | |
| 2478 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2479 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2480 | // detects there is an active non offloadable effect. |
| 2481 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2482 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2483 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2484 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2485 | return false; |
| 2486 | } |
| 2487 | |
| 2488 | // See if there is a profile to support this. |
| 2489 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2490 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2491 | offloadInfo.sample_rate, |
| 2492 | offloadInfo.format, |
| 2493 | offloadInfo.channel_mask, |
| 2494 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2495 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2496 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2499 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2500 | audio_port_type_t type, |
| 2501 | unsigned int *num_ports, |
| 2502 | struct audio_port *ports, |
| 2503 | unsigned int *generation) |
| 2504 | { |
| 2505 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2506 | generation == NULL) { |
| 2507 | return BAD_VALUE; |
| 2508 | } |
| 2509 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2510 | if (ports == NULL) { |
| 2511 | *num_ports = 0; |
| 2512 | } |
| 2513 | |
| 2514 | size_t portsWritten = 0; |
| 2515 | size_t portsMax = *num_ports; |
| 2516 | *num_ports = 0; |
| 2517 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2518 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2519 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2520 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2521 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2522 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2523 | continue; |
| 2524 | } |
| 2525 | if (portsWritten < portsMax) { |
| 2526 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2527 | } |
| 2528 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2529 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2530 | } |
| 2531 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2532 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2533 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2534 | continue; |
| 2535 | } |
| 2536 | if (portsWritten < portsMax) { |
| 2537 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2538 | } |
| 2539 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2540 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2544 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2545 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2546 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2547 | } |
| 2548 | *num_ports += mInputs.size(); |
| 2549 | } |
| 2550 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2551 | size_t numOutputs = 0; |
| 2552 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2553 | if (!mOutputs[i]->isDuplicated()) { |
| 2554 | numOutputs++; |
| 2555 | if (portsWritten < portsMax) { |
| 2556 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2557 | } |
| 2558 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2559 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2560 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2561 | } |
| 2562 | } |
| 2563 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2564 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2565 | return NO_ERROR; |
| 2566 | } |
| 2567 | |
| 2568 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2569 | { |
| 2570 | return NO_ERROR; |
| 2571 | } |
| 2572 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2573 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2574 | audio_patch_handle_t *handle, |
| 2575 | uid_t uid) |
| 2576 | { |
| 2577 | ALOGV("createAudioPatch()"); |
| 2578 | |
| 2579 | if (handle == NULL || patch == NULL) { |
| 2580 | return BAD_VALUE; |
| 2581 | } |
| 2582 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2583 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2584 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2585 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2586 | return BAD_VALUE; |
| 2587 | } |
| 2588 | // only one source per audio patch supported for now |
| 2589 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2590 | return INVALID_OPERATION; |
| 2591 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2592 | |
| 2593 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2594 | return INVALID_OPERATION; |
| 2595 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2596 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2597 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2598 | return INVALID_OPERATION; |
| 2599 | } |
| 2600 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2601 | |
| 2602 | sp<AudioPatch> patchDesc; |
| 2603 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2604 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2605 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2606 | patch->sources[0].role, |
| 2607 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2608 | #if LOG_NDEBUG == 0 |
| 2609 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2610 | 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] | 2611 | patch->sinks[i].role, |
| 2612 | patch->sinks[i].type); |
| 2613 | } |
| 2614 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2615 | |
| 2616 | if (index >= 0) { |
| 2617 | patchDesc = mAudioPatches.valueAt(index); |
| 2618 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2619 | mUidCached, patchDesc->mUid, uid); |
| 2620 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2621 | return INVALID_OPERATION; |
| 2622 | } |
| 2623 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2624 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2628 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2629 | if (outputDesc == NULL) { |
| 2630 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2631 | return BAD_VALUE; |
| 2632 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2633 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2634 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2635 | if (patchDesc != 0) { |
| 2636 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2637 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2638 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2639 | return BAD_VALUE; |
| 2640 | } |
| 2641 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2642 | DeviceVector devices; |
| 2643 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2644 | // Only support mix to devices connection |
| 2645 | // TODO add support for mix to mix connection |
| 2646 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2647 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2648 | return INVALID_OPERATION; |
| 2649 | } |
| 2650 | sp<DeviceDescriptor> devDesc = |
| 2651 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2652 | if (devDesc == 0) { |
| 2653 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2654 | return BAD_VALUE; |
| 2655 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2656 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2657 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2658 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2659 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2660 | NULL, // updatedSamplingRate |
| 2661 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2662 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2663 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2664 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2665 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2666 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2667 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2668 | return INVALID_OPERATION; |
| 2669 | } |
| 2670 | devices.add(devDesc); |
| 2671 | } |
| 2672 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2673 | return INVALID_OPERATION; |
| 2674 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2675 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2676 | // TODO: reconfigure output format and channels here |
| 2677 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2678 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2679 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2680 | index = mAudioPatches.indexOfKey(*handle); |
| 2681 | if (index >= 0) { |
| 2682 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2683 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2684 | } |
| 2685 | patchDesc = mAudioPatches.valueAt(index); |
| 2686 | patchDesc->mUid = uid; |
| 2687 | ALOGV("createAudioPatch() success"); |
| 2688 | } else { |
| 2689 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2690 | return INVALID_OPERATION; |
| 2691 | } |
| 2692 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2693 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2694 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2695 | // only one sink supported when connecting an input device to a mix |
| 2696 | if (patch->num_sinks > 1) { |
| 2697 | return INVALID_OPERATION; |
| 2698 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2699 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2700 | if (inputDesc == NULL) { |
| 2701 | return BAD_VALUE; |
| 2702 | } |
| 2703 | if (patchDesc != 0) { |
| 2704 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2705 | return BAD_VALUE; |
| 2706 | } |
| 2707 | } |
| 2708 | sp<DeviceDescriptor> devDesc = |
| 2709 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2710 | if (devDesc == 0) { |
| 2711 | return BAD_VALUE; |
| 2712 | } |
| 2713 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2714 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2715 | devDesc->mAddress, |
| 2716 | patch->sinks[0].sample_rate, |
| 2717 | NULL, /*updatedSampleRate*/ |
| 2718 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2719 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2720 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2721 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2722 | // FIXME for the parameter type, |
| 2723 | // and the NONE |
| 2724 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2725 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2726 | return INVALID_OPERATION; |
| 2727 | } |
| 2728 | // TODO: reconfigure output format and channels here |
| 2729 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2730 | devDesc->type(), inputDesc->mIoHandle); |
| 2731 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2732 | index = mAudioPatches.indexOfKey(*handle); |
| 2733 | if (index >= 0) { |
| 2734 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2735 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2736 | } |
| 2737 | patchDesc = mAudioPatches.valueAt(index); |
| 2738 | patchDesc->mUid = uid; |
| 2739 | ALOGV("createAudioPatch() success"); |
| 2740 | } else { |
| 2741 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2742 | return INVALID_OPERATION; |
| 2743 | } |
| 2744 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2745 | // device to device connection |
| 2746 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2747 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2748 | return BAD_VALUE; |
| 2749 | } |
| 2750 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2751 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2752 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2753 | if (srcDeviceDesc == 0) { |
| 2754 | return BAD_VALUE; |
| 2755 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2756 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2757 | //update source and sink with our own data as the data passed in the patch may |
| 2758 | // be incomplete. |
| 2759 | struct audio_patch newPatch = *patch; |
| 2760 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2761 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2762 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2763 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2764 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2765 | return INVALID_OPERATION; |
| 2766 | } |
| 2767 | |
| 2768 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2769 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2770 | if (sinkDeviceDesc == 0) { |
| 2771 | return BAD_VALUE; |
| 2772 | } |
| 2773 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2774 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2775 | // create a software bridge in PatchPanel if: |
| 2776 | // - source and sink devices are on differnt HW modules OR |
| 2777 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2778 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2779 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2780 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2781 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2782 | return INVALID_OPERATION; |
| 2783 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2784 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2785 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2786 | // if the sink device is reachable via an opened output stream, request to go via |
| 2787 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2788 | audio_io_handle_t output = selectOutput(outputs, |
| 2789 | AUDIO_OUTPUT_FLAG_NONE, |
| 2790 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2791 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2792 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2793 | if (outputDesc->isDuplicated()) { |
| 2794 | return INVALID_OPERATION; |
| 2795 | } |
| 2796 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2797 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2798 | newPatch.num_sources = 2; |
| 2799 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2800 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2801 | } |
| 2802 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2803 | |
| 2804 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2805 | if (index >= 0) { |
| 2806 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2807 | } |
| 2808 | |
| 2809 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2810 | &afPatchHandle, |
| 2811 | 0); |
| 2812 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2813 | status, afPatchHandle); |
| 2814 | if (status == NO_ERROR) { |
| 2815 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2816 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2817 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2818 | } else { |
| 2819 | patchDesc->mPatch = newPatch; |
| 2820 | } |
| 2821 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2822 | *handle = patchDesc->mHandle; |
| 2823 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2824 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2825 | } else { |
| 2826 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2827 | status); |
| 2828 | return INVALID_OPERATION; |
| 2829 | } |
| 2830 | } else { |
| 2831 | return BAD_VALUE; |
| 2832 | } |
| 2833 | } else { |
| 2834 | return BAD_VALUE; |
| 2835 | } |
| 2836 | return NO_ERROR; |
| 2837 | } |
| 2838 | |
| 2839 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2840 | uid_t uid) |
| 2841 | { |
| 2842 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2843 | |
| 2844 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2845 | |
| 2846 | if (index < 0) { |
| 2847 | return BAD_VALUE; |
| 2848 | } |
| 2849 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2850 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2851 | mUidCached, patchDesc->mUid, uid); |
| 2852 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2853 | return INVALID_OPERATION; |
| 2854 | } |
| 2855 | |
| 2856 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2857 | patchDesc->mUid = mUidCached; |
| 2858 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2859 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2860 | if (outputDesc == NULL) { |
| 2861 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2862 | return BAD_VALUE; |
| 2863 | } |
| 2864 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2865 | setOutputDevice(outputDesc, |
| 2866 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2867 | true, |
| 2868 | 0, |
| 2869 | NULL); |
| 2870 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2871 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2872 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2873 | if (inputDesc == NULL) { |
| 2874 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2875 | return BAD_VALUE; |
| 2876 | } |
| 2877 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2878 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2879 | true, |
| 2880 | NULL); |
| 2881 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2882 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2883 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2884 | status, patchDesc->mAfPatchHandle); |
| 2885 | removeAudioPatch(patchDesc->mHandle); |
| 2886 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2887 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2888 | } else { |
| 2889 | return BAD_VALUE; |
| 2890 | } |
| 2891 | } else { |
| 2892 | return BAD_VALUE; |
| 2893 | } |
| 2894 | return NO_ERROR; |
| 2895 | } |
| 2896 | |
| 2897 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2898 | struct audio_patch *patches, |
| 2899 | unsigned int *generation) |
| 2900 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2901 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2902 | return BAD_VALUE; |
| 2903 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2904 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2905 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2906 | } |
| 2907 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2908 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2909 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2910 | ALOGV("setAudioPortConfig()"); |
| 2911 | |
| 2912 | if (config == NULL) { |
| 2913 | return BAD_VALUE; |
| 2914 | } |
| 2915 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2916 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2917 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2918 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2919 | } |
| 2920 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2921 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2922 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2923 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2924 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2925 | if (outputDesc == NULL) { |
| 2926 | return BAD_VALUE; |
| 2927 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2928 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2929 | "setAudioPortConfig() called on duplicated output %d", |
| 2930 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2931 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2932 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2933 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2934 | if (inputDesc == NULL) { |
| 2935 | return BAD_VALUE; |
| 2936 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2937 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2938 | } else { |
| 2939 | return BAD_VALUE; |
| 2940 | } |
| 2941 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2942 | sp<DeviceDescriptor> deviceDesc; |
| 2943 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2944 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2945 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2946 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2947 | } else { |
| 2948 | return BAD_VALUE; |
| 2949 | } |
| 2950 | if (deviceDesc == NULL) { |
| 2951 | return BAD_VALUE; |
| 2952 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2953 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2954 | } else { |
| 2955 | return BAD_VALUE; |
| 2956 | } |
| 2957 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2958 | struct audio_port_config backupConfig; |
| 2959 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2960 | if (status == NO_ERROR) { |
| 2961 | struct audio_port_config newConfig; |
| 2962 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2963 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2964 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2965 | if (status != NO_ERROR) { |
| 2966 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2967 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2968 | |
| 2969 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2972 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 2973 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 2974 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2975 | clearAudioPatches(uid); |
| 2976 | clearSessionRoutes(uid); |
| 2977 | } |
| 2978 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2979 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2980 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2981 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2982 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2983 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2984 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | } |
| 2988 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2989 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 2990 | audio_io_handle_t ouptutToSkip) |
| 2991 | { |
| 2992 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2993 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 2994 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 2995 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 2996 | continue; |
| 2997 | } |
| 2998 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 2999 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3000 | continue; |
| 3001 | } |
| 3002 | // If the default device for this strategy is on another output mix, |
| 3003 | // invalidate all tracks in this strategy to force re connection. |
| 3004 | // Otherwise select new device on the output mix. |
| 3005 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3006 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3007 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3008 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3009 | } |
| 3010 | } |
| 3011 | } else { |
| 3012 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3013 | setOutputDevice(outputDesc, newDevice, false); |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3019 | { |
| 3020 | // remove output routes associated with this uid |
| 3021 | SortedVector<routing_strategy> affectedStrategies; |
| 3022 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3023 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3024 | if (route->mUid == uid) { |
| 3025 | mOutputRoutes.removeItemsAt(i); |
| 3026 | if (route->mDeviceDescriptor != 0) { |
| 3027 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3028 | } |
| 3029 | } |
| 3030 | } |
| 3031 | // reroute outputs if necessary |
| 3032 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 3033 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 3034 | } |
| 3035 | |
| 3036 | // remove input routes associated with this uid |
| 3037 | SortedVector<audio_source_t> affectedSources; |
| 3038 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3039 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3040 | if (route->mUid == uid) { |
| 3041 | mInputRoutes.removeItemsAt(i); |
| 3042 | if (route->mDeviceDescriptor != 0) { |
| 3043 | affectedSources.add(route->mSource); |
| 3044 | } |
| 3045 | } |
| 3046 | } |
| 3047 | // reroute inputs if necessary |
| 3048 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3049 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3050 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3051 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3052 | inputsToClose.add(inputDesc->mIoHandle); |
| 3053 | } |
| 3054 | } |
| 3055 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 3056 | closeInput(inputsToClose[i]); |
| 3057 | } |
| 3058 | } |
| 3059 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3060 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3061 | { |
| 3062 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3063 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3064 | if (sourceDesc->mUid == uid) { |
| 3065 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3066 | } |
| 3067 | } |
| 3068 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3069 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3070 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3071 | audio_io_handle_t *ioHandle, |
| 3072 | audio_devices_t *device) |
| 3073 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3074 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3075 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3076 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3077 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3078 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3079 | } |
| 3080 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3081 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3082 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3083 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3084 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3085 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3086 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3087 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3088 | return BAD_VALUE; |
| 3089 | } |
| 3090 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3091 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3092 | |
| 3093 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3094 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3095 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3096 | return INVALID_OPERATION; |
| 3097 | } |
| 3098 | |
| 3099 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3100 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3101 | String8(source->ext.device.address)); |
| 3102 | if (srcDeviceDesc == 0) { |
| 3103 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3104 | return BAD_VALUE; |
| 3105 | } |
| 3106 | sp<AudioSourceDescriptor> sourceDesc = |
| 3107 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3108 | |
| 3109 | struct audio_patch dummyPatch; |
| 3110 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3111 | sourceDesc->mPatchDesc = patchDesc; |
| 3112 | |
| 3113 | status_t status = connectAudioSource(sourceDesc); |
| 3114 | if (status == NO_ERROR) { |
| 3115 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3116 | *handle = sourceDesc->getHandle(); |
| 3117 | } |
| 3118 | return status; |
| 3119 | } |
| 3120 | |
| 3121 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3122 | { |
| 3123 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3124 | |
| 3125 | // make sure we only have one patch per source. |
| 3126 | disconnectAudioSource(sourceDesc); |
| 3127 | |
| 3128 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3129 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3130 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3131 | |
| 3132 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3133 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3134 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3135 | |
| 3136 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3137 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3138 | |
| 3139 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3140 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3141 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3142 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3143 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3144 | // create patch between src device and output device |
| 3145 | // create Hwoutput and add to mHwOutputs |
| 3146 | } else { |
| 3147 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3148 | audio_io_handle_t output = |
| 3149 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3150 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3151 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3152 | return INVALID_OPERATION; |
| 3153 | } |
| 3154 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3155 | if (outputDesc->isDuplicated()) { |
| 3156 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3157 | return INVALID_OPERATION; |
| 3158 | } |
| 3159 | // create a special patch with no sink and two sources: |
| 3160 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3161 | // be connected as well as the stream type for volume control |
| 3162 | // - the sink is defined by whatever output device is currently selected for the output |
| 3163 | // though which this patch is routed. |
| 3164 | patch->num_sinks = 0; |
| 3165 | patch->num_sources = 2; |
| 3166 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3167 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3168 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3169 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3170 | &afPatchHandle, |
| 3171 | 0); |
| 3172 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3173 | status, afPatchHandle); |
| 3174 | if (status != NO_ERROR) { |
| 3175 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3176 | __FUNCTION__, status); |
| 3177 | return INVALID_OPERATION; |
| 3178 | } |
| 3179 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3180 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3181 | |
| 3182 | if (status != NO_ERROR) { |
| 3183 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3184 | return status; |
| 3185 | } |
| 3186 | sourceDesc->mSwOutput = outputDesc; |
| 3187 | if (delayMs != 0) { |
| 3188 | usleep(delayMs * 1000); |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3193 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3194 | |
| 3195 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3198 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3199 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3200 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3201 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3202 | if (sourceDesc == 0) { |
| 3203 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3204 | return BAD_VALUE; |
| 3205 | } |
| 3206 | status_t status = disconnectAudioSource(sourceDesc); |
| 3207 | |
| 3208 | mAudioSources.removeItem(handle); |
| 3209 | return status; |
| 3210 | } |
| 3211 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3212 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3213 | { |
| 3214 | if (mMasterMono == mono) { |
| 3215 | return NO_ERROR; |
| 3216 | } |
| 3217 | mMasterMono = mono; |
| 3218 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3219 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3220 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3221 | // |
| 3222 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3223 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3224 | // play back offloaded. |
| 3225 | if (mMasterMono) { |
| 3226 | Vector<audio_io_handle_t> offloaded; |
| 3227 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3228 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3229 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3230 | offloaded.push(desc->mIoHandle); |
| 3231 | } |
| 3232 | } |
| 3233 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3234 | closeOutput(offloaded[i]); |
| 3235 | } |
| 3236 | } |
| 3237 | // update master mono for all remaining outputs |
| 3238 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3239 | updateMono(mOutputs.keyAt(i)); |
| 3240 | } |
| 3241 | return NO_ERROR; |
| 3242 | } |
| 3243 | |
| 3244 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3245 | { |
| 3246 | *mono = mMasterMono; |
| 3247 | return NO_ERROR; |
| 3248 | } |
| 3249 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3250 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3251 | { |
| 3252 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3253 | |
| 3254 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3255 | if (patchDesc == 0) { |
| 3256 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3257 | sourceDesc->mPatchDesc->mHandle); |
| 3258 | return BAD_VALUE; |
| 3259 | } |
| 3260 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3261 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3262 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3263 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3264 | if (swOutputDesc != 0) { |
| 3265 | stopSource(swOutputDesc, stream, false); |
| 3266 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3267 | } else { |
| 3268 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3269 | if (hwOutputDesc != 0) { |
| 3270 | // release patch between src device and output device |
| 3271 | // close Hwoutput and remove from mHwOutputs |
| 3272 | } else { |
| 3273 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3274 | } |
| 3275 | } |
| 3276 | return NO_ERROR; |
| 3277 | } |
| 3278 | |
| 3279 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3280 | audio_io_handle_t output, routing_strategy strategy) |
| 3281 | { |
| 3282 | sp<AudioSourceDescriptor> source; |
| 3283 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3284 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3285 | routing_strategy sourceStrategy = |
| 3286 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3287 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3288 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3289 | source = sourceDesc; |
| 3290 | break; |
| 3291 | } |
| 3292 | } |
| 3293 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3296 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3297 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3298 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3299 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3300 | { |
| 3301 | return android_atomic_inc(&mAudioPortGeneration); |
| 3302 | } |
| 3303 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3304 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3305 | : |
| 3306 | #ifdef AUDIO_POLICY_TEST |
| 3307 | Thread(false), |
| 3308 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3309 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3310 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3311 | mAudioPortGeneration(1), |
| 3312 | mBeaconMuteRefCount(0), |
| 3313 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3314 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3315 | mTtsOutputAvailable(false), |
| 3316 | mMasterMono(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3317 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3318 | mUidCached = getuid(); |
| 3319 | mpClientInterface = clientInterface; |
| 3320 | |
| 3321 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3322 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3323 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3324 | bool speakerDrcEnabled = false; |
| 3325 | |
| 3326 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3327 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3328 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3329 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3330 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
| 3331 | PolicySerializer serializer; |
| 3332 | if (serializer.deserialize(AUDIO_POLICY_XML_CONFIG_FILE, config) != NO_ERROR) { |
| 3333 | #else |
| 3334 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3335 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3336 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3337 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3338 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3339 | #endif |
| 3340 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3341 | config.setDefault(); |
| 3342 | } |
| 3343 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3344 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3345 | |
| 3346 | // 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] | 3347 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3348 | if (!engineInstance) { |
| 3349 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3350 | return; |
| 3351 | } |
| 3352 | // Retrieve the Policy Manager Interface |
| 3353 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3354 | if (mEngine == NULL) { |
| 3355 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3356 | return; |
| 3357 | } |
| 3358 | mEngine->setObserver(this); |
| 3359 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3360 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3361 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3362 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3363 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3364 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3365 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3366 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3367 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3368 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3369 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3370 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3371 | continue; |
| 3372 | } |
| 3373 | // open all output streams needed to access attached devices |
| 3374 | // except for direct output streams that are only opened when they are actually |
| 3375 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3376 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3377 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3378 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3379 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3380 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3381 | if (!outProfile->hasSupportedDevices()) { |
| 3382 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3383 | continue; |
| 3384 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3385 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3386 | mTtsOutputAvailable = true; |
| 3387 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3388 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3389 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3390 | continue; |
| 3391 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3392 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3393 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3394 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3395 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3396 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3397 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3398 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3399 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3400 | if ((profileType & outputDeviceTypes) == 0) { |
| 3401 | continue; |
| 3402 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3403 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3404 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3405 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3406 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3407 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3408 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3409 | |
| 3410 | outputDesc->mDevice = profileType; |
| 3411 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3412 | config.sample_rate = outputDesc->mSamplingRate; |
| 3413 | config.channel_mask = outputDesc->mChannelMask; |
| 3414 | config.format = outputDesc->mFormat; |
| 3415 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3416 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3417 | &output, |
| 3418 | &config, |
| 3419 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3420 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3421 | &outputDesc->mLatency, |
| 3422 | outputDesc->mFlags); |
| 3423 | |
| 3424 | if (status != NO_ERROR) { |
| 3425 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3426 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3427 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3428 | } else { |
| 3429 | outputDesc->mSamplingRate = config.sample_rate; |
| 3430 | outputDesc->mChannelMask = config.channel_mask; |
| 3431 | outputDesc->mFormat = config.format; |
| 3432 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3433 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3434 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3435 | // 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] | 3436 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3437 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3438 | } |
| 3439 | } |
| 3440 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3441 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3442 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3443 | } |
| 3444 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3445 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3446 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3447 | true, |
| 3448 | 0, |
| 3449 | NULL, |
| 3450 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3451 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3452 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3453 | // open input streams needed to access attached devices to validate |
| 3454 | // mAvailableInputDevices list |
| 3455 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3456 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3457 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3458 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3459 | if (!inProfile->hasSupportedDevices()) { |
| 3460 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3461 | continue; |
| 3462 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3463 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3464 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3465 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3466 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3467 | if ((profileType & inputDeviceTypes) == 0) { |
| 3468 | continue; |
| 3469 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3470 | sp<AudioInputDescriptor> inputDesc = |
| 3471 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3472 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3473 | inputDesc->mDevice = profileType; |
| 3474 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3475 | // find the address |
| 3476 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3477 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3478 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3479 | : String8(""); |
| 3480 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3481 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3482 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3483 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3484 | config.sample_rate = inputDesc->mSamplingRate; |
| 3485 | config.channel_mask = inputDesc->mChannelMask; |
| 3486 | config.format = inputDesc->mFormat; |
| 3487 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3488 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3489 | &input, |
| 3490 | &config, |
| 3491 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3492 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3493 | AUDIO_SOURCE_MIC, |
| 3494 | AUDIO_INPUT_FLAG_NONE); |
| 3495 | |
| 3496 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3497 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3498 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3499 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3500 | // 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] | 3501 | if (index >= 0) { |
| 3502 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3503 | if (!devDesc->isAttached()) { |
| 3504 | devDesc->attach(mHwModules[i]); |
| 3505 | devDesc->importAudioPort(inProfile); |
| 3506 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3507 | } |
| 3508 | } |
| 3509 | mpClientInterface->closeInput(input); |
| 3510 | } else { |
| 3511 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3512 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3513 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3514 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3515 | } |
| 3516 | } |
| 3517 | // make sure all attached devices have been allocated a unique ID |
| 3518 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3519 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3520 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3521 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3522 | continue; |
| 3523 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3524 | // The device is now validated and can be appended to the available devices of the engine |
| 3525 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3526 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3527 | i++; |
| 3528 | } |
| 3529 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3530 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3531 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3532 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3533 | continue; |
| 3534 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3535 | // The device is now validated and can be appended to the available devices of the engine |
| 3536 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3537 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3538 | i++; |
| 3539 | } |
| 3540 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3541 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3542 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3543 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 | |
| 3545 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3546 | |
| 3547 | updateDevicesAndOutputs(); |
| 3548 | |
| 3549 | #ifdef AUDIO_POLICY_TEST |
| 3550 | if (mPrimaryOutput != 0) { |
| 3551 | AudioParameter outputCmd = AudioParameter(); |
| 3552 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3553 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3554 | |
| 3555 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3556 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3557 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3558 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3559 | mTestLatencyMs = 0; |
| 3560 | mCurOutput = 0; |
| 3561 | mDirectOutput = false; |
| 3562 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3563 | mTestOutputs[i] = 0; |
| 3564 | } |
| 3565 | |
| 3566 | const size_t SIZE = 256; |
| 3567 | char buffer[SIZE]; |
| 3568 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3569 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3570 | } |
| 3571 | #endif //AUDIO_POLICY_TEST |
| 3572 | } |
| 3573 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3574 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3575 | { |
| 3576 | #ifdef AUDIO_POLICY_TEST |
| 3577 | exit(); |
| 3578 | #endif //AUDIO_POLICY_TEST |
| 3579 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3580 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3581 | } |
| 3582 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3583 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3584 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3585 | mAvailableOutputDevices.clear(); |
| 3586 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3587 | mOutputs.clear(); |
| 3588 | mInputs.clear(); |
| 3589 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3592 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3593 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3594 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3595 | } |
| 3596 | |
| 3597 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3598 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3599 | { |
| 3600 | ALOGV("entering threadLoop()"); |
| 3601 | while (!exitPending()) |
| 3602 | { |
| 3603 | String8 command; |
| 3604 | int valueInt; |
| 3605 | String8 value; |
| 3606 | |
| 3607 | Mutex::Autolock _l(mLock); |
| 3608 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3609 | |
| 3610 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3611 | AudioParameter param = AudioParameter(command); |
| 3612 | |
| 3613 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3614 | valueInt != 0) { |
| 3615 | ALOGV("Test command %s received", command.string()); |
| 3616 | String8 target; |
| 3617 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3618 | target = "Manager"; |
| 3619 | } |
| 3620 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3621 | param.remove(String8("test_cmd_policy_output")); |
| 3622 | mCurOutput = valueInt; |
| 3623 | } |
| 3624 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3625 | param.remove(String8("test_cmd_policy_direct")); |
| 3626 | if (value == "false") { |
| 3627 | mDirectOutput = false; |
| 3628 | } else if (value == "true") { |
| 3629 | mDirectOutput = true; |
| 3630 | } |
| 3631 | } |
| 3632 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3633 | param.remove(String8("test_cmd_policy_input")); |
| 3634 | mTestInput = valueInt; |
| 3635 | } |
| 3636 | |
| 3637 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3638 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3639 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3640 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3641 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3642 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3643 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3644 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3645 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3646 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3647 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3648 | if (target == "Manager") { |
| 3649 | mTestFormat = format; |
| 3650 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3651 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3652 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3653 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3654 | } |
| 3655 | } |
| 3656 | } |
| 3657 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3658 | param.remove(String8("test_cmd_policy_channels")); |
| 3659 | int channels = 0; |
| 3660 | |
| 3661 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3662 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3663 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3664 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3665 | } |
| 3666 | if (channels != 0) { |
| 3667 | if (target == "Manager") { |
| 3668 | mTestChannels = channels; |
| 3669 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3670 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3671 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3672 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3673 | } |
| 3674 | } |
| 3675 | } |
| 3676 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3677 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3678 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3679 | int samplingRate = valueInt; |
| 3680 | if (target == "Manager") { |
| 3681 | mTestSamplingRate = samplingRate; |
| 3682 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3683 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3684 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3685 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3686 | } |
| 3687 | } |
| 3688 | } |
| 3689 | |
| 3690 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3691 | param.remove(String8("test_cmd_policy_reopen")); |
| 3692 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3693 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3694 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3695 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3696 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3697 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3698 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3699 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3700 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3701 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3702 | config.sample_rate = outputDesc->mSamplingRate; |
| 3703 | config.channel_mask = outputDesc->mChannelMask; |
| 3704 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3705 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3706 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3707 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3708 | &config, |
| 3709 | &outputDesc->mDevice, |
| 3710 | String8(""), |
| 3711 | &outputDesc->mLatency, |
| 3712 | outputDesc->mFlags); |
| 3713 | if (status != NO_ERROR) { |
| 3714 | ALOGE("Failed to reopen hardware output stream, " |
| 3715 | "samplingRate: %d, format %d, channels %d", |
| 3716 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3717 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3718 | outputDesc->mSamplingRate = config.sample_rate; |
| 3719 | outputDesc->mChannelMask = config.channel_mask; |
| 3720 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3721 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3722 | AudioParameter outputCmd = AudioParameter(); |
| 3723 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3724 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3725 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | |
| 3730 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3731 | } |
| 3732 | } |
| 3733 | return false; |
| 3734 | } |
| 3735 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3736 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3737 | { |
| 3738 | { |
| 3739 | AutoMutex _l(mLock); |
| 3740 | requestExit(); |
| 3741 | mWaitWorkCV.signal(); |
| 3742 | } |
| 3743 | requestExitAndWait(); |
| 3744 | } |
| 3745 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3746 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3747 | { |
| 3748 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3749 | if (output == mTestOutputs[i]) return i; |
| 3750 | } |
| 3751 | return 0; |
| 3752 | } |
| 3753 | #endif //AUDIO_POLICY_TEST |
| 3754 | |
| 3755 | // --- |
| 3756 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3757 | void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3758 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3759 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3760 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3761 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3762 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3763 | } |
| 3764 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3765 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3766 | { |
| 3767 | mOutputs.removeItem(output); |
| 3768 | } |
| 3769 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3770 | void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3771 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3772 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3773 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3774 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3775 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3776 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3777 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3778 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3779 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3780 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3781 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3782 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3783 | if (devDesc != 0) { |
| 3784 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3785 | desc->mIoHandle, address.string()); |
| 3786 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3787 | } |
| 3788 | } |
| 3789 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3790 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3791 | audio_policy_dev_state_t state, |
| 3792 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3793 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3794 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3795 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3796 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3797 | |
| 3798 | if (audio_device_is_digital(device)) { |
| 3799 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3800 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3801 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3802 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3803 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3804 | // first list already open outputs that can be routed to this device |
| 3805 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3806 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3807 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3808 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3809 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3810 | outputs.add(mOutputs.keyAt(i)); |
| 3811 | } else { |
| 3812 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3813 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3814 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3815 | } |
| 3816 | } |
| 3817 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3818 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3819 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3820 | { |
| 3821 | if (mHwModules[i]->mHandle == 0) { |
| 3822 | continue; |
| 3823 | } |
| 3824 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3825 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3826 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3827 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3828 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3829 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3830 | profiles.add(profile); |
| 3831 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3832 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3833 | } |
| 3834 | } |
| 3835 | } |
| 3836 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3837 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3838 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3839 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3840 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3841 | return BAD_VALUE; |
| 3842 | } |
| 3843 | |
| 3844 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3845 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3846 | 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] | 3847 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3848 | |
| 3849 | // nothing to do if one output is already opened for this profile |
| 3850 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3851 | for (j = 0; j < outputs.size(); j++) { |
| 3852 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3853 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3854 | // matching profile: save the sample rates, format and channel masks supported |
| 3855 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3856 | if (audio_device_is_digital(device)) { |
| 3857 | devDesc->importAudioPort(profile); |
| 3858 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3859 | break; |
| 3860 | } |
| 3861 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3862 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3863 | continue; |
| 3864 | } |
| 3865 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3866 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3867 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3868 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3869 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3870 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3871 | config.sample_rate = desc->mSamplingRate; |
| 3872 | config.channel_mask = desc->mChannelMask; |
| 3873 | config.format = desc->mFormat; |
| 3874 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3875 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3876 | config.offload_info.format = desc->mFormat; |
| 3877 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3878 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3879 | &output, |
| 3880 | &config, |
| 3881 | &desc->mDevice, |
| 3882 | address, |
| 3883 | &desc->mLatency, |
| 3884 | desc->mFlags); |
| 3885 | if (status == NO_ERROR) { |
| 3886 | desc->mSamplingRate = config.sample_rate; |
| 3887 | desc->mChannelMask = config.channel_mask; |
| 3888 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3889 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3890 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3891 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3892 | char *param = audio_device_address_to_parameter(device, address); |
| 3893 | mpClientInterface->setParameters(output, String8(param)); |
| 3894 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3895 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3896 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3897 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3898 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3899 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3900 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3901 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3902 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3903 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3904 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3905 | config.offload_info.sample_rate = config.sample_rate; |
| 3906 | config.offload_info.channel_mask = config.channel_mask; |
| 3907 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3908 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3909 | &output, |
| 3910 | &config, |
| 3911 | &desc->mDevice, |
| 3912 | address, |
| 3913 | &desc->mLatency, |
| 3914 | desc->mFlags); |
| 3915 | if (status == NO_ERROR) { |
| 3916 | desc->mSamplingRate = config.sample_rate; |
| 3917 | desc->mChannelMask = config.channel_mask; |
| 3918 | desc->mFormat = config.format; |
| 3919 | } else { |
| 3920 | output = AUDIO_IO_HANDLE_NONE; |
| 3921 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3924 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3925 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3926 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3927 | sp<AudioPolicyMix> policyMix; |
| 3928 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3929 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3930 | address.string()); |
| 3931 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3932 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3933 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3934 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3935 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 3936 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3937 | // no duplicated output for direct outputs and |
| 3938 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3939 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3940 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3941 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3942 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3943 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3944 | //TODO: configure audio effect output stage here |
| 3945 | |
| 3946 | // 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] | 3947 | duplicatedOutput = |
| 3948 | mpClientInterface->openDuplicateOutput(output, |
| 3949 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3950 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3951 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3952 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 3953 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 3954 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 3955 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3956 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3957 | dupOutputDesc->mFormat = desc->mFormat; |
| 3958 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3959 | dupOutputDesc->mLatency = desc->mLatency; |
| 3960 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3961 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3962 | } else { |
| 3963 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3964 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3965 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3966 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3967 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3968 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3969 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3970 | } |
| 3971 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3972 | } else { |
| 3973 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3974 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3975 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3976 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3977 | profiles.removeAt(profile_index); |
| 3978 | profile_index--; |
| 3979 | } else { |
| 3980 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3981 | // Load digital format info only for digital devices |
| 3982 | if (audio_device_is_digital(device)) { |
| 3983 | devDesc->importAudioPort(profile); |
| 3984 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3985 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3986 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3987 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3988 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3989 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3990 | NULL/*patch handle*/, address.string()); |
| 3991 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3992 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3993 | } |
| 3994 | } |
| 3995 | |
| 3996 | if (profiles.isEmpty()) { |
| 3997 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3998 | return BAD_VALUE; |
| 3999 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4000 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4001 | // check if one opened output is not needed any more after disconnecting one device |
| 4002 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4003 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4004 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4005 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4006 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4007 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4008 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4009 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4010 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4011 | mOutputs.keyAt(i)); |
| 4012 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4013 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4014 | } |
| 4015 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4016 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4017 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4018 | { |
| 4019 | if (mHwModules[i]->mHandle == 0) { |
| 4020 | continue; |
| 4021 | } |
| 4022 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 4023 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4024 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4025 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4026 | ALOGV("checkOutputsForDevice(): " |
| 4027 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4028 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4029 | } |
| 4030 | } |
| 4031 | } |
| 4032 | } |
| 4033 | return NO_ERROR; |
| 4034 | } |
| 4035 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4036 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4037 | audio_policy_dev_state_t state, |
| 4038 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4039 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4040 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4041 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4042 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4043 | |
| 4044 | if (audio_device_is_digital(device)) { |
| 4045 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4046 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4047 | } |
| 4048 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4049 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4050 | // first list already open inputs that can be routed to this device |
| 4051 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4052 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4053 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4054 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4055 | inputs.add(mInputs.keyAt(input_index)); |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4060 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4061 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 4062 | { |
| 4063 | if (mHwModules[module_idx]->mHandle == 0) { |
| 4064 | continue; |
| 4065 | } |
| 4066 | for (size_t profile_index = 0; |
| 4067 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 4068 | profile_index++) |
| 4069 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4070 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 4071 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4072 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4073 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4074 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4075 | profiles.add(profile); |
| 4076 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 4077 | profile_index, module_idx); |
| 4078 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4079 | } |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4084 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4085 | return BAD_VALUE; |
| 4086 | } |
| 4087 | |
| 4088 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4089 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4090 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4091 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4092 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4093 | // nothing to do if one input is already opened for this profile |
| 4094 | size_t input_index; |
| 4095 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4096 | desc = mInputs.valueAt(input_index); |
| 4097 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4098 | if (audio_device_is_digital(device)) { |
| 4099 | devDesc->importAudioPort(profile); |
| 4100 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4101 | break; |
| 4102 | } |
| 4103 | } |
| 4104 | if (input_index != mInputs.size()) { |
| 4105 | continue; |
| 4106 | } |
| 4107 | |
| 4108 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 4109 | desc = new AudioInputDescriptor(profile); |
| 4110 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4111 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4112 | config.sample_rate = desc->mSamplingRate; |
| 4113 | config.channel_mask = desc->mChannelMask; |
| 4114 | config.format = desc->mFormat; |
| 4115 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4116 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4117 | &input, |
| 4118 | &config, |
| 4119 | &desc->mDevice, |
| 4120 | address, |
| 4121 | AUDIO_SOURCE_MIC, |
| 4122 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4123 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4124 | if (status == NO_ERROR) { |
| 4125 | desc->mSamplingRate = config.sample_rate; |
| 4126 | desc->mChannelMask = config.channel_mask; |
| 4127 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4128 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4129 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4130 | char *param = audio_device_address_to_parameter(device, address); |
| 4131 | mpClientInterface->setParameters(input, String8(param)); |
| 4132 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4133 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4134 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4135 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4136 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 4137 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4138 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
| 4141 | if (input != 0) { |
| 4142 | addInput(input, desc); |
| 4143 | } |
| 4144 | } // endif input != 0 |
| 4145 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4146 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4147 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4148 | profiles.removeAt(profile_index); |
| 4149 | profile_index--; |
| 4150 | } else { |
| 4151 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4152 | if (audio_device_is_digital(device)) { |
| 4153 | devDesc->importAudioPort(profile); |
| 4154 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4155 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4156 | } |
| 4157 | } // end scan profiles |
| 4158 | |
| 4159 | if (profiles.isEmpty()) { |
| 4160 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4161 | return BAD_VALUE; |
| 4162 | } |
| 4163 | } else { |
| 4164 | // Disconnect |
| 4165 | // check if one opened input is not needed any more after disconnecting one device |
| 4166 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4167 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4168 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4169 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4170 | mInputs.keyAt(input_index)); |
| 4171 | inputs.add(mInputs.keyAt(input_index)); |
| 4172 | } |
| 4173 | } |
| 4174 | // Clear any profiles associated with the disconnected device. |
| 4175 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4176 | if (mHwModules[module_index]->mHandle == 0) { |
| 4177 | continue; |
| 4178 | } |
| 4179 | for (size_t profile_index = 0; |
| 4180 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4181 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4182 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4183 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4184 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4185 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4186 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | } |
| 4190 | } // end disconnect |
| 4191 | |
| 4192 | return NO_ERROR; |
| 4193 | } |
| 4194 | |
| 4195 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4196 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4197 | { |
| 4198 | ALOGV("closeOutput(%d)", output); |
| 4199 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4200 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4201 | if (outputDesc == NULL) { |
| 4202 | ALOGW("closeOutput() unknown output %d", output); |
| 4203 | return; |
| 4204 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4205 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4206 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4207 | // look for duplicated outputs connected to the output being removed. |
| 4208 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4209 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4210 | if (dupOutputDesc->isDuplicated() && |
| 4211 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4212 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4213 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4214 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4215 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4216 | } else { |
| 4217 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4218 | } |
| 4219 | // As all active tracks on duplicated output will be deleted, |
| 4220 | // and as they were also referenced on the other output, the reference |
| 4221 | // count for their stream type must be adjusted accordingly on |
| 4222 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4223 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4225 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4226 | } |
| 4227 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4228 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4229 | |
| 4230 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4231 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4232 | } |
| 4233 | } |
| 4234 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4235 | nextAudioPortGeneration(); |
| 4236 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4237 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4238 | if (index >= 0) { |
| 4239 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4240 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4241 | mAudioPatches.removeItemsAt(index); |
| 4242 | mpClientInterface->onAudioPatchListUpdate(); |
| 4243 | } |
| 4244 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4245 | AudioParameter param; |
| 4246 | param.add(String8("closing"), String8("true")); |
| 4247 | mpClientInterface->setParameters(output, param.toString()); |
| 4248 | |
| 4249 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4250 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4251 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4255 | { |
| 4256 | ALOGV("closeInput(%d)", input); |
| 4257 | |
| 4258 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4259 | if (inputDesc == NULL) { |
| 4260 | ALOGW("closeInput() unknown input %d", input); |
| 4261 | return; |
| 4262 | } |
| 4263 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4264 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4265 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4266 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4267 | if (index >= 0) { |
| 4268 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4269 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4270 | mAudioPatches.removeItemsAt(index); |
| 4271 | mpClientInterface->onAudioPatchListUpdate(); |
| 4272 | } |
| 4273 | |
| 4274 | mpClientInterface->closeInput(input); |
| 4275 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4278 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4279 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4280 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4281 | { |
| 4282 | SortedVector<audio_io_handle_t> outputs; |
| 4283 | |
| 4284 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4285 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4286 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4287 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4288 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4289 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4290 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4291 | outputs.add(openOutputs.keyAt(i)); |
| 4292 | } |
| 4293 | } |
| 4294 | return outputs; |
| 4295 | } |
| 4296 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4297 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4298 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4299 | { |
| 4300 | if (outputs1.size() != outputs2.size()) { |
| 4301 | return false; |
| 4302 | } |
| 4303 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4304 | if (outputs1[i] != outputs2[i]) { |
| 4305 | return false; |
| 4306 | } |
| 4307 | } |
| 4308 | return true; |
| 4309 | } |
| 4310 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4311 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4312 | { |
| 4313 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4314 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4315 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4316 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4317 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4318 | // also take into account external policy-related changes: add all outputs which are |
| 4319 | // associated with policies in the "before" and "after" output vectors |
| 4320 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4321 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4322 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4323 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4324 | srcOutputs.add(desc->mIoHandle); |
| 4325 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4326 | } |
| 4327 | } |
| 4328 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4329 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4330 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4331 | dstOutputs.add(desc->mIoHandle); |
| 4332 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4333 | } |
| 4334 | } |
| 4335 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4336 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4337 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4338 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4339 | // mute strategy while moving tracks from one output to another |
| 4340 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4341 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4342 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4343 | setStrategyMute(strategy, true, desc); |
| 4344 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4345 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4346 | sp<AudioSourceDescriptor> source = |
| 4347 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4348 | if (source != 0){ |
| 4349 | connectAudioSource(source); |
| 4350 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4351 | } |
| 4352 | |
| 4353 | // Move effects associated to this strategy from previous output to new output |
| 4354 | if (strategy == STRATEGY_MEDIA) { |
| 4355 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 4356 | SortedVector<audio_io_handle_t> moved; |
| 4357 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4358 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 4359 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 4360 | effectDesc->mIo != fxOutput) { |
| 4361 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4362 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 4363 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4364 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4366 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4367 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4368 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4369 | } |
| 4370 | } |
| 4371 | } |
| 4372 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4373 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4374 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4375 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | } |
| 4379 | } |
| 4380 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4381 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4382 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4383 | 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] | 4384 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4386 | 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] | 4387 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4388 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4389 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4390 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4391 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4392 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4393 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4394 | } |
| 4395 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4396 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4397 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4398 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4399 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4400 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4401 | return; |
| 4402 | } |
| 4403 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4404 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4405 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4406 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4407 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4408 | |
| 4409 | // if suspended, restore A2DP output if: |
| 4410 | // ((SCO device is NOT connected) || |
| 4411 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4412 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4413 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4414 | // if not suspended, suspend A2DP output if: |
| 4415 | // (SCO device is connected) && |
| 4416 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4417 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4418 | // |
| 4419 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4420 | if (!isScoConnected || |
| 4421 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4422 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4423 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4424 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4425 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4426 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4427 | |
| 4428 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4429 | mA2dpSuspended = false; |
| 4430 | } |
| 4431 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4432 | if (isScoConnected && |
| 4433 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4434 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4435 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4436 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4437 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4438 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4439 | |
| 4440 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4441 | mA2dpSuspended = true; |
| 4442 | } |
| 4443 | } |
| 4444 | } |
| 4445 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4446 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4447 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4448 | { |
| 4449 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4450 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4451 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4452 | if (index >= 0) { |
| 4453 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4454 | if (patchDesc->mUid != mUidCached) { |
| 4455 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4456 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4457 | return outputDesc->device(); |
| 4458 | } |
| 4459 | } |
| 4460 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4461 | // 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] | 4462 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4463 | // use device for strategy enforced audible |
| 4464 | // 2: we are in call or the strategy phone is active on the output: |
| 4465 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4466 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4467 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4468 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4469 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4470 | // 5: the strategy accessibility is active on the output: |
| 4471 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4472 | // 6: the strategy "respectful" sonification is active on the output: |
| 4473 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4474 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4475 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4476 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4477 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4478 | // 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] | 4479 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4480 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4481 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4482 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4483 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4484 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4485 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4486 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4487 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4488 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4489 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4490 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4491 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4492 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4493 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4494 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4495 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4496 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4497 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4498 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4499 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4500 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4501 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4502 | } |
| 4503 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4504 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4505 | return device; |
| 4506 | } |
| 4507 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4508 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4509 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4510 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4511 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4512 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4513 | if (index >= 0) { |
| 4514 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4515 | if (patchDesc->mUid != mUidCached) { |
| 4516 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4517 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4518 | return inputDesc->mDevice; |
| 4519 | } |
| 4520 | } |
| 4521 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4522 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
| 4523 | if (isInCall()) { |
| 4524 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4525 | } else if (source != AUDIO_SOURCE_DEFAULT) { |
| 4526 | device = getDeviceAndMixForInputSource(source); |
| 4527 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4528 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4529 | return device; |
| 4530 | } |
| 4531 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4532 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4533 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4534 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4535 | } |
| 4536 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4537 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4538 | return (uint32_t)getStrategy(stream); |
| 4539 | } |
| 4540 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4541 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4542 | // By checking the range of stream before calling getStrategy, we avoid |
| 4543 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4544 | // 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] | 4545 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4546 | return AUDIO_DEVICE_NONE; |
| 4547 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4548 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4549 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4550 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4551 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4552 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4553 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4554 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4555 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4556 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4557 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4558 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4559 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4560 | curDevices |= outputDesc->device(); |
| 4561 | } |
| 4562 | } |
| 4563 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4564 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4565 | |
| 4566 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4567 | and doesn't really need to.*/ |
| 4568 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4569 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4570 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4571 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4572 | return devices; |
| 4573 | } |
| 4574 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4575 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4576 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4577 | 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] | 4578 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4579 | } |
| 4580 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4581 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4582 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4583 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4584 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4585 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4586 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4587 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4588 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4589 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4590 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4591 | } |
| 4592 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4593 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4594 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4595 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4597 | updateDevicesAndOutputs(); |
| 4598 | break; |
| 4599 | default: |
| 4600 | break; |
| 4601 | } |
| 4602 | } |
| 4603 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4604 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4605 | |
| 4606 | // skip beacon mute management if a dedicated TTS output is available |
| 4607 | if (mTtsOutputAvailable) { |
| 4608 | return 0; |
| 4609 | } |
| 4610 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4611 | switch(event) { |
| 4612 | case STARTING_OUTPUT: |
| 4613 | mBeaconMuteRefCount++; |
| 4614 | break; |
| 4615 | case STOPPING_OUTPUT: |
| 4616 | if (mBeaconMuteRefCount > 0) { |
| 4617 | mBeaconMuteRefCount--; |
| 4618 | } |
| 4619 | break; |
| 4620 | case STARTING_BEACON: |
| 4621 | mBeaconPlayingRefCount++; |
| 4622 | break; |
| 4623 | case STOPPING_BEACON: |
| 4624 | if (mBeaconPlayingRefCount > 0) { |
| 4625 | mBeaconPlayingRefCount--; |
| 4626 | } |
| 4627 | break; |
| 4628 | } |
| 4629 | |
| 4630 | if (mBeaconMuteRefCount > 0) { |
| 4631 | // any playback causes beacon to be muted |
| 4632 | return setBeaconMute(true); |
| 4633 | } else { |
| 4634 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4635 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4636 | } |
| 4637 | } |
| 4638 | |
| 4639 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4640 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4641 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4642 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4643 | if (mBeaconMuted != mute) { |
| 4644 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4645 | ALOGV("\t muting %d", mute); |
| 4646 | uint32_t maxLatency = 0; |
| 4647 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4648 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4649 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4650 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4651 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4652 | const uint32_t latency = desc->latency() * 2; |
| 4653 | if (latency > maxLatency) { |
| 4654 | maxLatency = latency; |
| 4655 | } |
| 4656 | } |
| 4657 | mBeaconMuted = mute; |
| 4658 | return maxLatency; |
| 4659 | } |
| 4660 | return 0; |
| 4661 | } |
| 4662 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4663 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4664 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4665 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4666 | // Routing |
| 4667 | // see if we have an explicit route |
| 4668 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4669 | // (getStrategy(stream)). |
| 4670 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4671 | // and activity count is non-zero |
| 4672 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4673 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4674 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4675 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4676 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4677 | return route->mDeviceDescriptor->type(); |
| 4678 | } |
| 4679 | } |
| 4680 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4681 | if (fromCache) { |
| 4682 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4683 | strategy, mDeviceForStrategy[strategy]); |
| 4684 | return mDeviceForStrategy[strategy]; |
| 4685 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4686 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4687 | } |
| 4688 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4689 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4690 | { |
| 4691 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4692 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4693 | } |
| 4694 | mPreviousOutputs = mOutputs; |
| 4695 | } |
| 4696 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4697 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4698 | audio_devices_t prevDevice, |
| 4699 | uint32_t delayMs) |
| 4700 | { |
| 4701 | // mute/unmute strategies using an incompatible device combination |
| 4702 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4703 | // if unmuting, unmute only after the specified delay |
| 4704 | if (outputDesc->isDuplicated()) { |
| 4705 | return 0; |
| 4706 | } |
| 4707 | |
| 4708 | uint32_t muteWaitMs = 0; |
| 4709 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4710 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4711 | |
| 4712 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4713 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4714 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4715 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4716 | bool doMute = false; |
| 4717 | |
| 4718 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4719 | doMute = true; |
| 4720 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4721 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4722 | doMute = true; |
| 4723 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4724 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4725 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4727 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 | // skip output if it does not share any device with current output |
| 4729 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4730 | == AUDIO_DEVICE_NONE) { |
| 4731 | continue; |
| 4732 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4733 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4734 | mute ? "muting" : "unmuting", i, curDevice); |
| 4735 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4736 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4737 | if (mute) { |
| 4738 | // FIXME: should not need to double latency if volume could be applied |
| 4739 | // immediately by the audioflinger mixer. We must account for the delay |
| 4740 | // between now and the next time the audioflinger thread for this output |
| 4741 | // will process a buffer (which corresponds to one buffer size, |
| 4742 | // usually 1/2 or 1/4 of the latency). |
| 4743 | if (muteWaitMs < desc->latency() * 2) { |
| 4744 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4745 | } |
| 4746 | } |
| 4747 | } |
| 4748 | } |
| 4749 | } |
| 4750 | } |
| 4751 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4752 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4753 | // different per device volumes |
| 4754 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4755 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4756 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4757 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4758 | if (muteWaitMs < tempMuteWaitMs) { |
| 4759 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4760 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4761 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4762 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4763 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4764 | // make sure that we do not start the temporary mute period too early in case of |
| 4765 | // delayed device change |
| 4766 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4767 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4768 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4769 | } |
| 4770 | } |
| 4771 | } |
| 4772 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4773 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4774 | if (muteWaitMs > delayMs) { |
| 4775 | muteWaitMs -= delayMs; |
| 4776 | usleep(muteWaitMs * 1000); |
| 4777 | return muteWaitMs; |
| 4778 | } |
| 4779 | return 0; |
| 4780 | } |
| 4781 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4782 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4783 | audio_devices_t device, |
| 4784 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4785 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4786 | audio_patch_handle_t *patchHandle, |
| 4787 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4788 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4789 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4790 | AudioParameter param; |
| 4791 | uint32_t muteWaitMs; |
| 4792 | |
| 4793 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4794 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4795 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4796 | return muteWaitMs; |
| 4797 | } |
| 4798 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4799 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4800 | if ((device != AUDIO_DEVICE_NONE) && |
| 4801 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4802 | return 0; |
| 4803 | } |
| 4804 | |
| 4805 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4806 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4807 | |
| 4808 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4809 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4810 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4811 | |
| 4812 | if (device != AUDIO_DEVICE_NONE) { |
| 4813 | outputDesc->mDevice = device; |
| 4814 | } |
| 4815 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4816 | |
| 4817 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4818 | // the requested device is AUDIO_DEVICE_NONE |
| 4819 | // OR the requested device is the same as current device |
| 4820 | // AND force is not specified |
| 4821 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4822 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4823 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4824 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4825 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4826 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4827 | return muteWaitMs; |
| 4828 | } |
| 4829 | |
| 4830 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4831 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4832 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4833 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4834 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4835 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4836 | DeviceVector deviceList; |
| 4837 | if ((address == NULL) || (strlen(address) == 0)) { |
| 4838 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 4839 | } else { |
| 4840 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 4841 | } |
| 4842 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4843 | if (!deviceList.isEmpty()) { |
| 4844 | struct audio_patch patch; |
| 4845 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4846 | patch.num_sources = 1; |
| 4847 | patch.num_sinks = 0; |
| 4848 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4849 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4850 | patch.num_sinks++; |
| 4851 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4852 | ssize_t index; |
| 4853 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4854 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4855 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4856 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4857 | } |
| 4858 | sp< AudioPatch> patchDesc; |
| 4859 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4860 | if (index >= 0) { |
| 4861 | patchDesc = mAudioPatches.valueAt(index); |
| 4862 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4863 | } |
| 4864 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4865 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4866 | &afPatchHandle, |
| 4867 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4868 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4869 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4870 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4871 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4872 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4873 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4874 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4875 | } else { |
| 4876 | patchDesc->mPatch = patch; |
| 4877 | } |
| 4878 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4879 | if (patchHandle) { |
| 4880 | *patchHandle = patchDesc->mHandle; |
| 4881 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4882 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4883 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4884 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4885 | } |
| 4886 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4887 | |
| 4888 | // inform all input as well |
| 4889 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4890 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4891 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4892 | AudioParameter inputCmd = AudioParameter(); |
| 4893 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4894 | inputDescriptor->mIoHandle, device); |
| 4895 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4896 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4897 | inputCmd.toString(), |
| 4898 | delayMs); |
| 4899 | } |
| 4900 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4901 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4902 | |
| 4903 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4904 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4905 | |
| 4906 | return muteWaitMs; |
| 4907 | } |
| 4908 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4909 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4910 | int delayMs, |
| 4911 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4912 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4913 | ssize_t index; |
| 4914 | if (patchHandle) { |
| 4915 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4916 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4917 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4918 | } |
| 4919 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4920 | return INVALID_OPERATION; |
| 4921 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4922 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4923 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4924 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4925 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4926 | removeAudioPatch(patchDesc->mHandle); |
| 4927 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4928 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4929 | return status; |
| 4930 | } |
| 4931 | |
| 4932 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4933 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4934 | bool force, |
| 4935 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4936 | { |
| 4937 | status_t status = NO_ERROR; |
| 4938 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4939 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4940 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4941 | inputDesc->mDevice = device; |
| 4942 | |
| 4943 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4944 | if (!deviceList.isEmpty()) { |
| 4945 | struct audio_patch patch; |
| 4946 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4947 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4948 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4949 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 4950 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4951 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4952 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4953 | patch.num_sinks = 1; |
| 4954 | //only one input device for now |
| 4955 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4956 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4957 | ssize_t index; |
| 4958 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4959 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4960 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4961 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4962 | } |
| 4963 | sp< AudioPatch> patchDesc; |
| 4964 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4965 | if (index >= 0) { |
| 4966 | patchDesc = mAudioPatches.valueAt(index); |
| 4967 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4968 | } |
| 4969 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4970 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4971 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4972 | 0); |
| 4973 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4974 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4975 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4976 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4977 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4978 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4979 | } else { |
| 4980 | patchDesc->mPatch = patch; |
| 4981 | } |
| 4982 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4983 | if (patchHandle) { |
| 4984 | *patchHandle = patchDesc->mHandle; |
| 4985 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4986 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4987 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4988 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4989 | } |
| 4990 | } |
| 4991 | } |
| 4992 | return status; |
| 4993 | } |
| 4994 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4995 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4996 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4997 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4998 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4999 | ssize_t index; |
| 5000 | if (patchHandle) { |
| 5001 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5002 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5003 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5004 | } |
| 5005 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5006 | return INVALID_OPERATION; |
| 5007 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5008 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5009 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5010 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5011 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5012 | removeAudioPatch(patchDesc->mHandle); |
| 5013 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5014 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5015 | return status; |
| 5016 | } |
| 5017 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5018 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5019 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5020 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5021 | audio_format_t& format, |
| 5022 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5023 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5024 | { |
| 5025 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5026 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5027 | // |
| 5028 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5029 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5030 | |
| 5031 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 5032 | { |
| 5033 | if (mHwModules[i]->mHandle == 0) { |
| 5034 | continue; |
| 5035 | } |
| 5036 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 5037 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5038 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5039 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5040 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5041 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5042 | format, |
| 5043 | &format /*updatedFormat*/, |
| 5044 | channelMask, |
| 5045 | &channelMask /*updatedChannelMask*/, |
| 5046 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5047 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5048 | return profile; |
| 5049 | } |
| 5050 | } |
| 5051 | } |
| 5052 | return NULL; |
| 5053 | } |
| 5054 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5055 | |
| 5056 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5057 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5058 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5059 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5060 | audio_devices_t selectedDeviceFromMix = |
| 5061 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5062 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5063 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5064 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5065 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5066 | return getDeviceForInputSource(inputSource); |
| 5067 | } |
| 5068 | |
| 5069 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5070 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5071 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5072 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5073 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5074 | return route->mDeviceDescriptor->type(); |
| 5075 | } |
| 5076 | } |
| 5077 | |
| 5078 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5079 | } |
| 5080 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5081 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5082 | int index, |
| 5083 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5084 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5085 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5086 | |
| 5087 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5088 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5089 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5090 | // the ringtone volume |
| 5091 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5092 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5093 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5094 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5095 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5096 | } |
| 5097 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5098 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5099 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5100 | // - always attenuate notifications volume by 6dB |
| 5101 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5102 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5103 | // - if music is playing, always limit the volume to current music volume, |
| 5104 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5105 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5106 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5107 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5108 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 5109 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 5110 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5111 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5112 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5113 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5114 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5115 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5116 | // when the phone is ringing we must consider that music could have been paused just before |
| 5117 | // by the music application and behave as if music was active if the last music track was |
| 5118 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5119 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5120 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5121 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5122 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5123 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5124 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5125 | musicDevice), |
| 5126 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5127 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5128 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5129 | if (volumeDB > minVolDB) { |
| 5130 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5131 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5132 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5133 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5134 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5135 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5136 | // intended to be played |
| 5137 | if ((volumeDB > -96.0f) && |
| 5138 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5139 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5140 | stream, device, |
| 5141 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5142 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5143 | } |
| 5144 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5145 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5146 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5147 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5148 | } |
| 5149 | } |
| 5150 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5151 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5152 | } |
| 5153 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5154 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5155 | int index, |
| 5156 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5157 | audio_devices_t device, |
| 5158 | int delayMs, |
| 5159 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5160 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5161 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5162 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5163 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5164 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5165 | return NO_ERROR; |
| 5166 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5167 | audio_policy_forced_cfg_t forceUseForComm = |
| 5168 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5169 | // 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] | 5170 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5171 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5172 | 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] | 5173 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5174 | return INVALID_OPERATION; |
| 5175 | } |
| 5176 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5177 | if (device == AUDIO_DEVICE_NONE) { |
| 5178 | device = outputDesc->device(); |
| 5179 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5180 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5181 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5182 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5183 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5184 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5185 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5186 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5187 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5188 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5189 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5190 | float voiceVolume; |
| 5191 | // 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] | 5192 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5193 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5194 | } else { |
| 5195 | voiceVolume = 1.0; |
| 5196 | } |
| 5197 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5198 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5199 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5200 | mLastVoiceVolume = voiceVolume; |
| 5201 | } |
| 5202 | } |
| 5203 | |
| 5204 | return NO_ERROR; |
| 5205 | } |
| 5206 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5207 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5208 | audio_devices_t device, |
| 5209 | int delayMs, |
| 5210 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5211 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5212 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5213 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5214 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5215 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5216 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5217 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5218 | device, |
| 5219 | delayMs, |
| 5220 | force); |
| 5221 | } |
| 5222 | } |
| 5223 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5224 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5225 | bool on, |
| 5226 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5227 | int delayMs, |
| 5228 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5229 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5230 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5231 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5232 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5233 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5234 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5235 | } |
| 5236 | } |
| 5237 | } |
| 5238 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5239 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5240 | bool on, |
| 5241 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5242 | int delayMs, |
| 5243 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5244 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5245 | if (device == AUDIO_DEVICE_NONE) { |
| 5246 | device = outputDesc->device(); |
| 5247 | } |
| 5248 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5249 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5250 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5251 | |
| 5252 | if (on) { |
| 5253 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5254 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5255 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5256 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5257 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5258 | } |
| 5259 | } |
| 5260 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5261 | outputDesc->mMuteCount[stream]++; |
| 5262 | } else { |
| 5263 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5264 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5265 | return; |
| 5266 | } |
| 5267 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5268 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5269 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5270 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5271 | device, |
| 5272 | delayMs); |
| 5273 | } |
| 5274 | } |
| 5275 | } |
| 5276 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5277 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5278 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5279 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5280 | if(!hasPrimaryOutput()) { |
| 5281 | return; |
| 5282 | } |
| 5283 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5284 | // if the stream pertains to sonification strategy and we are in call we must |
| 5285 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5286 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5287 | // interfere with the device used for phone strategy |
| 5288 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5289 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5290 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5291 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5292 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5293 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5294 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5295 | stream, starting, outputDesc->mDevice, stateChange); |
| 5296 | if (outputDesc->mRefCount[stream]) { |
| 5297 | int muteCount = 1; |
| 5298 | if (stateChange) { |
| 5299 | muteCount = outputDesc->mRefCount[stream]; |
| 5300 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5301 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5302 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5303 | for (int i = 0; i < muteCount; i++) { |
| 5304 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5305 | } |
| 5306 | } else { |
| 5307 | ALOGV("handleIncallSonification() high visibility"); |
| 5308 | if (outputDesc->device() & |
| 5309 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5310 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5311 | for (int i = 0; i < muteCount; i++) { |
| 5312 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5313 | } |
| 5314 | } |
| 5315 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5316 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5317 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5318 | } else { |
| 5319 | mpClientInterface->stopTone(); |
| 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | } |
| 5324 | } |
| 5325 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5326 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5327 | { |
| 5328 | // flags to stream type mapping |
| 5329 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5330 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5331 | } |
| 5332 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5333 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5334 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5335 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5336 | return AUDIO_STREAM_TTS; |
| 5337 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5338 | |
| 5339 | // usage to stream type mapping |
| 5340 | switch (attr->usage) { |
| 5341 | case AUDIO_USAGE_MEDIA: |
| 5342 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5343 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5344 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5345 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5346 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5347 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5348 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5349 | return AUDIO_STREAM_SYSTEM; |
| 5350 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5351 | return AUDIO_STREAM_VOICE_CALL; |
| 5352 | |
| 5353 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5354 | return AUDIO_STREAM_DTMF; |
| 5355 | |
| 5356 | case AUDIO_USAGE_ALARM: |
| 5357 | return AUDIO_STREAM_ALARM; |
| 5358 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5359 | return AUDIO_STREAM_RING; |
| 5360 | |
| 5361 | case AUDIO_USAGE_NOTIFICATION: |
| 5362 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5363 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5364 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5365 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5366 | return AUDIO_STREAM_NOTIFICATION; |
| 5367 | |
| 5368 | case AUDIO_USAGE_UNKNOWN: |
| 5369 | default: |
| 5370 | return AUDIO_STREAM_MUSIC; |
| 5371 | } |
| 5372 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5373 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5374 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5375 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5376 | // has flags that map to a strategy? |
| 5377 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5378 | return true; |
| 5379 | } |
| 5380 | |
| 5381 | // has known usage? |
| 5382 | switch (paa->usage) { |
| 5383 | case AUDIO_USAGE_UNKNOWN: |
| 5384 | case AUDIO_USAGE_MEDIA: |
| 5385 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5386 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5387 | case AUDIO_USAGE_ALARM: |
| 5388 | case AUDIO_USAGE_NOTIFICATION: |
| 5389 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5390 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5391 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5392 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5393 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5394 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5395 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5396 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5397 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5398 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5399 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5400 | break; |
| 5401 | default: |
| 5402 | return false; |
| 5403 | } |
| 5404 | return true; |
| 5405 | } |
| 5406 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5407 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5408 | routing_strategy strategy, uint32_t inPastMs, |
| 5409 | nsecs_t sysTime) const |
| 5410 | { |
| 5411 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5412 | sysTime = systemTime(); |
| 5413 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5414 | 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] | 5415 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5416 | (NUM_STRATEGIES == strategy)) && |
| 5417 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5418 | return true; |
| 5419 | } |
| 5420 | } |
| 5421 | return false; |
| 5422 | } |
| 5423 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5424 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5425 | { |
| 5426 | return mEngine->getForceUse(usage); |
| 5427 | } |
| 5428 | |
| 5429 | bool AudioPolicyManager::isInCall() |
| 5430 | { |
| 5431 | return isStateInCall(mEngine->getPhoneState()); |
| 5432 | } |
| 5433 | |
| 5434 | bool AudioPolicyManager::isStateInCall(int state) |
| 5435 | { |
| 5436 | return is_state_in_call(state); |
| 5437 | } |
| 5438 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5439 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5440 | { |
| 5441 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5442 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5443 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5444 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5445 | stopAudioSource(sourceDesc->getHandle()); |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5450 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5451 | bool release = false; |
| 5452 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5453 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5454 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5455 | source->ext.device.type == deviceDesc->type()) { |
| 5456 | release = true; |
| 5457 | } |
| 5458 | } |
| 5459 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5460 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5461 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5462 | sink->ext.device.type == deviceDesc->type()) { |
| 5463 | release = true; |
| 5464 | } |
| 5465 | } |
| 5466 | if (release) { |
| 5467 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5468 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5469 | } |
| 5470 | } |
| 5471 | } |
| 5472 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5473 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5474 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5475 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5476 | // TODO Set this based on Config properties. |
| 5477 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5478 | |
| 5479 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5480 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5481 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5482 | |
| 5483 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5484 | bool supportsAC3 = false; |
| 5485 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5486 | bool supportsIEC61937 = false; |
| 5487 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5488 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5489 | switch (format) { |
| 5490 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5491 | supportsAC3 = true; |
| 5492 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5493 | case AUDIO_FORMAT_E_AC3: |
| 5494 | case AUDIO_FORMAT_DTS: |
| 5495 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5496 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5497 | break; |
| 5498 | case AUDIO_FORMAT_IEC61937: |
| 5499 | supportsIEC61937 = true; |
| 5500 | break; |
| 5501 | default: |
| 5502 | break; |
| 5503 | } |
| 5504 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5505 | |
| 5506 | // Modify formats based on surround preferences. |
| 5507 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5508 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5509 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5510 | // Remove surround sound related formats. |
| 5511 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5512 | audio_format_t format = formats[formatIndex]; |
| 5513 | switch(format) { |
| 5514 | case AUDIO_FORMAT_AC3: |
| 5515 | case AUDIO_FORMAT_E_AC3: |
| 5516 | case AUDIO_FORMAT_DTS: |
| 5517 | case AUDIO_FORMAT_DTS_HD: |
| 5518 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5519 | formats.removeAt(formatIndex); |
| 5520 | break; |
| 5521 | default: |
| 5522 | formatIndex++; // keep it |
| 5523 | break; |
| 5524 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5525 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5526 | supportsAC3 = false; |
| 5527 | supportsOtherSurround = false; |
| 5528 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5529 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5530 | } else { // AUTO or ALWAYS |
| 5531 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5532 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5533 | && !supportsAC3) { |
| 5534 | formats.add(AUDIO_FORMAT_AC3); |
| 5535 | supportsAC3 = true; |
| 5536 | } |
| 5537 | |
| 5538 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5539 | // This assumes that if any of these formats are reported by the HAL |
| 5540 | // then the report is valid and should not be modified. |
| 5541 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5542 | && !supportsOtherSurround) { |
| 5543 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5544 | formats.add(AUDIO_FORMAT_DTS); |
| 5545 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5546 | supportsOtherSurround = true; |
| 5547 | } |
| 5548 | |
| 5549 | // Add support for IEC61937 if any raw surround supported. |
| 5550 | // The HAL could do this but add it here, just in case. |
| 5551 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5552 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5553 | supportsIEC61937 = true; |
| 5554 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5555 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5556 | } |
| 5557 | |
| 5558 | // Modify the list of channel masks supported. |
| 5559 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5560 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5561 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5562 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5563 | |
| 5564 | // If NEVER, then remove support for channelMasks > stereo. |
| 5565 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5566 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5567 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5568 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5569 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5570 | channelMasks.removeAt(maskIndex); |
| 5571 | } else { |
| 5572 | maskIndex++; |
| 5573 | } |
| 5574 | } |
| 5575 | // If ALWAYS, then make sure we at least support 5.1 |
| 5576 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5577 | bool supports5dot1 = false; |
| 5578 | // Are there any channel masks that can be considered "surround"? |
| 5579 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5580 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5581 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5582 | supports5dot1 = true; |
| 5583 | break; |
| 5584 | } |
| 5585 | } |
| 5586 | // If not then add 5.1 support. |
| 5587 | if (!supports5dot1) { |
| 5588 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5589 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5590 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5591 | } |
| 5592 | } |
| 5593 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5594 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5595 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5596 | AudioProfileVector &profiles) |
| 5597 | { |
| 5598 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5599 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5600 | // Format MUST be checked first to update the list of AudioProfile |
| 5601 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5602 | reply = mpClientInterface->getParameters( |
| 5603 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5604 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5605 | AudioParameter repliedParameters(reply); |
| 5606 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5607 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5608 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5609 | return; |
| 5610 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5611 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5612 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5613 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5614 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5615 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5616 | } |
| 5617 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5618 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5619 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5620 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5621 | ChannelsVector channelMasks; |
| 5622 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5623 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5624 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5625 | |
| 5626 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5627 | reply = mpClientInterface->getParameters( |
| 5628 | ioHandle, |
| 5629 | requestedParameters.toString() + ";" + |
| 5630 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5631 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5632 | AudioParameter repliedParameters(reply); |
| 5633 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5634 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5635 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5636 | } |
| 5637 | } |
| 5638 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5639 | reply = mpClientInterface->getParameters(ioHandle, |
| 5640 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5641 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5642 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5643 | AudioParameter repliedParameters(reply); |
| 5644 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5645 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5646 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5647 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5648 | filterSurroundChannelMasks(&channelMasks); |
| 5649 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5650 | } |
| 5651 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5652 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5653 | } |
| 5654 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5655 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5656 | }; // namespace android |