Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 17 | #define LOG_TAG "APM_AudioPolicyManager" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | //#define VERY_VERBOSE_LOGGING |
| 21 | #ifdef VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 |
| 28 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 29 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 30 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 31 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 32 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 33 | #include <AudioPolicyManagerInterface.h> |
| 34 | #include <AudioPolicyEngineInstance.h> |
Mathias Agopian | 05d19b0 | 2017-02-28 16:28:19 -0800 | [diff] [blame] | 35 | #include <cutils/atomic.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 38 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 39 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | #include <soundtrigger/SoundTrigger.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 41 | #include <system/audio.h> |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 42 | #include <audio_policy_conf.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 43 | #include "AudioPolicyManager.h" |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #ifndef USE_XML_AUDIO_POLICY_CONF |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 45 | #include <ConfigParsingUtils.h> |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <StreamDescriptor.h> |
François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 47 | #endif |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 48 | #include <Serializer.h> |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 49 | #include "TypeConverter.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 50 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 52 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 53 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 54 | //FIXME: workaround for truncated touch sounds |
| 55 | // to be removed when the problem is handled by system UI |
| 56 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- |
| 58 | // AudioPolicyInterface implementation |
| 59 | // ---------------------------------------------------------------------------- |
| 60 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 61 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 62 | audio_policy_dev_state_t state, |
| 63 | const char *device_address, |
| 64 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 65 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 66 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 67 | } |
| 68 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 69 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, |
| 70 | audio_policy_dev_state_t state, |
| 71 | const String8 &device_address) |
| 72 | { |
| 73 | AudioParameter param(device_address); |
| 74 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 75 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 76 | param.addInt(key, device); |
| 77 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 78 | } |
| 79 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 80 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 81 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 82 | const char *device_address, |
| 83 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 84 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 85 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 86 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | |
| 88 | // connect/disconnect only 1 device at a time |
| 89 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 90 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 91 | sp<DeviceDescriptor> devDesc = |
| 92 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 93 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 94 | // handle output devices |
| 95 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 96 | SortedVector <audio_io_handle_t> outputs; |
| 97 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 99 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 100 | // save a copy of the opened output descriptors before any output is opened or closed |
| 101 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 102 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 103 | switch (state) |
| 104 | { |
| 105 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 106 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 107 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 108 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 109 | return INVALID_OPERATION; |
| 110 | } |
| 111 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 112 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 113 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 114 | index = mAvailableOutputDevices.add(devDesc); |
| 115 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 116 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 117 | if (module == 0) { |
| 118 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 119 | device); |
| 120 | mAvailableOutputDevices.remove(devDesc); |
| 121 | return INVALID_OPERATION; |
| 122 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 123 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 124 | } else { |
| 125 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 126 | } |
| 127 | |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 128 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic |
| 129 | // parameters on newly connected devices (instead of opening the outputs...) |
| 130 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 131 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 132 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 133 | mAvailableOutputDevices.remove(devDesc); |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 134 | |
| 135 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 136 | devDesc->mAddress); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 137 | return INVALID_OPERATION; |
| 138 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 139 | // Propagate device availability to Engine |
| 140 | mEngine->setDeviceConnectionState(devDesc, state); |
| 141 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 142 | // outputs should never be empty here |
| 143 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 144 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 145 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 146 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 147 | |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 148 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 149 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 150 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 151 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 153 | return INVALID_OPERATION; |
| 154 | } |
| 155 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 156 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 157 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 158 | // Send Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 159 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 160 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 163 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 164 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 165 | |
| 166 | // Propagate device availability to Engine |
| 167 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 168 | } break; |
| 169 | |
| 170 | default: |
| 171 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 172 | return BAD_VALUE; |
| 173 | } |
| 174 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 175 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 176 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 177 | checkA2dpSuspend(); |
| 178 | checkOutputForAllStrategies(); |
| 179 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 180 | if (!outputs.isEmpty()) { |
| 181 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 182 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | // close unused outputs after device disconnection or direct outputs that have been |
| 184 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 185 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 186 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 187 | (desc->mDirectOpenCount == 0))) { |
| 188 | closeOutput(outputs[i]); |
| 189 | } |
| 190 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 191 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 192 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | updateDevicesAndOutputs(); |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 196 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 197 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 198 | updateCallRouting(newDevice); |
| 199 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 201 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 202 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 203 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 204 | // do not force device change on duplicated output because if device is 0, it will |
| 205 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 206 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 207 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 208 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 209 | // always force when disconnecting (a non-duplicated device) |
| 210 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 211 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 212 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 215 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 216 | cleanUpForDevice(devDesc); |
| 217 | } |
| 218 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 219 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 220 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 221 | } // end if is output device |
| 222 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 | // handle input devices |
| 224 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 225 | SortedVector <audio_io_handle_t> inputs; |
| 226 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 227 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | switch (state) |
| 229 | { |
| 230 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 231 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 232 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 234 | return INVALID_OPERATION; |
| 235 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 236 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 237 | if (module == NULL) { |
| 238 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 239 | device); |
| 240 | return INVALID_OPERATION; |
| 241 | } |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 242 | |
| 243 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic |
| 244 | // parameters on newly connected devices (instead of opening the inputs...) |
| 245 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
| 246 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 247 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 248 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 249 | devDesc->mAddress); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 250 | return INVALID_OPERATION; |
| 251 | } |
| 252 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 253 | index = mAvailableInputDevices.add(devDesc); |
| 254 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 255 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 256 | } else { |
| 257 | return NO_MEMORY; |
| 258 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 259 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 260 | // Propagate device availability to Engine |
| 261 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 262 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | |
| 264 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 265 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 266 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 268 | return INVALID_OPERATION; |
| 269 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 270 | |
| 271 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 272 | |
| 273 | // Set Disconnect to HALs |
François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 274 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 275 | |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 276 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 277 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 278 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 279 | // Propagate device availability to Engine |
| 280 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 281 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 282 | |
| 283 | default: |
| 284 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 285 | return BAD_VALUE; |
| 286 | } |
| 287 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 288 | closeAllInputs(); |
Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 289 | // As the input device list can impact the output device selection, update |
| 290 | // getDeviceForStrategy() cache |
| 291 | updateDevicesAndOutputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 293 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 294 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 295 | updateCallRouting(newDevice); |
| 296 | } |
| 297 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 298 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { |
| 299 | cleanUpForDevice(devDesc); |
| 300 | } |
| 301 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 302 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 304 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 | |
| 306 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 307 | return BAD_VALUE; |
| 308 | } |
| 309 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 310 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 311 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | { |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 313 | sp<DeviceDescriptor> devDesc = |
| 314 | mHwModules.getDeviceDescriptor(device, device_address, "", |
| 315 | (strlen(device_address) != 0)/*matchAddress*/); |
| 316 | |
| 317 | if (devDesc == 0) { |
| 318 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", |
| 319 | device, device_address); |
| 320 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 321 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 322 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 323 | DeviceVector *deviceVector; |
| 324 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 325 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 326 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 327 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 328 | deviceVector = &mAvailableInputDevices; |
| 329 | } else { |
| 330 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 331 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | } |
Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 333 | |
| 334 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? |
| 335 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 338 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, |
| 339 | const char *device_address, |
| 340 | const char *device_name) |
| 341 | { |
| 342 | status_t status; |
| 343 | |
| 344 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", |
| 345 | device, device_address, device_name); |
| 346 | |
Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 347 | // connect/disconnect only 1 device at a time |
| 348 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 349 | |
Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 350 | // Check if the device is currently connected |
| 351 | sp<DeviceDescriptor> devDesc = |
| 352 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
| 353 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 354 | if (index < 0) { |
| 355 | // Nothing to do: device is not connected |
| 356 | return NO_ERROR; |
| 357 | } |
| 358 | |
| 359 | // Toggle the device state: UNAVAILABLE -> AVAILABLE |
| 360 | // This will force reading again the device configuration |
| 361 | status = setDeviceConnectionState(device, |
| 362 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 363 | device_address, device_name); |
| 364 | if (status != NO_ERROR) { |
| 365 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", |
| 366 | status); |
| 367 | return status; |
| 368 | } |
| 369 | |
| 370 | status = setDeviceConnectionState(device, |
| 371 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 372 | device_address, device_name); |
| 373 | if (status != NO_ERROR) { |
| 374 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", |
| 375 | status); |
| 376 | return status; |
| 377 | } |
| 378 | |
| 379 | return NO_ERROR; |
| 380 | } |
| 381 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 382 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 383 | { |
| 384 | bool createTxPatch = false; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 385 | status_t status; |
| 386 | audio_patch_handle_t afPatchHandle; |
| 387 | DeviceVector deviceList; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 388 | uint32_t muteWaitMs = 0; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 389 | |
Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 390 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 391 | return muteWaitMs; |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 392 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 393 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 394 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 395 | |
| 396 | // release existing RX patch if any |
| 397 | if (mCallRxPatch != 0) { |
| 398 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 399 | mCallRxPatch.clear(); |
| 400 | } |
| 401 | // release TX patch if any |
| 402 | if (mCallTxPatch != 0) { |
| 403 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 404 | mCallTxPatch.clear(); |
| 405 | } |
| 406 | |
| 407 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 408 | // via setOutputDevice() on primary output. |
| 409 | // Otherwise, create two audio patches for TX and RX path. |
| 410 | if (availablePrimaryOutputDevices() & rxDevice) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 411 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 412 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 413 | // of it due to legacy implementation. If not, create a patch. |
| 414 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 415 | == AUDIO_DEVICE_NONE) { |
| 416 | createTxPatch = true; |
| 417 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 418 | } else { // create RX path audio patch |
| 419 | struct audio_patch patch; |
| 420 | |
| 421 | patch.num_sources = 1; |
| 422 | patch.num_sinks = 1; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 423 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 424 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 425 | "updateCallRouting() selected device not in output device list"); |
| 426 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 427 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 428 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 429 | "updateCallRouting() no telephony RX device"); |
| 430 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 431 | |
| 432 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 433 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 434 | |
| 435 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 436 | SortedVector<audio_io_handle_t> outputs = |
| 437 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 438 | audio_io_handle_t output = selectOutput(outputs, |
| 439 | AUDIO_OUTPUT_FLAG_NONE, |
| 440 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 442 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 443 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 444 | "updateCallRouting() RX device output is duplicated"); |
| 445 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 446 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 447 | patch.num_sources = 2; |
| 448 | } |
| 449 | |
| 450 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 451 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 453 | status); |
| 454 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 455 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 456 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 457 | mCallRxPatch->mUid = mUidCached; |
| 458 | } |
| 459 | createTxPatch = true; |
| 460 | } |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 461 | if (createTxPatch) { // create TX path audio patch |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 462 | struct audio_patch patch; |
Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 463 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 464 | patch.num_sources = 1; |
| 465 | patch.num_sinks = 1; |
| 466 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 467 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 468 | "updateCallRouting() selected device not in input device list"); |
| 469 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 470 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 471 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 472 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 473 | "updateCallRouting() no telephony TX device"); |
| 474 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 475 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 476 | |
| 477 | SortedVector<audio_io_handle_t> outputs = |
| 478 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 479 | audio_io_handle_t output = selectOutput(outputs, |
| 480 | AUDIO_OUTPUT_FLAG_NONE, |
| 481 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 482 | // request to reuse existing output stream if one is already opened to reach the TX |
| 483 | // path output device |
| 484 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 485 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 486 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 487 | "updateCallRouting() RX device output is duplicated"); |
| 488 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 489 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 490 | patch.num_sources = 2; |
| 491 | } |
| 492 | |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 493 | // terminate active capture if on the same HW module as the call TX source device |
| 494 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 495 | // call TX device but this information is not in the audio patch and logic here must be |
| 496 | // symmetric to the one in startInput() |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 497 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 498 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 499 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 500 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { |
| 501 | AudioSessionCollection activeSessions = |
| 502 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 503 | for (size_t j = 0; j < activeSessions.size(); j++) { |
| 504 | audio_session_t activeSession = activeSessions.keyAt(j); |
| 505 | stopInput(activeDesc->mIoHandle, activeSession); |
| 506 | releaseInput(activeDesc->mIoHandle, activeSession); |
| 507 | } |
Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 511 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 512 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 513 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 514 | status); |
| 515 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 516 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 517 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 518 | mCallTxPatch->mUid = mUidCached; |
| 519 | } |
| 520 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 521 | |
| 522 | return muteWaitMs; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 525 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 526 | { |
| 527 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 528 | // store previous phone state for management of sonification strategy below |
| 529 | int oldState = mEngine->getPhoneState(); |
| 530 | |
| 531 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 532 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 533 | return; |
| 534 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 535 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | // if leaving call state, handle special case of active streams |
| 537 | // pertaining to sonification strategy see handleIncallSonification() |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 538 | if (isStateInCall(oldState)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 539 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 540 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 541 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 542 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 543 | |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 544 | // force reevaluating accessibility routing when call stops |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 545 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 546 | } |
| 547 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 548 | /** |
| 549 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 550 | * routing command. |
| 551 | */ |
| 552 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 553 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 | |
| 555 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | checkA2dpSuspend(); |
| 557 | checkOutputForAllStrategies(); |
| 558 | updateDevicesAndOutputs(); |
| 559 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | int delayMs = 0; |
| 561 | if (isStateInCall(state)) { |
| 562 | nsecs_t sysTime = systemTime(); |
| 563 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 564 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | // mute media and sonification strategies and delay device switch by the largest |
| 566 | // latency of any output where either strategy is active. |
| 567 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 568 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 569 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 570 | sysTime) || |
| 571 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 572 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 573 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 574 | (delayMs < (int)desc->latency()*2)) { |
| 575 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 576 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 577 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 578 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 580 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 581 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 583 | } |
| 584 | } |
| 585 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 586 | if (hasPrimaryOutput()) { |
| 587 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 588 | // the device returned is not necessarily reachable via this output |
| 589 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 590 | // force routing command to audio hardware when ending call |
| 591 | // even if no device change is needed |
| 592 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 593 | rxDevice = mPrimaryOutput->device(); |
| 594 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 595 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 596 | if (state == AUDIO_MODE_IN_CALL) { |
| 597 | updateCallRouting(rxDevice, delayMs); |
| 598 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 599 | if (mCallRxPatch != 0) { |
| 600 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 601 | mCallRxPatch.clear(); |
| 602 | } |
| 603 | if (mCallTxPatch != 0) { |
| 604 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 605 | mCallTxPatch.clear(); |
| 606 | } |
| 607 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 608 | } else { |
| 609 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 610 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 611 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 612 | // if entering in call state, handle special case of active streams |
| 613 | // pertaining to sonification strategy see handleIncallSonification() |
| 614 | if (isStateInCall(state)) { |
| 615 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 616 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 617 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 618 | } |
Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 619 | |
| 620 | // force reevaluating accessibility routing when call starts |
| 621 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 625 | if (state == AUDIO_MODE_RINGTONE && |
| 626 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | mLimitRingtoneVolume = true; |
| 628 | } else { |
| 629 | mLimitRingtoneVolume = false; |
| 630 | } |
| 631 | } |
| 632 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 633 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 634 | return mEngine->getPhoneState(); |
| 635 | } |
| 636 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 637 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 638 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 639 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 640 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame] | 641 | if (config == mEngine->getForceUse(usage)) { |
| 642 | return; |
| 643 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 644 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 645 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 646 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 647 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 648 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 649 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 650 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 651 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 652 | |
| 653 | // check for device and output changes triggered by new force usage |
| 654 | checkA2dpSuspend(); |
| 655 | checkOutputForAllStrategies(); |
| 656 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 657 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 658 | //FIXME: workaround for truncated touch sounds |
| 659 | // to be removed when the problem is handled by system UI |
| 660 | uint32_t delayMs = 0; |
| 661 | uint32_t waitMs = 0; |
| 662 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 663 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 664 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 665 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 666 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 667 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 668 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 669 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 670 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 671 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 672 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 673 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 674 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 675 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 676 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 677 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 681 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 682 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 683 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 684 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 685 | // Force new input selection if the new device can not be reached via current input |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 686 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 687 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 688 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 689 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 690 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 691 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 692 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | } |
| 694 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 695 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 696 | { |
| 697 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 698 | } |
| 699 | |
| 700 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 701 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 702 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 703 | audio_devices_t device, |
| 704 | uint32_t samplingRate, |
| 705 | audio_format_t format, |
| 706 | audio_channel_mask_t channelMask, |
| 707 | audio_output_flags_t flags) |
| 708 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 709 | // only retain flags that will drive the direct output profile selection |
| 710 | // if explicitly requested |
| 711 | static const uint32_t kRelevantFlags = |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 712 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD | |
| 713 | AUDIO_OUTPUT_FLAG_VOIP_RX); |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 714 | flags = |
| 715 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 716 | |
| 717 | sp<IOProfile> profile; |
| 718 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 719 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 720 | if (mHwModules[i]->mHandle == 0) { |
| 721 | continue; |
| 722 | } |
| 723 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 724 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 725 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 726 | samplingRate, NULL /*updatedSamplingRate*/, |
| 727 | format, NULL /*updatedFormat*/, |
| 728 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 729 | flags)) { |
| 730 | continue; |
| 731 | } |
| 732 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 733 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 734 | continue; |
| 735 | } |
| 736 | // if several profiles are compatible, give priority to one with offload capability |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 737 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 738 | continue; |
| 739 | } |
| 740 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 741 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 742 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 743 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 744 | } |
| 745 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 746 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 749 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 750 | uint32_t samplingRate, |
| 751 | audio_format_t format, |
| 752 | audio_channel_mask_t channelMask, |
| 753 | audio_output_flags_t flags, |
| 754 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 755 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 756 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 757 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 758 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 759 | device, stream, samplingRate, format, channelMask, flags); |
| 760 | |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 761 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, stream, samplingRate, format, |
| 762 | channelMask, flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 765 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 766 | audio_io_handle_t *output, |
| 767 | audio_session_t session, |
| 768 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 769 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 770 | const audio_config_t *config, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 771 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 772 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 773 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 774 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 775 | audio_attributes_t attributes; |
| 776 | if (attr != NULL) { |
| 777 | if (!isValidAttributes(attr)) { |
| 778 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 779 | attr->usage, attr->content_type, attr->flags, |
| 780 | attr->tags); |
| 781 | return BAD_VALUE; |
| 782 | } |
| 783 | attributes = *attr; |
| 784 | } else { |
| 785 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 786 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 787 | return BAD_VALUE; |
| 788 | } |
| 789 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 790 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 791 | |
| 792 | // TODO: check for existing client for this port ID |
| 793 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 794 | *portId = AudioPort::getNextUniqueId(); |
| 795 | } |
| 796 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 797 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 798 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 799 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 800 | if (!audio_has_proportional_frames(config->format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 801 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 802 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 803 | *stream = streamTypefromAttributesInt(&attributes); |
| 804 | *output = desc->mIoHandle; |
| 805 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 806 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 807 | } |
| 808 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 809 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 810 | return BAD_VALUE; |
| 811 | } |
| 812 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 813 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 814 | " session %d selectedDeviceId %d", |
| 815 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 816 | session, selectedDeviceId); |
| 817 | |
| 818 | *stream = streamTypefromAttributesInt(&attributes); |
| 819 | |
| 820 | // Explicit routing? |
| 821 | sp<DeviceDescriptor> deviceDesc; |
| 822 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 823 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 824 | deviceDesc = mAvailableOutputDevices[i]; |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 829 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 830 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 831 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 832 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 833 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 834 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 835 | } |
| 836 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 837 | 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] | 838 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 839 | |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 840 | *output = getOutputForDevice(device, session, *stream, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 841 | config->sample_rate, config->format, config->channel_mask, |
| 842 | flags, &config->offload_info); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 843 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 844 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 845 | return INVALID_OPERATION; |
| 846 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 847 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 848 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 852 | audio_devices_t device, |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 853 | audio_session_t session, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 854 | audio_stream_type_t stream, |
| 855 | uint32_t samplingRate, |
| 856 | audio_format_t format, |
| 857 | audio_channel_mask_t channelMask, |
| 858 | audio_output_flags_t flags, |
| 859 | const audio_offload_info_t *offloadInfo) |
| 860 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 861 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 862 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 863 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 864 | #ifdef AUDIO_POLICY_TEST |
| 865 | if (mCurOutput != 0) { |
| 866 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 867 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 868 | |
| 869 | if (mTestOutputs[mCurOutput] == 0) { |
| 870 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 871 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 872 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 873 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 874 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 875 | outputDesc->mFlags = |
| 876 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 877 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 878 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 879 | config.sample_rate = mTestSamplingRate; |
| 880 | config.channel_mask = mTestChannels; |
| 881 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 882 | if (offloadInfo != NULL) { |
| 883 | config.offload_info = *offloadInfo; |
| 884 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 885 | status = mpClientInterface->openOutput(0, |
| 886 | &mTestOutputs[mCurOutput], |
| 887 | &config, |
| 888 | &outputDesc->mDevice, |
| 889 | String8(""), |
| 890 | &outputDesc->mLatency, |
| 891 | outputDesc->mFlags); |
| 892 | if (status == NO_ERROR) { |
| 893 | outputDesc->mSamplingRate = config.sample_rate; |
| 894 | outputDesc->mFormat = config.format; |
| 895 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 896 | AudioParameter outputCmd = AudioParameter(); |
| 897 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 898 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 899 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 900 | } |
| 901 | } |
| 902 | return mTestOutputs[mCurOutput]; |
| 903 | } |
| 904 | #endif //AUDIO_POLICY_TEST |
| 905 | |
| 906 | // open a direct output if required by specified parameters |
| 907 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 908 | // and all common behaviors are driven by checking only the direct flag |
| 909 | // this should normally be set appropriately in the policy configuration file |
| 910 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 911 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 912 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 913 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 914 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 915 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 916 | // only allow deep buffering for music stream type |
| 917 | if (stream != AUDIO_STREAM_MUSIC) { |
| 918 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 919 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 920 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 921 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 922 | // use DEEP_BUFFER as default output for music stream type |
| 923 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 924 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 925 | if (stream == AUDIO_STREAM_TTS) { |
| 926 | flags = AUDIO_OUTPUT_FLAG_TTS; |
Haynes Mathew George | 84c621e | 2017-04-25 11:41:50 -0700 | [diff] [blame] | 927 | } else if (stream == AUDIO_STREAM_VOICE_CALL && |
| 928 | getPhoneState() == AUDIO_MODE_IN_COMMUNICATION && |
| 929 | audio_is_linear_pcm(format)) { |
| 930 | flags = (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_VOIP_RX | |
| 931 | AUDIO_OUTPUT_FLAG_DIRECT); |
| 932 | ALOGV("Set VoIP and Direct output flags for PCM format"); |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 933 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 934 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 935 | sp<IOProfile> profile; |
| 936 | |
| 937 | // 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] | 938 | // and not explicitly requested |
| 939 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 940 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 941 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 942 | goto non_direct_output; |
| 943 | } |
| 944 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 945 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 946 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 947 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 948 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 949 | // This may prevent offloading in rare situations where effects are left active by apps |
| 950 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 951 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 952 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 953 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 954 | profile = getProfileForDirectOutput(device, |
| 955 | samplingRate, |
| 956 | format, |
| 957 | channelMask, |
| 958 | (audio_output_flags_t)flags); |
| 959 | } |
| 960 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 961 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 962 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 963 | |
| 964 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 965 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 966 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 967 | outputDesc = desc; |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 968 | // reuse direct output if currently open by the same client |
| 969 | // and configured with same parameters |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | if ((samplingRate == outputDesc->mSamplingRate) && |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 971 | audio_formats_match(format, outputDesc->mFormat) && |
| 972 | (channelMask == outputDesc->mChannelMask)) { |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 973 | if (session == outputDesc->mDirectClientSession) { |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 974 | outputDesc->mDirectOpenCount++; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 975 | ALOGV("getOutput() reusing direct output %d for session %d", |
| 976 | mOutputs.keyAt(i), session); |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 977 | return mOutputs.keyAt(i); |
| 978 | } else { |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 979 | ALOGV("getOutput() do not reuse direct output because current client (%d) " |
| 980 | "is not the same as requesting client (%d)", |
| 981 | outputDesc->mDirectClientSession, session); |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 982 | goto non_direct_output; |
| 983 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | } |
| 987 | // close direct output if currently open and configured with different parameters |
| 988 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 989 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 990 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 991 | |
| 992 | // if the selected profile is offloaded and no offload info was specified, |
| 993 | // create a default one |
| 994 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 995 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 996 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 997 | defaultOffloadInfo.sample_rate = samplingRate; |
| 998 | defaultOffloadInfo.channel_mask = channelMask; |
| 999 | defaultOffloadInfo.format = format; |
| 1000 | defaultOffloadInfo.stream_type = stream; |
| 1001 | defaultOffloadInfo.bit_rate = 0; |
| 1002 | defaultOffloadInfo.duration_us = -1; |
| 1003 | defaultOffloadInfo.has_video = true; // conservative |
| 1004 | defaultOffloadInfo.is_streaming = true; // likely |
| 1005 | offloadInfo = &defaultOffloadInfo; |
| 1006 | } |
| 1007 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1008 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1009 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1010 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1011 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1012 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1013 | config.sample_rate = samplingRate; |
| 1014 | config.channel_mask = channelMask; |
| 1015 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 1016 | if (offloadInfo != NULL) { |
| 1017 | config.offload_info = *offloadInfo; |
| 1018 | } |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1019 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); |
| 1020 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress |
| 1021 | : String8(""); |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1022 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1023 | &output, |
| 1024 | &config, |
| 1025 | &outputDesc->mDevice, |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1026 | address, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1027 | &outputDesc->mLatency, |
| 1028 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1029 | |
| 1030 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1031 | if (status != NO_ERROR || |
| 1032 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1033 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1034 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1035 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 1036 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1037 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1038 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1039 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1040 | mpClientInterface->closeOutput(output); |
| 1041 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1042 | // 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] | 1043 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1044 | goto non_direct_output; |
| 1045 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1046 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1047 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1048 | outputDesc->mSamplingRate = config.sample_rate; |
| 1049 | outputDesc->mChannelMask = config.channel_mask; |
| 1050 | outputDesc->mFormat = config.format; |
| 1051 | outputDesc->mRefCount[stream] = 0; |
| 1052 | outputDesc->mStopTime[stream] = 0; |
| 1053 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 169753c | 2017-03-06 14:18:23 -0800 | [diff] [blame] | 1054 | outputDesc->mDirectClientSession = session; |
| 1055 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1056 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1057 | mPreviousOutputs = mOutputs; |
| 1058 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1059 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1060 | return output; |
| 1061 | } |
| 1062 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1063 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1064 | |
| 1065 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1066 | // stamps are embedded in audio data |
| 1067 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1068 | return AUDIO_IO_HANDLE_NONE; |
| 1069 | } |
| 1070 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1071 | // ignoring channel mask due to downmix capability in mixer |
| 1072 | |
| 1073 | // open a non direct output |
| 1074 | |
| 1075 | // for non direct outputs, only PCM is supported |
| 1076 | if (audio_is_linear_pcm(format)) { |
| 1077 | // get which output is suitable for the specified stream. The actual |
| 1078 | // routing change will happen when startOutput() will be called |
| 1079 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1080 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1081 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1082 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1083 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1084 | } |
| 1085 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1086 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1087 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1088 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1089 | |
| 1090 | return output; |
| 1091 | } |
| 1092 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1093 | 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] | 1094 | audio_output_flags_t flags, |
| 1095 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1096 | { |
| 1097 | // select one output among several that provide a path to a particular device or set of |
| 1098 | // devices (the list was previously build by getOutputsForDevice()). |
| 1099 | // The priority is as follows: |
| 1100 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1101 | // 2: the output with the bit depth the closest to the requested one |
| 1102 | // 3: the primary output |
| 1103 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1104 | |
| 1105 | if (outputs.size() == 0) { |
| 1106 | return 0; |
| 1107 | } |
| 1108 | if (outputs.size() == 1) { |
| 1109 | return outputs[0]; |
| 1110 | } |
| 1111 | |
| 1112 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1113 | audio_io_handle_t outputForFlags = 0; |
| 1114 | audio_io_handle_t outputForPrimary = 0; |
| 1115 | audio_io_handle_t outputForFormat = 0; |
| 1116 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1117 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1118 | |
| 1119 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1120 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1121 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1122 | // if a valid format is specified, skip output if not compatible |
| 1123 | if (format != AUDIO_FORMAT_INVALID) { |
| 1124 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1125 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1126 | continue; |
| 1127 | } |
| 1128 | } else if (!audio_is_linear_pcm(format)) { |
| 1129 | continue; |
| 1130 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1131 | if (AudioPort::isBetterFormatMatch( |
| 1132 | outputDesc->mFormat, bestFormat, format)) { |
| 1133 | outputForFormat = outputs[i]; |
| 1134 | bestFormat = outputDesc->mFormat; |
| 1135 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1136 | } |
| 1137 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1138 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1139 | if (commonFlags >= maxCommonFlags) { |
| 1140 | if (commonFlags == maxCommonFlags) { |
| 1141 | if (AudioPort::isBetterFormatMatch( |
| 1142 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1143 | outputForFlags = outputs[i]; |
| 1144 | bestFormatForFlags = outputDesc->mFormat; |
| 1145 | } |
| 1146 | } else { |
| 1147 | outputForFlags = outputs[i]; |
| 1148 | maxCommonFlags = commonFlags; |
| 1149 | bestFormatForFlags = outputDesc->mFormat; |
| 1150 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1151 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1152 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1153 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1154 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1159 | if (outputForFlags != 0) { |
| 1160 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1161 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1162 | if (outputForFormat != 0) { |
| 1163 | return outputForFormat; |
| 1164 | } |
| 1165 | if (outputForPrimary != 0) { |
| 1166 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | return outputs[0]; |
| 1170 | } |
| 1171 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1172 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1173 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1174 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1175 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1176 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1177 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1178 | ssize_t index = mOutputs.indexOfKey(output); |
| 1179 | if (index < 0) { |
| 1180 | ALOGW("startOutput() unknown output %d", output); |
| 1181 | return BAD_VALUE; |
| 1182 | } |
| 1183 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1184 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1185 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1186 | // Routing? |
| 1187 | mOutputRoutes.incRouteActivity(session); |
| 1188 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1189 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1190 | AudioMix *policyMix = NULL; |
| 1191 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1192 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1193 | policyMix = outputDesc->mPolicyMix; |
| 1194 | address = policyMix->mDeviceAddress.string(); |
| 1195 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1196 | newDevice = policyMix->mDeviceType; |
| 1197 | } else { |
| 1198 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1199 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1200 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1201 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1202 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1203 | } else { |
| 1204 | newDevice = AUDIO_DEVICE_NONE; |
| 1205 | } |
| 1206 | |
| 1207 | uint32_t delayMs = 0; |
| 1208 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1209 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1210 | |
| 1211 | if (status != NO_ERROR) { |
| 1212 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1213 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1214 | } |
| 1215 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1216 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1217 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1218 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1219 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1220 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1221 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1222 | "remote-submix"); |
| 1223 | } |
| 1224 | |
| 1225 | if (delayMs != 0) { |
| 1226 | usleep(delayMs * 1000); |
| 1227 | } |
| 1228 | |
| 1229 | return status; |
| 1230 | } |
| 1231 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1232 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1233 | audio_stream_type_t stream, |
| 1234 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1235 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1236 | uint32_t *delayMs) |
| 1237 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1238 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1239 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1240 | |
| 1241 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1242 | if (stream == AUDIO_STREAM_TTS) { |
| 1243 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1244 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1245 | return INVALID_OPERATION; |
| 1246 | } else { |
| 1247 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1248 | } |
| 1249 | } else { |
| 1250 | // some playback other than beacon starts |
| 1251 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1252 | } |
| 1253 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1254 | // 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] | 1255 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1256 | bool force = !outputDesc->isActive() && |
| 1257 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1258 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1259 | // increment usage count for this stream on the requested output: |
| 1260 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1261 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1262 | outputDesc->changeRefCount(stream, 1); |
| 1263 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1264 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1265 | selectOutputForMusicEffects(); |
| 1266 | } |
| 1267 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1268 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1269 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1270 | if (device == AUDIO_DEVICE_NONE) { |
| 1271 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1272 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1273 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1274 | routing_strategy strategy = getStrategy(stream); |
| 1275 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1276 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1277 | (beaconMuteLatency > 0); |
| 1278 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1279 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1280 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1281 | if (desc != outputDesc) { |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1282 | // force a device change if any other output is: |
| 1283 | // - managed by the same hw module |
| 1284 | // - has a current device selection that differs from selected device. |
| 1285 | // - supports currently selected device |
| 1286 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1287 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1288 | // change the device currently selected by the other active output. |
| 1289 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1290 | desc->device() != device && |
| 1291 | desc->supportedDevices() & device && |
| 1292 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1293 | force = true; |
| 1294 | } |
| 1295 | // 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] | 1296 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1297 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1298 | uint32_t latency = desc->latency(); |
| 1299 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1300 | waitMs = latency; |
| 1301 | } |
| 1302 | } |
| 1303 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1304 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1305 | |
| 1306 | // handle special case for sonification while in call |
| 1307 | if (isInCall()) { |
| 1308 | handleIncallSonification(stream, true, false); |
| 1309 | } |
| 1310 | |
| 1311 | // apply volume rules for current stream and device if necessary |
| 1312 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1313 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1314 | outputDesc, |
| 1315 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | |
| 1317 | // update the outputs if starting an output with a stream that can affect notification |
| 1318 | // routing |
| 1319 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1320 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1321 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1322 | if (strategy == STRATEGY_SONIFICATION) { |
| 1323 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1324 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1325 | |
| 1326 | if (waitMs > muteWaitMs) { |
| 1327 | *delayMs = waitMs - muteWaitMs; |
| 1328 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1329 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1330 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1331 | return NO_ERROR; |
| 1332 | } |
| 1333 | |
| 1334 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1335 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1336 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1337 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1338 | { |
| 1339 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1340 | ssize_t index = mOutputs.indexOfKey(output); |
| 1341 | if (index < 0) { |
| 1342 | ALOGW("stopOutput() unknown output %d", output); |
| 1343 | return BAD_VALUE; |
| 1344 | } |
| 1345 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1346 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1347 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1348 | if (outputDesc->mRefCount[stream] == 1) { |
| 1349 | // Automatically disable the remote submix input when output is stopped on a |
| 1350 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1351 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1352 | outputDesc->mPolicyMix != NULL && |
| 1353 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1354 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1355 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1356 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1357 | "remote-submix"); |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1362 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1363 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1364 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1365 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1366 | |
| 1367 | if (forceDeviceUpdate) { |
| 1368 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1369 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1370 | } |
| 1371 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1372 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1373 | } |
| 1374 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1375 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1376 | audio_stream_type_t stream, |
| 1377 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1378 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1379 | // always handle stream stop, check which stream type is stopping |
| 1380 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1381 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1382 | // handle special case for sonification while in call |
| 1383 | if (isInCall()) { |
| 1384 | handleIncallSonification(stream, false, false); |
| 1385 | } |
| 1386 | |
| 1387 | if (outputDesc->mRefCount[stream] > 0) { |
| 1388 | // decrement usage count of this stream on the output |
| 1389 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1390 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1391 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1392 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1393 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1394 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1395 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1396 | // the track stop() command is received and at that time the audio track buffer can |
| 1397 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1398 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1399 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1400 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1401 | |
| 1402 | // force restoring the device selection on other active outputs if it differs from the |
| 1403 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1404 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1405 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1406 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1407 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1408 | desc->isActive() && |
| 1409 | outputDesc->sharesHwModuleWith(desc) && |
| 1410 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1411 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1412 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1413 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1414 | newDevice2, |
| 1415 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1416 | delayMs); |
| 1417 | // re-apply device specific volume if not done by setOutputDevice() |
| 1418 | if (!force) { |
| 1419 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1420 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1421 | } |
| 1422 | } |
| 1423 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1424 | handleNotificationRoutingForStream(stream); |
| 1425 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1426 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1427 | selectOutputForMusicEffects(); |
| 1428 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1429 | return NO_ERROR; |
| 1430 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1431 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1432 | return INVALID_OPERATION; |
| 1433 | } |
| 1434 | } |
| 1435 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1436 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1437 | audio_stream_type_t stream __unused, |
| 1438 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1439 | { |
| 1440 | ALOGV("releaseOutput() %d", output); |
| 1441 | ssize_t index = mOutputs.indexOfKey(output); |
| 1442 | if (index < 0) { |
| 1443 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | #ifdef AUDIO_POLICY_TEST |
| 1448 | int testIndex = testOutputIndex(output); |
| 1449 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1450 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1451 | if (outputDesc->isActive()) { |
| 1452 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1453 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1454 | mTestOutputs[testIndex] = 0; |
| 1455 | } |
| 1456 | return; |
| 1457 | } |
| 1458 | #endif //AUDIO_POLICY_TEST |
| 1459 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1460 | // Routing |
| 1461 | mOutputRoutes.removeRoute(session); |
| 1462 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1463 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1464 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1465 | if (desc->mDirectOpenCount <= 0) { |
| 1466 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1467 | desc->mDirectOpenCount, output); |
| 1468 | return; |
| 1469 | } |
| 1470 | if (--desc->mDirectOpenCount == 0) { |
| 1471 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1472 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1478 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1479 | audio_io_handle_t *input, |
| 1480 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1481 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1482 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1483 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1484 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1485 | input_type_t *inputType, |
| 1486 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1487 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1488 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1489 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1490 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1491 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1492 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1493 | *inputType = API_INPUT_INVALID; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1494 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1495 | audio_devices_t device; |
| 1496 | // handle legacy remote submix case where the address was not always specified |
| 1497 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1498 | audio_source_t inputSource = attr->source; |
| 1499 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1500 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1501 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1502 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1503 | inputSource = AUDIO_SOURCE_MIC; |
| 1504 | } |
| 1505 | halInputSource = inputSource; |
| 1506 | |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1507 | // TODO: check for existing client for this port ID |
| 1508 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1509 | *portId = AudioPort::getNextUniqueId(); |
| 1510 | } |
| 1511 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1512 | // Explicit routing? |
| 1513 | sp<DeviceDescriptor> deviceDesc; |
| 1514 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1515 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1516 | deviceDesc = mAvailableInputDevices[i]; |
| 1517 | break; |
| 1518 | } |
| 1519 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1520 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1521 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1522 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1523 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1524 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1525 | if (ret != NO_ERROR) { |
| 1526 | return ret; |
| 1527 | } |
| 1528 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1529 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1530 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1531 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1532 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1533 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1534 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1535 | return BAD_VALUE; |
| 1536 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1537 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1538 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1539 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1540 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1541 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1542 | // know about it and is therefore considered "legacy" |
| 1543 | *inputType = API_INPUT_LEGACY; |
| 1544 | } else { |
| 1545 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1546 | // an external policy |
| 1547 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1548 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1549 | } else if (audio_is_remote_submix_device(device)) { |
| 1550 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1551 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1552 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1553 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1554 | } else { |
| 1555 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1556 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1557 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1561 | config->sample_rate, config->format, config->channel_mask, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1562 | policyMix); |
| 1563 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1564 | mInputRoutes.removeRoute(session); |
| 1565 | return INVALID_OPERATION; |
| 1566 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1567 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1568 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1569 | return NO_ERROR; |
| 1570 | } |
| 1571 | |
| 1572 | |
| 1573 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1574 | String8 address, |
| 1575 | audio_session_t session, |
| 1576 | uid_t uid, |
| 1577 | audio_source_t inputSource, |
| 1578 | uint32_t samplingRate, |
| 1579 | audio_format_t format, |
| 1580 | audio_channel_mask_t channelMask, |
| 1581 | audio_input_flags_t flags, |
| 1582 | AudioMix *policyMix) |
| 1583 | { |
| 1584 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1585 | audio_source_t halInputSource = inputSource; |
| 1586 | bool isSoundTrigger = false; |
| 1587 | |
| 1588 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1589 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1590 | if (index >= 0) { |
| 1591 | input = mSoundTriggerSessions.valueFor(session); |
| 1592 | isSoundTrigger = true; |
| 1593 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1594 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1595 | } else { |
| 1596 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1597 | } |
Haynes Mathew George | 851d3ff | 2017-06-19 20:01:57 -0700 | [diff] [blame^] | 1598 | } else if (inputSource == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 1599 | getPhoneState() == AUDIO_MODE_IN_COMMUNICATION && |
| 1600 | audio_is_linear_pcm(format)) { |
| 1601 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_VOIP_TX); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1602 | } |
| 1603 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1604 | // find a compatible input profile (not necessarily identical in parameters) |
| 1605 | sp<IOProfile> profile; |
| 1606 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1607 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1608 | audio_format_t profileFormat = format; |
| 1609 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1610 | audio_input_flags_t profileFlags = flags; |
| 1611 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1612 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1613 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1614 | profileFlags); |
| 1615 | if (profile != 0) { |
| 1616 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1617 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1618 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1619 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1620 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1621 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1622 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1623 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1624 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1625 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1626 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1627 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1628 | // Pick input sampling rate if not specified by client |
| 1629 | if (samplingRate == 0) { |
| 1630 | samplingRate = profileSamplingRate; |
| 1631 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1632 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1633 | if (profile->getModuleHandle() == 0) { |
| 1634 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1635 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1636 | } |
| 1637 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1638 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1639 | inputSource, |
| 1640 | format, |
| 1641 | samplingRate, |
| 1642 | channelMask, |
| 1643 | flags, |
| 1644 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1645 | isSoundTrigger, |
| 1646 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1647 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1648 | // FIXME: disable concurrent capture until UI is ready |
| 1649 | #if 0 |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1650 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1651 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1652 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1653 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1654 | // reuse input if: |
| 1655 | // - it shares the same profile |
| 1656 | // AND |
| 1657 | // - it is not a reroute submix input |
| 1658 | // AND |
| 1659 | // - it is: not used for sound trigger |
| 1660 | // OR |
| 1661 | // used for sound trigger and all clients use the same session ID |
| 1662 | // |
| 1663 | if ((profile == desc->mProfile) && |
| 1664 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1665 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1666 | |
| 1667 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1668 | if (as != 0) { |
| 1669 | // do not allow unmatching properties on same session |
| 1670 | if (as->matches(audioSession)) { |
| 1671 | as->changeOpenCount(1); |
| 1672 | } else { |
| 1673 | ALOGW("getInputForDevice() record with different attributes" |
| 1674 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1675 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1676 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1677 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1678 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1679 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1680 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1681 | // Reuse the already opened input stream on this profile if: |
| 1682 | // - the new capture source is background OR |
| 1683 | // - the path requested configurations match OR |
| 1684 | // - the new source priority is less than the highest source priority on this input |
| 1685 | // If the input stream cannot be reused, close it before opening a new stream |
| 1686 | // on the same profile for the new client so that the requested path configuration |
| 1687 | // can be selected. |
| 1688 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1689 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1690 | desc->mChannelMask != channelMask || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1691 | !audio_formats_match(desc->mFormat, format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1692 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1693 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1694 | reusedInputDesc = desc; |
| 1695 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1696 | } else { |
| 1697 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1698 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1699 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1700 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1701 | } |
| 1702 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1703 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1704 | if (reusedInputDesc != 0) { |
| 1705 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1706 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1707 | audio_session_t currentSession = sessions.keyAt(j); |
| 1708 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1709 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1710 | } |
| 1711 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1712 | #endif |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1713 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1714 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1715 | config.sample_rate = profileSamplingRate; |
| 1716 | config.channel_mask = profileChannelMask; |
| 1717 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1718 | |
Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1719 | if (address == "") { |
| 1720 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); |
| 1721 | // the inputs vector must be of size 1, but we don't want to crash here |
| 1722 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); |
| 1723 | } |
| 1724 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1725 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1726 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1727 | &config, |
| 1728 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1729 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1730 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1731 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1732 | |
| 1733 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1734 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1735 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1736 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1737 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1738 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1739 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1740 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1741 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1742 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1743 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1744 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1745 | } |
| 1746 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1747 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1748 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1749 | inputDesc->mFormat = profileFormat; |
| 1750 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1751 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1752 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1753 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1754 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1755 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1756 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1757 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1758 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1759 | } |
| 1760 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1761 | //static |
| 1762 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1763 | { |
| 1764 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1765 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1766 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1767 | } |
| 1768 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1769 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1770 | const sp<AudioSession>& audioSession) |
| 1771 | { |
| 1772 | // Do not allow capture if an active voice call is using a software patch and |
| 1773 | // the call TX source device is on the same HW module. |
| 1774 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1775 | // call TX device but this information is not in the audio patch |
| 1776 | if (mCallTxPatch != 0 && |
| 1777 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
| 1781 | // starting concurrent capture is enabled if: |
| 1782 | // 1) capturing for re-routing |
| 1783 | // 2) capturing for HOTWORD source |
| 1784 | // 3) capturing for FM TUNER source |
| 1785 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1786 | |
| 1787 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1788 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1789 | return true; |
| 1790 | } |
| 1791 | |
| 1792 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1793 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1794 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1795 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1796 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1797 | return false; |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | return true; |
| 1802 | } |
| 1803 | |
| 1804 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1805 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1806 | audio_session_t session, |
| 1807 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1808 | { |
| 1809 | ALOGV("startInput() input %d", input); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1810 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1811 | ssize_t index = mInputs.indexOfKey(input); |
| 1812 | if (index < 0) { |
| 1813 | ALOGW("startInput() unknown input %d", input); |
| 1814 | return BAD_VALUE; |
| 1815 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1816 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1817 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1818 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1819 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1820 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1821 | return BAD_VALUE; |
| 1822 | } |
| 1823 | |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1824 | // FIXME: disable concurrent capture until UI is ready |
| 1825 | #if 0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1826 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1827 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1828 | return INVALID_OPERATION; |
| 1829 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1830 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1831 | if (isInCall()) { |
| 1832 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1833 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1834 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1835 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1836 | } |
Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1837 | #else |
| 1838 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
| 1839 | if (mCallTxPatch != 0 && |
| 1840 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1841 | ALOGW("startInput(%d) failed: call in progress", input); |
| 1842 | return INVALID_OPERATION; |
| 1843 | } |
| 1844 | |
| 1845 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1846 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1847 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 1848 | |
| 1849 | if (is_virtual_input_device(activeDesc->mDevice)) { |
| 1850 | continue; |
| 1851 | } |
| 1852 | |
| 1853 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1854 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { |
| 1855 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1856 | if (activeDesc->hasPreemptedSession(session)) { |
| 1857 | ALOGW("startInput(%d) failed for HOTWORD: " |
| 1858 | "other input %d already started for HOTWORD", |
| 1859 | input, activeDesc->mIoHandle); |
| 1860 | return INVALID_OPERATION; |
| 1861 | } |
| 1862 | } else { |
| 1863 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", |
| 1864 | input, activeDesc->mIoHandle); |
| 1865 | return INVALID_OPERATION; |
| 1866 | } |
| 1867 | } else { |
| 1868 | if (activeSource != AUDIO_SOURCE_HOTWORD) { |
| 1869 | ALOGW("startInput(%d) failed: other input %d already started", |
| 1870 | input, activeDesc->mIoHandle); |
| 1871 | return INVALID_OPERATION; |
| 1872 | } |
| 1873 | } |
| 1874 | } |
| 1875 | |
| 1876 | // if capture is allowed, preempt currently active HOTWORD captures |
| 1877 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1878 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 1879 | |
| 1880 | if (is_virtual_input_device(activeDesc->mDevice)) { |
| 1881 | continue; |
| 1882 | } |
| 1883 | |
| 1884 | audio_source_t activeSource = activeDesc->inputSource(true); |
| 1885 | if (activeSource == AUDIO_SOURCE_HOTWORD) { |
| 1886 | AudioSessionCollection activeSessions = |
| 1887 | activeDesc->getAudioSessions(true /*activeOnly*/); |
| 1888 | audio_session_t activeSession = activeSessions.keyAt(0); |
| 1889 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; |
| 1890 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); |
| 1891 | sessions.add(activeSession); |
| 1892 | inputDesc->setPreemptedSessions(sessions); |
| 1893 | stopInput(activeHandle, activeSession); |
| 1894 | releaseInput(activeHandle, activeSession); |
| 1895 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", |
| 1896 | input, activeDesc->mIoHandle); |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | #endif |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1901 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1902 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1903 | // are considered for device selection |
| 1904 | audioSession->changeActiveCount(1); |
| 1905 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1906 | // Routing? |
| 1907 | mInputRoutes.incRouteActivity(session); |
| 1908 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1909 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1910 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 1911 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1912 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1913 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1914 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1915 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
| 1916 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1917 | if ((inputDesc->mPolicyMix != NULL) |
| 1918 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1919 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1920 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1921 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1922 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1923 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1924 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 1925 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1926 | SoundTrigger::setCaptureState(true); |
| 1927 | } |
| 1928 | |
| 1929 | // automatically enable the remote submix output when input is started if not |
| 1930 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1931 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1932 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1933 | String8 address = String8(""); |
| 1934 | if (inputDesc->mPolicyMix == NULL) { |
| 1935 | address = String8("0"); |
| 1936 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1937 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1938 | } |
| 1939 | if (address != "") { |
| 1940 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1941 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1942 | address, "remote-submix"); |
| 1943 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1944 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1945 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1948 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1949 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1950 | return NO_ERROR; |
| 1951 | } |
| 1952 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1953 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1954 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1955 | { |
| 1956 | ALOGV("stopInput() input %d", input); |
| 1957 | ssize_t index = mInputs.indexOfKey(input); |
| 1958 | if (index < 0) { |
| 1959 | ALOGW("stopInput() unknown input %d", input); |
| 1960 | return BAD_VALUE; |
| 1961 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1962 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1963 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1964 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1965 | if (index < 0) { |
| 1966 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1967 | return BAD_VALUE; |
| 1968 | } |
| 1969 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1970 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1971 | ALOGW("stopInput() input %d already stopped", input); |
| 1972 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1975 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1976 | |
| 1977 | // Routing? |
| 1978 | mInputRoutes.decRouteActivity(session); |
| 1979 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1980 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1981 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1982 | if (inputDesc->isActive()) { |
| 1983 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 1984 | } else { |
| 1985 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1986 | if ((inputDesc->mPolicyMix != NULL) |
| 1987 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1988 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1989 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1990 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1991 | |
| 1992 | // automatically disable the remote submix output when input is stopped if not |
| 1993 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1994 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1995 | String8 address = String8(""); |
| 1996 | if (inputDesc->mPolicyMix == NULL) { |
| 1997 | address = String8("0"); |
| 1998 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1999 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 2000 | } |
| 2001 | if (address != "") { |
| 2002 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2003 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2004 | address, "remote-submix"); |
| 2005 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2006 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2007 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2008 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2009 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2010 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2011 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 2012 | // primary HW module |
| 2013 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 2014 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 2015 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2016 | SoundTrigger::setCaptureState(false); |
| 2017 | } |
| 2018 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2019 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2020 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2021 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2024 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 2025 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2026 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2027 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2028 | ALOGV("releaseInput() %d", input); |
| 2029 | ssize_t index = mInputs.indexOfKey(input); |
| 2030 | if (index < 0) { |
| 2031 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 2032 | return; |
| 2033 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2034 | |
| 2035 | // Routing |
| 2036 | mInputRoutes.removeRoute(session); |
| 2037 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2038 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 2039 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2040 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2041 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2042 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2043 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 2044 | return; |
| 2045 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2046 | |
| 2047 | if (audioSession->openCount() == 0) { |
| 2048 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 2049 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2050 | return; |
| 2051 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2052 | |
| 2053 | if (audioSession->changeOpenCount(-1) == 0) { |
| 2054 | inputDesc->removeAudioSession(session); |
| 2055 | } |
| 2056 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2057 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2058 | ALOGV("releaseInput() exit > 0"); |
| 2059 | return; |
| 2060 | } |
| 2061 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2062 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2063 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2064 | ALOGV("releaseInput() exit"); |
| 2065 | } |
| 2066 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2067 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2068 | bool patchRemoved = false; |
| 2069 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2070 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2071 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2072 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2073 | if (patch_index >= 0) { |
| 2074 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2075 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2076 | mAudioPatches.removeItemsAt(patch_index); |
| 2077 | patchRemoved = true; |
| 2078 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2079 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 2080 | } |
| 2081 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2082 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2083 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2084 | |
| 2085 | if (patchRemoved) { |
| 2086 | mpClientInterface->onAudioPatchListUpdate(); |
| 2087 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2088 | } |
| 2089 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2090 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2091 | int indexMin, |
| 2092 | int indexMax) |
| 2093 | { |
| 2094 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2095 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2096 | |
| 2097 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2098 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2099 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2100 | continue; |
| 2101 | } |
| 2102 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2103 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2106 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2107 | int index, |
| 2108 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2109 | { |
| 2110 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2111 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 2112 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2113 | return BAD_VALUE; |
| 2114 | } |
| 2115 | if (!audio_is_output_device(device)) { |
| 2116 | return BAD_VALUE; |
| 2117 | } |
| 2118 | |
| 2119 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2120 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2121 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2122 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2123 | stream, device, index); |
| 2124 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2125 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2126 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2127 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2128 | continue; |
| 2129 | } |
| 2130 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2131 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2132 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2133 | // update volume on all outputs and streams matching the following: |
| 2134 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2135 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2136 | // the requested device |
| 2137 | // - For non default requested device, currently selected device on the output is either the |
| 2138 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2139 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2140 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2141 | status_t status = NO_ERROR; |
| 2142 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2143 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2144 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2145 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2146 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2147 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2148 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2149 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2150 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2151 | continue; |
| 2152 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2153 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2154 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/); |
| 2155 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2156 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2157 | continue; |
| 2158 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2159 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2160 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2161 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2162 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2163 | } else { |
| 2164 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
| 2165 | stream, Volume::getDeviceForVolume(curStreamDevice)); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2166 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2167 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2168 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2169 | //FIXME: workaround for truncated touch sounds |
| 2170 | // delayed volume change for system stream to be removed when the problem is |
| 2171 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2172 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2173 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2174 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2175 | if (volStatus != NO_ERROR) { |
| 2176 | status = volStatus; |
| 2177 | } |
| 2178 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2179 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2180 | } |
| 2181 | return status; |
| 2182 | } |
| 2183 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2184 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2185 | int *index, |
| 2186 | audio_devices_t device) |
| 2187 | { |
| 2188 | if (index == NULL) { |
| 2189 | return BAD_VALUE; |
| 2190 | } |
| 2191 | if (!audio_is_output_device(device)) { |
| 2192 | return BAD_VALUE; |
| 2193 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2194 | // 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] | 2195 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2196 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2197 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2198 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2199 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2200 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2201 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2202 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2203 | return NO_ERROR; |
| 2204 | } |
| 2205 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2206 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2207 | { |
| 2208 | // select one output among several suitable for global effects. |
| 2209 | // The priority is as follows: |
| 2210 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2211 | // AudioFlinger will invalidate the track and the offloaded output |
| 2212 | // will be closed causing the effect to be moved to a PCM output. |
| 2213 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2214 | // 3: The primary output |
| 2215 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2216 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2217 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2218 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2219 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2220 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2221 | if (outputs.size() == 0) { |
| 2222 | return AUDIO_IO_HANDLE_NONE; |
| 2223 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2224 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2225 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2226 | bool activeOnly = true; |
| 2227 | |
| 2228 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2229 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2230 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2231 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2232 | |
| 2233 | for (size_t i = 0; i < outputs.size(); i++) { |
| 2234 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
| 2235 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2236 | continue; |
| 2237 | } |
| 2238 | ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x", |
| 2239 | activeOnly, i, desc->mFlags); |
| 2240 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 2241 | outputOffloaded = outputs[i]; |
| 2242 | } |
| 2243 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 2244 | outputDeepBuffer = outputs[i]; |
| 2245 | } |
| 2246 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
| 2247 | outputPrimary = outputs[i]; |
| 2248 | } |
| 2249 | } |
| 2250 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2251 | output = outputOffloaded; |
| 2252 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2253 | output = outputDeepBuffer; |
| 2254 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2255 | output = outputPrimary; |
| 2256 | } else { |
| 2257 | output = outputs[0]; |
| 2258 | } |
| 2259 | activeOnly = false; |
| 2260 | } |
| 2261 | |
| 2262 | if (output != mMusicEffectOutput) { |
| 2263 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2264 | mMusicEffectOutput = output; |
| 2265 | } |
| 2266 | |
| 2267 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2268 | return output; |
| 2269 | } |
| 2270 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2271 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2272 | { |
| 2273 | return selectOutputForMusicEffects(); |
| 2274 | } |
| 2275 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2276 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2277 | audio_io_handle_t io, |
| 2278 | uint32_t strategy, |
| 2279 | int session, |
| 2280 | int id) |
| 2281 | { |
| 2282 | ssize_t index = mOutputs.indexOfKey(io); |
| 2283 | if (index < 0) { |
| 2284 | index = mInputs.indexOfKey(io); |
| 2285 | if (index < 0) { |
| 2286 | ALOGW("registerEffect() unknown io %d", io); |
| 2287 | return INVALID_OPERATION; |
| 2288 | } |
| 2289 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2290 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2293 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2294 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2295 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2296 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2297 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2298 | continue; |
| 2299 | } |
| 2300 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2301 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2302 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2306 | { |
| 2307 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2308 | } |
| 2309 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2310 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2311 | { |
| 2312 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2313 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2314 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2315 | return true; |
| 2316 | } |
| 2317 | } |
| 2318 | return false; |
| 2319 | } |
| 2320 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2321 | // Register a list of custom mixes with their attributes and format. |
| 2322 | // When a mix is registered, corresponding input and output profiles are |
| 2323 | // added to the remote submix hw module. The profile contains only the |
| 2324 | // parameters (sampling rate, format...) specified by the mix. |
| 2325 | // The corresponding input remote submix device is also connected. |
| 2326 | // |
| 2327 | // When a remote submix device is connected, the address is checked to select the |
| 2328 | // appropriate profile and the corresponding input or output stream is opened. |
| 2329 | // |
| 2330 | // When capture starts, getInputForAttr() will: |
| 2331 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2332 | // - 2 if none found, getDeviceForInputSource() will: |
| 2333 | // - 2.1 look for a mix matching the attributes source |
| 2334 | // - 2.2 if none found, default to device selection by policy rules |
| 2335 | // At this time, the corresponding output remote submix device is also connected |
| 2336 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2337 | // after AudioTracks are invalidated |
| 2338 | // |
| 2339 | // When playback starts, getOutputForAttr() will: |
| 2340 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2341 | // - 2 if none found, look for a mix matching the attributes usage |
| 2342 | // - 3 if none found, default to device and output selection by policy rules. |
| 2343 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2344 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2345 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2346 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2347 | status_t res = NO_ERROR; |
| 2348 | |
| 2349 | sp<HwModule> rSubmixModule; |
| 2350 | // examine each mix's route type |
| 2351 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2352 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2353 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2354 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2355 | break; |
| 2356 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2357 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2358 | // Loop back through "remote submix" |
| 2359 | if (rSubmixModule == 0) { |
| 2360 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2361 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2362 | && mHwModules[j]->mHandle != 0) { |
| 2363 | rSubmixModule = mHwModules[j]; |
| 2364 | break; |
| 2365 | } |
| 2366 | } |
| 2367 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2368 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2369 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2370 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2371 | if (rSubmixModule == 0) { |
| 2372 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2373 | res = INVALID_OPERATION; |
| 2374 | break; |
| 2375 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2376 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2377 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2378 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2379 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2380 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2381 | res = INVALID_OPERATION; |
| 2382 | break; |
| 2383 | } |
| 2384 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2385 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2386 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2387 | // stereo and let audio flinger do the channel conversion if needed. |
| 2388 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2389 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2390 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2391 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2392 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2393 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2394 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2395 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2396 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2397 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2398 | address.string(), "remote-submix"); |
| 2399 | } else { |
| 2400 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2401 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2402 | address.string(), "remote-submix"); |
| 2403 | } |
| 2404 | } 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] | 2405 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2406 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2407 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2408 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2409 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2410 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2411 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2412 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2413 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2414 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2415 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2416 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2417 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2418 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2419 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2420 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2421 | } else { |
| 2422 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2423 | } |
| 2424 | break; |
| 2425 | } |
| 2426 | } |
| 2427 | |
| 2428 | if (res != NO_ERROR) { |
| 2429 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2430 | i, device, address.string()); |
| 2431 | res = INVALID_OPERATION; |
| 2432 | break; |
| 2433 | } else if (!foundOutput) { |
| 2434 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2435 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2436 | res = INVALID_OPERATION; |
| 2437 | break; |
| 2438 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2439 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2440 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2441 | if (res != NO_ERROR) { |
| 2442 | unregisterPolicyMixes(mixes); |
| 2443 | } |
| 2444 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2445 | } |
| 2446 | |
| 2447 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2448 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2449 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2450 | status_t res = NO_ERROR; |
| 2451 | sp<HwModule> rSubmixModule; |
| 2452 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2453 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2454 | 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] | 2455 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2456 | if (rSubmixModule == 0) { |
| 2457 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2458 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2459 | && mHwModules[j]->mHandle != 0) { |
| 2460 | rSubmixModule = mHwModules[j]; |
| 2461 | break; |
| 2462 | } |
| 2463 | } |
| 2464 | } |
| 2465 | if (rSubmixModule == 0) { |
| 2466 | res = INVALID_OPERATION; |
| 2467 | continue; |
| 2468 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2469 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2470 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2471 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2472 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2473 | res = INVALID_OPERATION; |
| 2474 | continue; |
| 2475 | } |
| 2476 | |
| 2477 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2478 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2479 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2480 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2481 | address.string(), "remote-submix"); |
| 2482 | } |
| 2483 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2484 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2485 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2486 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2487 | address.string(), "remote-submix"); |
| 2488 | } |
| 2489 | rSubmixModule->removeOutputProfile(address); |
| 2490 | rSubmixModule->removeInputProfile(address); |
| 2491 | |
| 2492 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2493 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2494 | res = INVALID_OPERATION; |
| 2495 | continue; |
| 2496 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2497 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2498 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2499 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2500 | } |
| 2501 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2502 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2503 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2504 | { |
| 2505 | const size_t SIZE = 256; |
| 2506 | char buffer[SIZE]; |
| 2507 | String8 result; |
| 2508 | |
| 2509 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2510 | result.append(buffer); |
| 2511 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2512 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2513 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2514 | result.append(buffer); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2515 | std::string stateLiteral; |
| 2516 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
| 2517 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2518 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2519 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2520 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2521 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2522 | 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] | 2523 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2524 | 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] | 2525 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2526 | 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] | 2527 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2528 | 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] | 2529 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2530 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2531 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2532 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2533 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2534 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2535 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2536 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2537 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2538 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2539 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2540 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2541 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2542 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2543 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2544 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2545 | mHwModules.dump(fd); |
| 2546 | mOutputs.dump(fd); |
| 2547 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2548 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2549 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2550 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2551 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2552 | |
| 2553 | return NO_ERROR; |
| 2554 | } |
| 2555 | |
| 2556 | // This function checks for the parameters which can be offloaded. |
| 2557 | // This can be enhanced depending on the capability of the DSP and policy |
| 2558 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2559 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2560 | { |
| 2561 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2562 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2563 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2564 | offloadInfo.format, |
| 2565 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2566 | offloadInfo.has_video); |
| 2567 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2568 | if (mMasterMono) { |
| 2569 | return false; // no offloading if mono is set. |
| 2570 | } |
| 2571 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2572 | // Check if offload has been disabled |
| 2573 | char propValue[PROPERTY_VALUE_MAX]; |
| 2574 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2575 | if (atoi(propValue) != 0) { |
| 2576 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2577 | return false; |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | // Check if stream type is music, then only allow offload as of now. |
| 2582 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2583 | { |
| 2584 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2585 | return false; |
| 2586 | } |
| 2587 | |
| 2588 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2589 | const bool allowOffloadWithVideo = |
| 2590 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2591 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2592 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2593 | return false; |
| 2594 | } |
| 2595 | |
| 2596 | //If duration is less than minimum value defined in property, return false |
| 2597 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2598 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2599 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2600 | return false; |
| 2601 | } |
| 2602 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2603 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2604 | return false; |
| 2605 | } |
| 2606 | |
| 2607 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2608 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2609 | // detects there is an active non offloadable effect. |
| 2610 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2611 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2612 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2613 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2614 | return false; |
| 2615 | } |
| 2616 | |
| 2617 | // See if there is a profile to support this. |
| 2618 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2619 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2620 | offloadInfo.sample_rate, |
| 2621 | offloadInfo.format, |
| 2622 | offloadInfo.channel_mask, |
| 2623 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2624 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2625 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2626 | } |
| 2627 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2628 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2629 | audio_port_type_t type, |
| 2630 | unsigned int *num_ports, |
| 2631 | struct audio_port *ports, |
| 2632 | unsigned int *generation) |
| 2633 | { |
| 2634 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2635 | generation == NULL) { |
| 2636 | return BAD_VALUE; |
| 2637 | } |
| 2638 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2639 | if (ports == NULL) { |
| 2640 | *num_ports = 0; |
| 2641 | } |
| 2642 | |
| 2643 | size_t portsWritten = 0; |
| 2644 | size_t portsMax = *num_ports; |
| 2645 | *num_ports = 0; |
| 2646 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2647 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2648 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2649 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2650 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2651 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2652 | continue; |
| 2653 | } |
| 2654 | if (portsWritten < portsMax) { |
| 2655 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2656 | } |
| 2657 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2658 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2659 | } |
| 2660 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2661 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2662 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2663 | continue; |
| 2664 | } |
| 2665 | if (portsWritten < portsMax) { |
| 2666 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2667 | } |
| 2668 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2669 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2673 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2674 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2675 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2676 | } |
| 2677 | *num_ports += mInputs.size(); |
| 2678 | } |
| 2679 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2680 | size_t numOutputs = 0; |
| 2681 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2682 | if (!mOutputs[i]->isDuplicated()) { |
| 2683 | numOutputs++; |
| 2684 | if (portsWritten < portsMax) { |
| 2685 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2686 | } |
| 2687 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2688 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2689 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2690 | } |
| 2691 | } |
| 2692 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2693 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2694 | return NO_ERROR; |
| 2695 | } |
| 2696 | |
| 2697 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2698 | { |
| 2699 | return NO_ERROR; |
| 2700 | } |
| 2701 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2702 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2703 | audio_patch_handle_t *handle, |
| 2704 | uid_t uid) |
| 2705 | { |
| 2706 | ALOGV("createAudioPatch()"); |
| 2707 | |
| 2708 | if (handle == NULL || patch == NULL) { |
| 2709 | return BAD_VALUE; |
| 2710 | } |
| 2711 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2712 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2713 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2714 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2715 | return BAD_VALUE; |
| 2716 | } |
| 2717 | // only one source per audio patch supported for now |
| 2718 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2719 | return INVALID_OPERATION; |
| 2720 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2721 | |
| 2722 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2723 | return INVALID_OPERATION; |
| 2724 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2725 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2726 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2727 | return INVALID_OPERATION; |
| 2728 | } |
| 2729 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2730 | |
| 2731 | sp<AudioPatch> patchDesc; |
| 2732 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2733 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2734 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2735 | patch->sources[0].role, |
| 2736 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2737 | #if LOG_NDEBUG == 0 |
| 2738 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2739 | 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] | 2740 | patch->sinks[i].role, |
| 2741 | patch->sinks[i].type); |
| 2742 | } |
| 2743 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2744 | |
| 2745 | if (index >= 0) { |
| 2746 | patchDesc = mAudioPatches.valueAt(index); |
| 2747 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2748 | mUidCached, patchDesc->mUid, uid); |
| 2749 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2750 | return INVALID_OPERATION; |
| 2751 | } |
| 2752 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2753 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2754 | } |
| 2755 | |
| 2756 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2757 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2758 | if (outputDesc == NULL) { |
| 2759 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2760 | return BAD_VALUE; |
| 2761 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2762 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2763 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2764 | if (patchDesc != 0) { |
| 2765 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2766 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2767 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2768 | return BAD_VALUE; |
| 2769 | } |
| 2770 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2771 | DeviceVector devices; |
| 2772 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2773 | // Only support mix to devices connection |
| 2774 | // TODO add support for mix to mix connection |
| 2775 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2776 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2777 | return INVALID_OPERATION; |
| 2778 | } |
| 2779 | sp<DeviceDescriptor> devDesc = |
| 2780 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2781 | if (devDesc == 0) { |
| 2782 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2783 | return BAD_VALUE; |
| 2784 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2785 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2786 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2787 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2788 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2789 | NULL, // updatedSamplingRate |
| 2790 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2791 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2792 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2793 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2794 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2795 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2796 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2797 | return INVALID_OPERATION; |
| 2798 | } |
| 2799 | devices.add(devDesc); |
| 2800 | } |
| 2801 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2802 | return INVALID_OPERATION; |
| 2803 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2804 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2805 | // TODO: reconfigure output format and channels here |
| 2806 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2807 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2808 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2809 | index = mAudioPatches.indexOfKey(*handle); |
| 2810 | if (index >= 0) { |
| 2811 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2812 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2813 | } |
| 2814 | patchDesc = mAudioPatches.valueAt(index); |
| 2815 | patchDesc->mUid = uid; |
| 2816 | ALOGV("createAudioPatch() success"); |
| 2817 | } else { |
| 2818 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2819 | return INVALID_OPERATION; |
| 2820 | } |
| 2821 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2822 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2823 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2824 | // only one sink supported when connecting an input device to a mix |
| 2825 | if (patch->num_sinks > 1) { |
| 2826 | return INVALID_OPERATION; |
| 2827 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2828 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2829 | if (inputDesc == NULL) { |
| 2830 | return BAD_VALUE; |
| 2831 | } |
| 2832 | if (patchDesc != 0) { |
| 2833 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2834 | return BAD_VALUE; |
| 2835 | } |
| 2836 | } |
| 2837 | sp<DeviceDescriptor> devDesc = |
| 2838 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2839 | if (devDesc == 0) { |
| 2840 | return BAD_VALUE; |
| 2841 | } |
| 2842 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2843 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2844 | devDesc->mAddress, |
| 2845 | patch->sinks[0].sample_rate, |
| 2846 | NULL, /*updatedSampleRate*/ |
| 2847 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2848 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2849 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2850 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2851 | // FIXME for the parameter type, |
| 2852 | // and the NONE |
| 2853 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2854 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2855 | return INVALID_OPERATION; |
| 2856 | } |
| 2857 | // TODO: reconfigure output format and channels here |
| 2858 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2859 | devDesc->type(), inputDesc->mIoHandle); |
| 2860 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2861 | index = mAudioPatches.indexOfKey(*handle); |
| 2862 | if (index >= 0) { |
| 2863 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2864 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2865 | } |
| 2866 | patchDesc = mAudioPatches.valueAt(index); |
| 2867 | patchDesc->mUid = uid; |
| 2868 | ALOGV("createAudioPatch() success"); |
| 2869 | } else { |
| 2870 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2871 | return INVALID_OPERATION; |
| 2872 | } |
| 2873 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2874 | // device to device connection |
| 2875 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2876 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2877 | return BAD_VALUE; |
| 2878 | } |
| 2879 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2880 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2881 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2882 | if (srcDeviceDesc == 0) { |
| 2883 | return BAD_VALUE; |
| 2884 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2885 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2886 | //update source and sink with our own data as the data passed in the patch may |
| 2887 | // be incomplete. |
| 2888 | struct audio_patch newPatch = *patch; |
| 2889 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2890 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2891 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2892 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2893 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2894 | return INVALID_OPERATION; |
| 2895 | } |
| 2896 | |
| 2897 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2898 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2899 | if (sinkDeviceDesc == 0) { |
| 2900 | return BAD_VALUE; |
| 2901 | } |
| 2902 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2903 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2904 | // create a software bridge in PatchPanel if: |
| 2905 | // - source and sink devices are on differnt HW modules OR |
| 2906 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2907 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2908 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2909 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2910 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2911 | return INVALID_OPERATION; |
| 2912 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2913 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2914 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2915 | // if the sink device is reachable via an opened output stream, request to go via |
| 2916 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2917 | audio_io_handle_t output = selectOutput(outputs, |
| 2918 | AUDIO_OUTPUT_FLAG_NONE, |
| 2919 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2920 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2921 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2922 | if (outputDesc->isDuplicated()) { |
| 2923 | return INVALID_OPERATION; |
| 2924 | } |
| 2925 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2926 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2927 | newPatch.num_sources = 2; |
| 2928 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2929 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2930 | } |
| 2931 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2932 | |
| 2933 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2934 | if (index >= 0) { |
| 2935 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2936 | } |
| 2937 | |
| 2938 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2939 | &afPatchHandle, |
| 2940 | 0); |
| 2941 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2942 | status, afPatchHandle); |
| 2943 | if (status == NO_ERROR) { |
| 2944 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2945 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2946 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2947 | } else { |
| 2948 | patchDesc->mPatch = newPatch; |
| 2949 | } |
| 2950 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2951 | *handle = patchDesc->mHandle; |
| 2952 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2953 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2954 | } else { |
| 2955 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2956 | status); |
| 2957 | return INVALID_OPERATION; |
| 2958 | } |
| 2959 | } else { |
| 2960 | return BAD_VALUE; |
| 2961 | } |
| 2962 | } else { |
| 2963 | return BAD_VALUE; |
| 2964 | } |
| 2965 | return NO_ERROR; |
| 2966 | } |
| 2967 | |
| 2968 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2969 | uid_t uid) |
| 2970 | { |
| 2971 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2972 | |
| 2973 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2974 | |
| 2975 | if (index < 0) { |
| 2976 | return BAD_VALUE; |
| 2977 | } |
| 2978 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2979 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2980 | mUidCached, patchDesc->mUid, uid); |
| 2981 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2982 | return INVALID_OPERATION; |
| 2983 | } |
| 2984 | |
| 2985 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2986 | patchDesc->mUid = mUidCached; |
| 2987 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2988 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2989 | if (outputDesc == NULL) { |
| 2990 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2991 | return BAD_VALUE; |
| 2992 | } |
| 2993 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2994 | setOutputDevice(outputDesc, |
| 2995 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2996 | true, |
| 2997 | 0, |
| 2998 | NULL); |
| 2999 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 3000 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3001 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3002 | if (inputDesc == NULL) { |
| 3003 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 3004 | return BAD_VALUE; |
| 3005 | } |
| 3006 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 3007 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3008 | true, |
| 3009 | NULL); |
| 3010 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3011 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3012 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 3013 | status, patchDesc->mAfPatchHandle); |
| 3014 | removeAudioPatch(patchDesc->mHandle); |
| 3015 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3016 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3017 | } else { |
| 3018 | return BAD_VALUE; |
| 3019 | } |
| 3020 | } else { |
| 3021 | return BAD_VALUE; |
| 3022 | } |
| 3023 | return NO_ERROR; |
| 3024 | } |
| 3025 | |
| 3026 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 3027 | struct audio_patch *patches, |
| 3028 | unsigned int *generation) |
| 3029 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3030 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3031 | return BAD_VALUE; |
| 3032 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3033 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3034 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3035 | } |
| 3036 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3037 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3038 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3039 | ALOGV("setAudioPortConfig()"); |
| 3040 | |
| 3041 | if (config == NULL) { |
| 3042 | return BAD_VALUE; |
| 3043 | } |
| 3044 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 3045 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3046 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 3047 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3050 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3051 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 3052 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3053 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3054 | if (outputDesc == NULL) { |
| 3055 | return BAD_VALUE; |
| 3056 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3057 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 3058 | "setAudioPortConfig() called on duplicated output %d", |
| 3059 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3060 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3061 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3062 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3063 | if (inputDesc == NULL) { |
| 3064 | return BAD_VALUE; |
| 3065 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3066 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3067 | } else { |
| 3068 | return BAD_VALUE; |
| 3069 | } |
| 3070 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 3071 | sp<DeviceDescriptor> deviceDesc; |
| 3072 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 3073 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 3074 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 3075 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 3076 | } else { |
| 3077 | return BAD_VALUE; |
| 3078 | } |
| 3079 | if (deviceDesc == NULL) { |
| 3080 | return BAD_VALUE; |
| 3081 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3082 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3083 | } else { |
| 3084 | return BAD_VALUE; |
| 3085 | } |
| 3086 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3087 | struct audio_port_config backupConfig; |
| 3088 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 3089 | if (status == NO_ERROR) { |
| 3090 | struct audio_port_config newConfig; |
| 3091 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3092 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3093 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3094 | if (status != NO_ERROR) { |
| 3095 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3096 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3097 | |
| 3098 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3099 | } |
| 3100 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3101 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3102 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3103 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3104 | clearAudioPatches(uid); |
| 3105 | clearSessionRoutes(uid); |
| 3106 | } |
| 3107 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3108 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3109 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3110 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3111 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3112 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3113 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | } |
| 3117 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3118 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3119 | audio_io_handle_t ouptutToSkip) |
| 3120 | { |
| 3121 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3122 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3123 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3124 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3125 | continue; |
| 3126 | } |
| 3127 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3128 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3129 | continue; |
| 3130 | } |
| 3131 | // If the default device for this strategy is on another output mix, |
| 3132 | // invalidate all tracks in this strategy to force re connection. |
| 3133 | // Otherwise select new device on the output mix. |
| 3134 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3135 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3136 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3137 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3138 | } |
| 3139 | } |
| 3140 | } else { |
| 3141 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3142 | setOutputDevice(outputDesc, newDevice, false); |
| 3143 | } |
| 3144 | } |
| 3145 | } |
| 3146 | |
| 3147 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3148 | { |
| 3149 | // remove output routes associated with this uid |
| 3150 | SortedVector<routing_strategy> affectedStrategies; |
| 3151 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3152 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3153 | if (route->mUid == uid) { |
| 3154 | mOutputRoutes.removeItemsAt(i); |
| 3155 | if (route->mDeviceDescriptor != 0) { |
| 3156 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3157 | } |
| 3158 | } |
| 3159 | } |
| 3160 | // reroute outputs if necessary |
| 3161 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 3162 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 3163 | } |
| 3164 | |
| 3165 | // remove input routes associated with this uid |
| 3166 | SortedVector<audio_source_t> affectedSources; |
| 3167 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3168 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3169 | if (route->mUid == uid) { |
| 3170 | mInputRoutes.removeItemsAt(i); |
| 3171 | if (route->mDeviceDescriptor != 0) { |
| 3172 | affectedSources.add(route->mSource); |
| 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | // reroute inputs if necessary |
| 3177 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3178 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3179 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3180 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3181 | inputsToClose.add(inputDesc->mIoHandle); |
| 3182 | } |
| 3183 | } |
| 3184 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 3185 | closeInput(inputsToClose[i]); |
| 3186 | } |
| 3187 | } |
| 3188 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3189 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3190 | { |
| 3191 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3192 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3193 | if (sourceDesc->mUid == uid) { |
| 3194 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3195 | } |
| 3196 | } |
| 3197 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3198 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3199 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3200 | audio_io_handle_t *ioHandle, |
| 3201 | audio_devices_t *device) |
| 3202 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3203 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3204 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3205 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3206 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3207 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3208 | } |
| 3209 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3210 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3211 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3212 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3213 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3214 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3215 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3216 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3217 | return BAD_VALUE; |
| 3218 | } |
| 3219 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3220 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3221 | |
| 3222 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3223 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3224 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3225 | return INVALID_OPERATION; |
| 3226 | } |
| 3227 | |
| 3228 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3229 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3230 | String8(source->ext.device.address)); |
| 3231 | if (srcDeviceDesc == 0) { |
| 3232 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3233 | return BAD_VALUE; |
| 3234 | } |
| 3235 | sp<AudioSourceDescriptor> sourceDesc = |
| 3236 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3237 | |
| 3238 | struct audio_patch dummyPatch; |
| 3239 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3240 | sourceDesc->mPatchDesc = patchDesc; |
| 3241 | |
| 3242 | status_t status = connectAudioSource(sourceDesc); |
| 3243 | if (status == NO_ERROR) { |
| 3244 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3245 | *handle = sourceDesc->getHandle(); |
| 3246 | } |
| 3247 | return status; |
| 3248 | } |
| 3249 | |
| 3250 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3251 | { |
| 3252 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3253 | |
| 3254 | // make sure we only have one patch per source. |
| 3255 | disconnectAudioSource(sourceDesc); |
| 3256 | |
| 3257 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3258 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3259 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3260 | |
| 3261 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3262 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3263 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3264 | |
| 3265 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3266 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3267 | |
| 3268 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3269 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3270 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3271 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3272 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3273 | // create patch between src device and output device |
| 3274 | // create Hwoutput and add to mHwOutputs |
| 3275 | } else { |
| 3276 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3277 | audio_io_handle_t output = |
| 3278 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3279 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3280 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3281 | return INVALID_OPERATION; |
| 3282 | } |
| 3283 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3284 | if (outputDesc->isDuplicated()) { |
| 3285 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3286 | return INVALID_OPERATION; |
| 3287 | } |
| 3288 | // create a special patch with no sink and two sources: |
| 3289 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3290 | // be connected as well as the stream type for volume control |
| 3291 | // - the sink is defined by whatever output device is currently selected for the output |
| 3292 | // though which this patch is routed. |
| 3293 | patch->num_sinks = 0; |
| 3294 | patch->num_sources = 2; |
| 3295 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3296 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3297 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3298 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3299 | &afPatchHandle, |
| 3300 | 0); |
| 3301 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3302 | status, afPatchHandle); |
| 3303 | if (status != NO_ERROR) { |
| 3304 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3305 | __FUNCTION__, status); |
| 3306 | return INVALID_OPERATION; |
| 3307 | } |
| 3308 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3309 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3310 | |
| 3311 | if (status != NO_ERROR) { |
| 3312 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3313 | return status; |
| 3314 | } |
| 3315 | sourceDesc->mSwOutput = outputDesc; |
| 3316 | if (delayMs != 0) { |
| 3317 | usleep(delayMs * 1000); |
| 3318 | } |
| 3319 | } |
| 3320 | |
| 3321 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3322 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3323 | |
| 3324 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3325 | } |
| 3326 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3327 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3328 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3329 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3330 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3331 | if (sourceDesc == 0) { |
| 3332 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3333 | return BAD_VALUE; |
| 3334 | } |
| 3335 | status_t status = disconnectAudioSource(sourceDesc); |
| 3336 | |
| 3337 | mAudioSources.removeItem(handle); |
| 3338 | return status; |
| 3339 | } |
| 3340 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3341 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3342 | { |
| 3343 | if (mMasterMono == mono) { |
| 3344 | return NO_ERROR; |
| 3345 | } |
| 3346 | mMasterMono = mono; |
| 3347 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3348 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3349 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3350 | // |
| 3351 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3352 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3353 | // play back offloaded. |
| 3354 | if (mMasterMono) { |
| 3355 | Vector<audio_io_handle_t> offloaded; |
| 3356 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3357 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3358 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3359 | offloaded.push(desc->mIoHandle); |
| 3360 | } |
| 3361 | } |
| 3362 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3363 | closeOutput(offloaded[i]); |
| 3364 | } |
| 3365 | } |
| 3366 | // update master mono for all remaining outputs |
| 3367 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3368 | updateMono(mOutputs.keyAt(i)); |
| 3369 | } |
| 3370 | return NO_ERROR; |
| 3371 | } |
| 3372 | |
| 3373 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3374 | { |
| 3375 | *mono = mMasterMono; |
| 3376 | return NO_ERROR; |
| 3377 | } |
| 3378 | |
Eric Laurent | ac9cef5 | 2017-06-09 15:46:26 -0700 | [diff] [blame] | 3379 | float AudioPolicyManager::getStreamVolumeDB( |
| 3380 | audio_stream_type_t stream, int index, audio_devices_t device) |
| 3381 | { |
| 3382 | return computeVolume(stream, index, device); |
| 3383 | } |
| 3384 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3385 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3386 | { |
| 3387 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3388 | |
| 3389 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3390 | if (patchDesc == 0) { |
| 3391 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3392 | sourceDesc->mPatchDesc->mHandle); |
| 3393 | return BAD_VALUE; |
| 3394 | } |
| 3395 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3396 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3397 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3398 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3399 | if (swOutputDesc != 0) { |
| 3400 | stopSource(swOutputDesc, stream, false); |
| 3401 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3402 | } else { |
| 3403 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3404 | if (hwOutputDesc != 0) { |
| 3405 | // release patch between src device and output device |
| 3406 | // close Hwoutput and remove from mHwOutputs |
| 3407 | } else { |
| 3408 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3409 | } |
| 3410 | } |
| 3411 | return NO_ERROR; |
| 3412 | } |
| 3413 | |
| 3414 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3415 | audio_io_handle_t output, routing_strategy strategy) |
| 3416 | { |
| 3417 | sp<AudioSourceDescriptor> source; |
| 3418 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3419 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3420 | routing_strategy sourceStrategy = |
| 3421 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3422 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3423 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3424 | source = sourceDesc; |
| 3425 | break; |
| 3426 | } |
| 3427 | } |
| 3428 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3429 | } |
| 3430 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3431 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3432 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3433 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3434 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3435 | { |
| 3436 | return android_atomic_inc(&mAudioPortGeneration); |
| 3437 | } |
| 3438 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3439 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3440 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3441 | static const char *kConfigLocationList[] = |
| 3442 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3443 | static const int kConfigLocationListSize = |
| 3444 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3445 | |
| 3446 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3447 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
| 3448 | status_t ret; |
| 3449 | |
| 3450 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3451 | PolicySerializer serializer; |
| 3452 | snprintf(audioPolicyXmlConfigFile, |
| 3453 | sizeof(audioPolicyXmlConfigFile), |
| 3454 | "%s/%s", |
| 3455 | kConfigLocationList[i], |
| 3456 | AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3457 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3458 | if (ret == NO_ERROR) { |
| 3459 | break; |
| 3460 | } |
| 3461 | } |
| 3462 | return ret; |
| 3463 | } |
| 3464 | #endif |
| 3465 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3466 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3467 | : |
| 3468 | #ifdef AUDIO_POLICY_TEST |
| 3469 | Thread(false), |
| 3470 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3471 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3472 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3473 | mAudioPortGeneration(1), |
| 3474 | mBeaconMuteRefCount(0), |
| 3475 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3476 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3477 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3478 | mMasterMono(false), |
| 3479 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3480 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3481 | mUidCached = getuid(); |
| 3482 | mpClientInterface = clientInterface; |
| 3483 | |
| 3484 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3485 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3486 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3487 | bool speakerDrcEnabled = false; |
| 3488 | |
| 3489 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3490 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3491 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3492 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3493 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3494 | if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3495 | #else |
| 3496 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3497 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3498 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3499 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3500 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3501 | #endif |
| 3502 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3503 | config.setDefault(); |
| 3504 | } |
| 3505 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3506 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3507 | |
| 3508 | // 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] | 3509 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3510 | if (!engineInstance) { |
| 3511 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3512 | return; |
| 3513 | } |
| 3514 | // Retrieve the Policy Manager Interface |
| 3515 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3516 | if (mEngine == NULL) { |
| 3517 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3518 | return; |
| 3519 | } |
| 3520 | mEngine->setObserver(this); |
| 3521 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3522 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3523 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3524 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3525 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3526 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3527 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3528 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3529 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3530 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3531 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3532 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3533 | continue; |
| 3534 | } |
| 3535 | // open all output streams needed to access attached devices |
| 3536 | // except for direct output streams that are only opened when they are actually |
| 3537 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3538 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3539 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3540 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3541 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3542 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3543 | if (!outProfile->hasSupportedDevices()) { |
| 3544 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3545 | continue; |
| 3546 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3547 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3548 | mTtsOutputAvailable = true; |
| 3549 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3550 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3551 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3552 | continue; |
| 3553 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3554 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3555 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3556 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3557 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3558 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3559 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3560 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3561 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3562 | if ((profileType & outputDeviceTypes) == 0) { |
| 3563 | continue; |
| 3564 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3565 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3566 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3567 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3568 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3569 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3570 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3571 | |
| 3572 | outputDesc->mDevice = profileType; |
| 3573 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3574 | config.sample_rate = outputDesc->mSamplingRate; |
| 3575 | config.channel_mask = outputDesc->mChannelMask; |
| 3576 | config.format = outputDesc->mFormat; |
| 3577 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3578 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3579 | &output, |
| 3580 | &config, |
| 3581 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3582 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3583 | &outputDesc->mLatency, |
| 3584 | outputDesc->mFlags); |
| 3585 | |
| 3586 | if (status != NO_ERROR) { |
| 3587 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3588 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3589 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3590 | } else { |
| 3591 | outputDesc->mSamplingRate = config.sample_rate; |
| 3592 | outputDesc->mChannelMask = config.channel_mask; |
| 3593 | outputDesc->mFormat = config.format; |
| 3594 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3595 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3596 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3597 | // 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] | 3598 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3599 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3600 | } |
| 3601 | } |
| 3602 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3603 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3604 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3605 | } |
| 3606 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3607 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3608 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3609 | true, |
| 3610 | 0, |
| 3611 | NULL, |
| 3612 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3613 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3614 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3615 | // open input streams needed to access attached devices to validate |
| 3616 | // mAvailableInputDevices list |
| 3617 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3618 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3619 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3620 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3621 | if (!inProfile->hasSupportedDevices()) { |
| 3622 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3623 | continue; |
| 3624 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3625 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3626 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3627 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3628 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3629 | if ((profileType & inputDeviceTypes) == 0) { |
| 3630 | continue; |
| 3631 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3632 | sp<AudioInputDescriptor> inputDesc = |
| 3633 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3634 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3635 | inputDesc->mDevice = profileType; |
| 3636 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3637 | // find the address |
| 3638 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3639 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3640 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3641 | : String8(""); |
| 3642 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3643 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3644 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3645 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3646 | config.sample_rate = inputDesc->mSamplingRate; |
| 3647 | config.channel_mask = inputDesc->mChannelMask; |
| 3648 | config.format = inputDesc->mFormat; |
| 3649 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3650 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3651 | &input, |
| 3652 | &config, |
| 3653 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3654 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3655 | AUDIO_SOURCE_MIC, |
| 3656 | AUDIO_INPUT_FLAG_NONE); |
| 3657 | |
| 3658 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3659 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3660 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3661 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3662 | // 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] | 3663 | if (index >= 0) { |
| 3664 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3665 | if (!devDesc->isAttached()) { |
| 3666 | devDesc->attach(mHwModules[i]); |
| 3667 | devDesc->importAudioPort(inProfile); |
| 3668 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3669 | } |
| 3670 | } |
| 3671 | mpClientInterface->closeInput(input); |
| 3672 | } else { |
| 3673 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3674 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3675 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3676 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | // make sure all attached devices have been allocated a unique ID |
| 3680 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3681 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3682 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3683 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3684 | continue; |
| 3685 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3686 | // The device is now validated and can be appended to the available devices of the engine |
| 3687 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3688 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3689 | i++; |
| 3690 | } |
| 3691 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3692 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3693 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3694 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3695 | continue; |
| 3696 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3697 | // The device is now validated and can be appended to the available devices of the engine |
| 3698 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3699 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3700 | i++; |
| 3701 | } |
| 3702 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3703 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3704 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3705 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3706 | |
| 3707 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3708 | |
| 3709 | updateDevicesAndOutputs(); |
| 3710 | |
| 3711 | #ifdef AUDIO_POLICY_TEST |
| 3712 | if (mPrimaryOutput != 0) { |
| 3713 | AudioParameter outputCmd = AudioParameter(); |
| 3714 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3715 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3716 | |
| 3717 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3718 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3719 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3720 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3721 | mTestLatencyMs = 0; |
| 3722 | mCurOutput = 0; |
| 3723 | mDirectOutput = false; |
| 3724 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3725 | mTestOutputs[i] = 0; |
| 3726 | } |
| 3727 | |
| 3728 | const size_t SIZE = 256; |
| 3729 | char buffer[SIZE]; |
| 3730 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3731 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3732 | } |
| 3733 | #endif //AUDIO_POLICY_TEST |
| 3734 | } |
| 3735 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3736 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3737 | { |
| 3738 | #ifdef AUDIO_POLICY_TEST |
| 3739 | exit(); |
| 3740 | #endif //AUDIO_POLICY_TEST |
| 3741 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3742 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3743 | } |
| 3744 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3745 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3746 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3747 | mAvailableOutputDevices.clear(); |
| 3748 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3749 | mOutputs.clear(); |
| 3750 | mInputs.clear(); |
| 3751 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3752 | } |
| 3753 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3754 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3755 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3756 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3760 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3761 | { |
| 3762 | ALOGV("entering threadLoop()"); |
| 3763 | while (!exitPending()) |
| 3764 | { |
| 3765 | String8 command; |
| 3766 | int valueInt; |
| 3767 | String8 value; |
| 3768 | |
| 3769 | Mutex::Autolock _l(mLock); |
| 3770 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3771 | |
| 3772 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3773 | AudioParameter param = AudioParameter(command); |
| 3774 | |
| 3775 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3776 | valueInt != 0) { |
| 3777 | ALOGV("Test command %s received", command.string()); |
| 3778 | String8 target; |
| 3779 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3780 | target = "Manager"; |
| 3781 | } |
| 3782 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3783 | param.remove(String8("test_cmd_policy_output")); |
| 3784 | mCurOutput = valueInt; |
| 3785 | } |
| 3786 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3787 | param.remove(String8("test_cmd_policy_direct")); |
| 3788 | if (value == "false") { |
| 3789 | mDirectOutput = false; |
| 3790 | } else if (value == "true") { |
| 3791 | mDirectOutput = true; |
| 3792 | } |
| 3793 | } |
| 3794 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3795 | param.remove(String8("test_cmd_policy_input")); |
| 3796 | mTestInput = valueInt; |
| 3797 | } |
| 3798 | |
| 3799 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3800 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3801 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3802 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3803 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3804 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3805 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3806 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3807 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3809 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3810 | if (target == "Manager") { |
| 3811 | mTestFormat = format; |
| 3812 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3813 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3814 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3815 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3816 | } |
| 3817 | } |
| 3818 | } |
| 3819 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3820 | param.remove(String8("test_cmd_policy_channels")); |
| 3821 | int channels = 0; |
| 3822 | |
| 3823 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3824 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3825 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3826 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3827 | } |
| 3828 | if (channels != 0) { |
| 3829 | if (target == "Manager") { |
| 3830 | mTestChannels = channels; |
| 3831 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3832 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3833 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3834 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3835 | } |
| 3836 | } |
| 3837 | } |
| 3838 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3839 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3840 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3841 | int samplingRate = valueInt; |
| 3842 | if (target == "Manager") { |
| 3843 | mTestSamplingRate = samplingRate; |
| 3844 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3845 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3846 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3847 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3848 | } |
| 3849 | } |
| 3850 | } |
| 3851 | |
| 3852 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3853 | param.remove(String8("test_cmd_policy_reopen")); |
| 3854 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3855 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3856 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3857 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3858 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3859 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3860 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3861 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3862 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3863 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3864 | config.sample_rate = outputDesc->mSamplingRate; |
| 3865 | config.channel_mask = outputDesc->mChannelMask; |
| 3866 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3867 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3868 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3869 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3870 | &config, |
| 3871 | &outputDesc->mDevice, |
| 3872 | String8(""), |
| 3873 | &outputDesc->mLatency, |
| 3874 | outputDesc->mFlags); |
| 3875 | if (status != NO_ERROR) { |
| 3876 | ALOGE("Failed to reopen hardware output stream, " |
| 3877 | "samplingRate: %d, format %d, channels %d", |
| 3878 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3880 | outputDesc->mSamplingRate = config.sample_rate; |
| 3881 | outputDesc->mChannelMask = config.channel_mask; |
| 3882 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3883 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3884 | AudioParameter outputCmd = AudioParameter(); |
| 3885 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3886 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3887 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | |
| 3892 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3893 | } |
| 3894 | } |
| 3895 | return false; |
| 3896 | } |
| 3897 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3898 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3899 | { |
| 3900 | { |
| 3901 | AutoMutex _l(mLock); |
| 3902 | requestExit(); |
| 3903 | mWaitWorkCV.signal(); |
| 3904 | } |
| 3905 | requestExitAndWait(); |
| 3906 | } |
| 3907 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3908 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3909 | { |
| 3910 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3911 | if (output == mTestOutputs[i]) return i; |
| 3912 | } |
| 3913 | return 0; |
| 3914 | } |
| 3915 | #endif //AUDIO_POLICY_TEST |
| 3916 | |
| 3917 | // --- |
| 3918 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3919 | void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3920 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3921 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3922 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3923 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3924 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3925 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3926 | } |
| 3927 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3928 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3929 | { |
| 3930 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3931 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3932 | } |
| 3933 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3934 | void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3935 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3936 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3937 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3938 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3939 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3940 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3941 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3942 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3943 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3944 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3945 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3946 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3947 | if (devDesc != 0) { |
| 3948 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3949 | desc->mIoHandle, address.string()); |
| 3950 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3951 | } |
| 3952 | } |
| 3953 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3954 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3955 | audio_policy_dev_state_t state, |
| 3956 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3957 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3958 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3959 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3960 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3961 | |
| 3962 | if (audio_device_is_digital(device)) { |
| 3963 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3964 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3965 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3966 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3967 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3968 | // first list already open outputs that can be routed to this device |
| 3969 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3970 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3971 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3972 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3973 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3974 | outputs.add(mOutputs.keyAt(i)); |
| 3975 | } else { |
| 3976 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3977 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3978 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3982 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3983 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3984 | { |
| 3985 | if (mHwModules[i]->mHandle == 0) { |
| 3986 | continue; |
| 3987 | } |
| 3988 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3989 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3990 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3991 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3992 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3993 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3994 | profiles.add(profile); |
| 3995 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3996 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3997 | } |
| 3998 | } |
| 3999 | } |
| 4000 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 4001 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4002 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4003 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 4004 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4005 | return BAD_VALUE; |
| 4006 | } |
| 4007 | |
| 4008 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 4009 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4010 | 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] | 4011 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4012 | |
| 4013 | // nothing to do if one output is already opened for this profile |
| 4014 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4015 | for (j = 0; j < outputs.size(); j++) { |
| 4016 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4017 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4018 | // matching profile: save the sample rates, format and channel masks supported |
| 4019 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4020 | if (audio_device_is_digital(device)) { |
| 4021 | devDesc->importAudioPort(profile); |
| 4022 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4023 | break; |
| 4024 | } |
| 4025 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4026 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4027 | continue; |
| 4028 | } |
| 4029 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4030 | ALOGV("opening output for device %08x with params %s profile %p", |
| 4031 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4032 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4033 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4034 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4035 | config.sample_rate = desc->mSamplingRate; |
| 4036 | config.channel_mask = desc->mChannelMask; |
| 4037 | config.format = desc->mFormat; |
| 4038 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 4039 | config.offload_info.channel_mask = desc->mChannelMask; |
| 4040 | config.offload_info.format = desc->mFormat; |
| 4041 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4042 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4043 | &output, |
| 4044 | &config, |
| 4045 | &desc->mDevice, |
| 4046 | address, |
| 4047 | &desc->mLatency, |
| 4048 | desc->mFlags); |
| 4049 | if (status == NO_ERROR) { |
| 4050 | desc->mSamplingRate = config.sample_rate; |
| 4051 | desc->mChannelMask = config.channel_mask; |
| 4052 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4053 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4054 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4055 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4056 | char *param = audio_device_address_to_parameter(device, address); |
| 4057 | mpClientInterface->setParameters(output, String8(param)); |
| 4058 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4059 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4060 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4061 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4062 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4063 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4064 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4065 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4066 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4067 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4068 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4069 | config.offload_info.sample_rate = config.sample_rate; |
| 4070 | config.offload_info.channel_mask = config.channel_mask; |
| 4071 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4072 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4073 | &output, |
| 4074 | &config, |
| 4075 | &desc->mDevice, |
| 4076 | address, |
| 4077 | &desc->mLatency, |
| 4078 | desc->mFlags); |
| 4079 | if (status == NO_ERROR) { |
| 4080 | desc->mSamplingRate = config.sample_rate; |
| 4081 | desc->mChannelMask = config.channel_mask; |
| 4082 | desc->mFormat = config.format; |
| 4083 | } else { |
| 4084 | output = AUDIO_IO_HANDLE_NONE; |
| 4085 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4088 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4089 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4090 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4091 | sp<AudioPolicyMix> policyMix; |
| 4092 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4093 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 4094 | address.string()); |
| 4095 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4096 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4097 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4098 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4099 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4100 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4101 | // no duplicated output for direct outputs and |
| 4102 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4103 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4104 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4105 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4106 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4107 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4108 | //TODO: configure audio effect output stage here |
| 4109 | |
| 4110 | // 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] | 4111 | duplicatedOutput = |
| 4112 | mpClientInterface->openDuplicateOutput(output, |
| 4113 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4114 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4115 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4116 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4117 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4118 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 4119 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4120 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 4121 | dupOutputDesc->mFormat = desc->mFormat; |
| 4122 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 4123 | dupOutputDesc->mLatency = desc->mLatency; |
| 4124 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4125 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4126 | } else { |
| 4127 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4128 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4129 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4130 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4131 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4132 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4133 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4134 | } |
| 4135 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4136 | } else { |
| 4137 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4138 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4139 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4140 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4141 | profiles.removeAt(profile_index); |
| 4142 | profile_index--; |
| 4143 | } else { |
| 4144 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4145 | // Load digital format info only for digital devices |
| 4146 | if (audio_device_is_digital(device)) { |
| 4147 | devDesc->importAudioPort(profile); |
| 4148 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4149 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4150 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4151 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4152 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4153 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4154 | NULL/*patch handle*/, address.string()); |
| 4155 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4156 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4157 | } |
| 4158 | } |
| 4159 | |
| 4160 | if (profiles.isEmpty()) { |
| 4161 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4162 | return BAD_VALUE; |
| 4163 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4164 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4165 | // check if one opened output is not needed any more after disconnecting one device |
| 4166 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4167 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4168 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4169 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4170 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4171 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4172 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4173 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4174 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4175 | mOutputs.keyAt(i)); |
| 4176 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4177 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4178 | } |
| 4179 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4180 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4181 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4182 | { |
| 4183 | if (mHwModules[i]->mHandle == 0) { |
| 4184 | continue; |
| 4185 | } |
| 4186 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 4187 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4188 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4189 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4190 | ALOGV("checkOutputsForDevice(): " |
| 4191 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4192 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | } |
| 4196 | } |
| 4197 | return NO_ERROR; |
| 4198 | } |
| 4199 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4200 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4201 | audio_policy_dev_state_t state, |
| 4202 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4203 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4204 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4205 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4206 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4207 | |
| 4208 | if (audio_device_is_digital(device)) { |
| 4209 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4210 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4211 | } |
| 4212 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4213 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4214 | // first list already open inputs that can be routed to this device |
| 4215 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4216 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4217 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4218 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4219 | inputs.add(mInputs.keyAt(input_index)); |
| 4220 | } |
| 4221 | } |
| 4222 | |
| 4223 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4224 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4225 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 4226 | { |
| 4227 | if (mHwModules[module_idx]->mHandle == 0) { |
| 4228 | continue; |
| 4229 | } |
| 4230 | for (size_t profile_index = 0; |
| 4231 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 4232 | profile_index++) |
| 4233 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4234 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 4235 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4236 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4237 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4238 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4239 | profiles.add(profile); |
| 4240 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 4241 | profile_index, module_idx); |
| 4242 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4243 | } |
| 4244 | } |
| 4245 | } |
| 4246 | |
| 4247 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4248 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4249 | return BAD_VALUE; |
| 4250 | } |
| 4251 | |
| 4252 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4253 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4254 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4255 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4256 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4257 | // nothing to do if one input is already opened for this profile |
| 4258 | size_t input_index; |
| 4259 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4260 | desc = mInputs.valueAt(input_index); |
| 4261 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4262 | if (audio_device_is_digital(device)) { |
| 4263 | devDesc->importAudioPort(profile); |
| 4264 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4265 | break; |
| 4266 | } |
| 4267 | } |
| 4268 | if (input_index != mInputs.size()) { |
| 4269 | continue; |
| 4270 | } |
| 4271 | |
| 4272 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 4273 | desc = new AudioInputDescriptor(profile); |
| 4274 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4275 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4276 | config.sample_rate = desc->mSamplingRate; |
| 4277 | config.channel_mask = desc->mChannelMask; |
| 4278 | config.format = desc->mFormat; |
| 4279 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4280 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4281 | &input, |
| 4282 | &config, |
| 4283 | &desc->mDevice, |
| 4284 | address, |
| 4285 | AUDIO_SOURCE_MIC, |
| 4286 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4287 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4288 | if (status == NO_ERROR) { |
| 4289 | desc->mSamplingRate = config.sample_rate; |
| 4290 | desc->mChannelMask = config.channel_mask; |
| 4291 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4292 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4293 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4294 | char *param = audio_device_address_to_parameter(device, address); |
| 4295 | mpClientInterface->setParameters(input, String8(param)); |
| 4296 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4297 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4298 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4299 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4300 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 4301 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4302 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4303 | } |
| 4304 | |
| 4305 | if (input != 0) { |
| 4306 | addInput(input, desc); |
| 4307 | } |
| 4308 | } // endif input != 0 |
| 4309 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4310 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4311 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4312 | profiles.removeAt(profile_index); |
| 4313 | profile_index--; |
| 4314 | } else { |
| 4315 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4316 | if (audio_device_is_digital(device)) { |
| 4317 | devDesc->importAudioPort(profile); |
| 4318 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4319 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4320 | } |
| 4321 | } // end scan profiles |
| 4322 | |
| 4323 | if (profiles.isEmpty()) { |
| 4324 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4325 | return BAD_VALUE; |
| 4326 | } |
| 4327 | } else { |
| 4328 | // Disconnect |
| 4329 | // check if one opened input is not needed any more after disconnecting one device |
| 4330 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4331 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4332 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4333 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4334 | mInputs.keyAt(input_index)); |
| 4335 | inputs.add(mInputs.keyAt(input_index)); |
| 4336 | } |
| 4337 | } |
| 4338 | // Clear any profiles associated with the disconnected device. |
| 4339 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4340 | if (mHwModules[module_index]->mHandle == 0) { |
| 4341 | continue; |
| 4342 | } |
| 4343 | for (size_t profile_index = 0; |
| 4344 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4345 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4346 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4347 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4348 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4349 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4350 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4351 | } |
| 4352 | } |
| 4353 | } |
| 4354 | } // end disconnect |
| 4355 | |
| 4356 | return NO_ERROR; |
| 4357 | } |
| 4358 | |
| 4359 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4360 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4361 | { |
| 4362 | ALOGV("closeOutput(%d)", output); |
| 4363 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4364 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | if (outputDesc == NULL) { |
| 4366 | ALOGW("closeOutput() unknown output %d", output); |
| 4367 | return; |
| 4368 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4369 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4370 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | // look for duplicated outputs connected to the output being removed. |
| 4372 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4373 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4374 | if (dupOutputDesc->isDuplicated() && |
| 4375 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4376 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4377 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4378 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4379 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4380 | } else { |
| 4381 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4382 | } |
| 4383 | // As all active tracks on duplicated output will be deleted, |
| 4384 | // and as they were also referenced on the other output, the reference |
| 4385 | // count for their stream type must be adjusted accordingly on |
| 4386 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4387 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4388 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4389 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4390 | } |
| 4391 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4392 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4393 | |
| 4394 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4395 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4396 | } |
| 4397 | } |
| 4398 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4399 | nextAudioPortGeneration(); |
| 4400 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4401 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4402 | if (index >= 0) { |
| 4403 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4404 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4405 | mAudioPatches.removeItemsAt(index); |
| 4406 | mpClientInterface->onAudioPatchListUpdate(); |
| 4407 | } |
| 4408 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4409 | AudioParameter param; |
| 4410 | param.add(String8("closing"), String8("true")); |
| 4411 | mpClientInterface->setParameters(output, param.toString()); |
| 4412 | |
| 4413 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4414 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4415 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4419 | { |
| 4420 | ALOGV("closeInput(%d)", input); |
| 4421 | |
| 4422 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4423 | if (inputDesc == NULL) { |
| 4424 | ALOGW("closeInput() unknown input %d", input); |
| 4425 | return; |
| 4426 | } |
| 4427 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4428 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4429 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4430 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4431 | if (index >= 0) { |
| 4432 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4433 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4434 | mAudioPatches.removeItemsAt(index); |
| 4435 | mpClientInterface->onAudioPatchListUpdate(); |
| 4436 | } |
| 4437 | |
| 4438 | mpClientInterface->closeInput(input); |
| 4439 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4440 | } |
| 4441 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4442 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4443 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4444 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4445 | { |
| 4446 | SortedVector<audio_io_handle_t> outputs; |
| 4447 | |
| 4448 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4449 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4450 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4451 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4452 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4453 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4454 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4455 | outputs.add(openOutputs.keyAt(i)); |
| 4456 | } |
| 4457 | } |
| 4458 | return outputs; |
| 4459 | } |
| 4460 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4461 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4462 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4463 | { |
| 4464 | if (outputs1.size() != outputs2.size()) { |
| 4465 | return false; |
| 4466 | } |
| 4467 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4468 | if (outputs1[i] != outputs2[i]) { |
| 4469 | return false; |
| 4470 | } |
| 4471 | } |
| 4472 | return true; |
| 4473 | } |
| 4474 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4475 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4476 | { |
| 4477 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4478 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4479 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4480 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4481 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4482 | // also take into account external policy-related changes: add all outputs which are |
| 4483 | // associated with policies in the "before" and "after" output vectors |
| 4484 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4485 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4486 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4487 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4488 | srcOutputs.add(desc->mIoHandle); |
| 4489 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4490 | } |
| 4491 | } |
| 4492 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4493 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4494 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4495 | dstOutputs.add(desc->mIoHandle); |
| 4496 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4497 | } |
| 4498 | } |
| 4499 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4500 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4501 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4502 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4503 | // mute strategy while moving tracks from one output to another |
| 4504 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4505 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4506 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4507 | setStrategyMute(strategy, true, desc); |
| 4508 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4509 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4510 | sp<AudioSourceDescriptor> source = |
| 4511 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4512 | if (source != 0){ |
| 4513 | connectAudioSource(source); |
| 4514 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | // Move effects associated to this strategy from previous output to new output |
| 4518 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4519 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | } |
| 4521 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4522 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4523 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4524 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4525 | } |
| 4526 | } |
| 4527 | } |
| 4528 | } |
| 4529 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4530 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4531 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4532 | 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] | 4533 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4534 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4535 | 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] | 4536 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4537 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4538 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4539 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4540 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4541 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4542 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4543 | } |
| 4544 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4545 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4546 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4547 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4548 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4549 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4550 | return; |
| 4551 | } |
| 4552 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4553 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4554 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4555 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4556 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4557 | |
| 4558 | // if suspended, restore A2DP output if: |
| 4559 | // ((SCO device is NOT connected) || |
| 4560 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4561 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4562 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4563 | // if not suspended, suspend A2DP output if: |
| 4564 | // (SCO device is connected) && |
| 4565 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4566 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4567 | // |
| 4568 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4569 | if (!isScoConnected || |
| 4570 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4571 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4572 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4573 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4574 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4575 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4576 | |
| 4577 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4578 | mA2dpSuspended = false; |
| 4579 | } |
| 4580 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4581 | if (isScoConnected && |
| 4582 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4583 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4584 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4585 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4586 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4587 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4588 | |
| 4589 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4590 | mA2dpSuspended = true; |
| 4591 | } |
| 4592 | } |
| 4593 | } |
| 4594 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4595 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4596 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4597 | { |
| 4598 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4599 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4600 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4601 | if (index >= 0) { |
| 4602 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4603 | if (patchDesc->mUid != mUidCached) { |
| 4604 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4605 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4606 | return outputDesc->device(); |
| 4607 | } |
| 4608 | } |
| 4609 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4610 | // 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] | 4611 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4612 | // use device for strategy enforced audible |
| 4613 | // 2: we are in call or the strategy phone is active on the output: |
| 4614 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4615 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4616 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4617 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4618 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4619 | // 5: the strategy accessibility is active on the output: |
| 4620 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4621 | // 6: the strategy "respectful" sonification is active on the output: |
| 4622 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4623 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4625 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4626 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4627 | // 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] | 4628 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4629 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4630 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4631 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4632 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4633 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4634 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4635 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4636 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4637 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4638 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4639 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4640 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4641 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4642 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4643 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4644 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4645 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4646 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4647 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4648 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4649 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4650 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4653 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4654 | return device; |
| 4655 | } |
| 4656 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4657 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4658 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4659 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4660 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4661 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4662 | if (index >= 0) { |
| 4663 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4664 | if (patchDesc->mUid != mUidCached) { |
| 4665 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4666 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4667 | return inputDesc->mDevice; |
| 4668 | } |
| 4669 | } |
| 4670 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4671 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
| 4672 | if (isInCall()) { |
| 4673 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4674 | } else if (source != AUDIO_SOURCE_DEFAULT) { |
| 4675 | device = getDeviceAndMixForInputSource(source); |
| 4676 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4677 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4678 | return device; |
| 4679 | } |
| 4680 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4681 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4682 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4683 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4684 | } |
| 4685 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4686 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4687 | return (uint32_t)getStrategy(stream); |
| 4688 | } |
| 4689 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4690 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4691 | // By checking the range of stream before calling getStrategy, we avoid |
| 4692 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4693 | // 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] | 4694 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4695 | return AUDIO_DEVICE_NONE; |
| 4696 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4697 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4698 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4699 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4700 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4701 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4702 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4703 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4704 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4705 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4706 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4707 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4708 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4709 | curDevices |= outputDesc->device(); |
| 4710 | } |
| 4711 | } |
| 4712 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4713 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4714 | |
| 4715 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4716 | and doesn't really need to.*/ |
| 4717 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4718 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4719 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4720 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4721 | return devices; |
| 4722 | } |
| 4723 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4724 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4725 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4726 | 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] | 4727 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 | } |
| 4729 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4730 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4731 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4732 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4733 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4734 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4735 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4736 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4737 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4738 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4739 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4740 | } |
| 4741 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4742 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4743 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4744 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4745 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4746 | updateDevicesAndOutputs(); |
| 4747 | break; |
| 4748 | default: |
| 4749 | break; |
| 4750 | } |
| 4751 | } |
| 4752 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4753 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4754 | |
| 4755 | // skip beacon mute management if a dedicated TTS output is available |
| 4756 | if (mTtsOutputAvailable) { |
| 4757 | return 0; |
| 4758 | } |
| 4759 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4760 | switch(event) { |
| 4761 | case STARTING_OUTPUT: |
| 4762 | mBeaconMuteRefCount++; |
| 4763 | break; |
| 4764 | case STOPPING_OUTPUT: |
| 4765 | if (mBeaconMuteRefCount > 0) { |
| 4766 | mBeaconMuteRefCount--; |
| 4767 | } |
| 4768 | break; |
| 4769 | case STARTING_BEACON: |
| 4770 | mBeaconPlayingRefCount++; |
| 4771 | break; |
| 4772 | case STOPPING_BEACON: |
| 4773 | if (mBeaconPlayingRefCount > 0) { |
| 4774 | mBeaconPlayingRefCount--; |
| 4775 | } |
| 4776 | break; |
| 4777 | } |
| 4778 | |
| 4779 | if (mBeaconMuteRefCount > 0) { |
| 4780 | // any playback causes beacon to be muted |
| 4781 | return setBeaconMute(true); |
| 4782 | } else { |
| 4783 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4784 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4785 | } |
| 4786 | } |
| 4787 | |
| 4788 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4789 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4790 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4791 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4792 | if (mBeaconMuted != mute) { |
| 4793 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4794 | ALOGV("\t muting %d", mute); |
| 4795 | uint32_t maxLatency = 0; |
| 4796 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4797 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4798 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4799 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4800 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4801 | const uint32_t latency = desc->latency() * 2; |
| 4802 | if (latency > maxLatency) { |
| 4803 | maxLatency = latency; |
| 4804 | } |
| 4805 | } |
| 4806 | mBeaconMuted = mute; |
| 4807 | return maxLatency; |
| 4808 | } |
| 4809 | return 0; |
| 4810 | } |
| 4811 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4812 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4813 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4814 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4815 | // Routing |
| 4816 | // see if we have an explicit route |
| 4817 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4818 | // (getStrategy(stream)). |
| 4819 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4820 | // and activity count is non-zero |
| 4821 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4822 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4823 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4824 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4825 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4826 | return route->mDeviceDescriptor->type(); |
| 4827 | } |
| 4828 | } |
| 4829 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | if (fromCache) { |
| 4831 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4832 | strategy, mDeviceForStrategy[strategy]); |
| 4833 | return mDeviceForStrategy[strategy]; |
| 4834 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4835 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4836 | } |
| 4837 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4838 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4839 | { |
| 4840 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4841 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4842 | } |
| 4843 | mPreviousOutputs = mOutputs; |
| 4844 | } |
| 4845 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4846 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4847 | audio_devices_t prevDevice, |
| 4848 | uint32_t delayMs) |
| 4849 | { |
| 4850 | // mute/unmute strategies using an incompatible device combination |
| 4851 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4852 | // if unmuting, unmute only after the specified delay |
| 4853 | if (outputDesc->isDuplicated()) { |
| 4854 | return 0; |
| 4855 | } |
| 4856 | |
| 4857 | uint32_t muteWaitMs = 0; |
| 4858 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4859 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4860 | |
| 4861 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4862 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4863 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4864 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4865 | bool doMute = false; |
| 4866 | |
| 4867 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4868 | doMute = true; |
| 4869 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4870 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4871 | doMute = true; |
| 4872 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4873 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4874 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4875 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4876 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4877 | // skip output if it does not share any device with current output |
| 4878 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4879 | == AUDIO_DEVICE_NONE) { |
| 4880 | continue; |
| 4881 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4882 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4883 | mute ? "muting" : "unmuting", i, curDevice); |
| 4884 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4885 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4886 | if (mute) { |
| 4887 | // FIXME: should not need to double latency if volume could be applied |
| 4888 | // immediately by the audioflinger mixer. We must account for the delay |
| 4889 | // between now and the next time the audioflinger thread for this output |
| 4890 | // will process a buffer (which corresponds to one buffer size, |
| 4891 | // usually 1/2 or 1/4 of the latency). |
| 4892 | if (muteWaitMs < desc->latency() * 2) { |
| 4893 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4894 | } |
| 4895 | } |
| 4896 | } |
| 4897 | } |
| 4898 | } |
| 4899 | } |
| 4900 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4901 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4902 | // different per device volumes |
| 4903 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4904 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4905 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4906 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4907 | if (muteWaitMs < tempMuteWaitMs) { |
| 4908 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4909 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4910 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4911 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4912 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4913 | // make sure that we do not start the temporary mute period too early in case of |
| 4914 | // delayed device change |
| 4915 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4916 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4917 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4918 | } |
| 4919 | } |
| 4920 | } |
| 4921 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4922 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4923 | if (muteWaitMs > delayMs) { |
| 4924 | muteWaitMs -= delayMs; |
| 4925 | usleep(muteWaitMs * 1000); |
| 4926 | return muteWaitMs; |
| 4927 | } |
| 4928 | return 0; |
| 4929 | } |
| 4930 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4931 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4932 | audio_devices_t device, |
| 4933 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4934 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4935 | audio_patch_handle_t *patchHandle, |
| 4936 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4937 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4938 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4939 | AudioParameter param; |
| 4940 | uint32_t muteWaitMs; |
| 4941 | |
| 4942 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4943 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4944 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4945 | return muteWaitMs; |
| 4946 | } |
| 4947 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4948 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4949 | if ((device != AUDIO_DEVICE_NONE) && |
| 4950 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4951 | return 0; |
| 4952 | } |
| 4953 | |
| 4954 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4955 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4956 | |
| 4957 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4958 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4959 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4960 | |
| 4961 | if (device != AUDIO_DEVICE_NONE) { |
| 4962 | outputDesc->mDevice = device; |
| 4963 | } |
| 4964 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4965 | |
| 4966 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4967 | // the requested device is AUDIO_DEVICE_NONE |
| 4968 | // OR the requested device is the same as current device |
| 4969 | // AND force is not specified |
| 4970 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4971 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4972 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4973 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4974 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4975 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4976 | return muteWaitMs; |
| 4977 | } |
| 4978 | |
| 4979 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4980 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4981 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4982 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4983 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4984 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4985 | DeviceVector deviceList; |
| 4986 | if ((address == NULL) || (strlen(address) == 0)) { |
| 4987 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 4988 | } else { |
| 4989 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 4990 | } |
| 4991 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4992 | if (!deviceList.isEmpty()) { |
| 4993 | struct audio_patch patch; |
| 4994 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4995 | patch.num_sources = 1; |
| 4996 | patch.num_sinks = 0; |
| 4997 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4998 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4999 | patch.num_sinks++; |
| 5000 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5001 | ssize_t index; |
| 5002 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5003 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5004 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5005 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5006 | } |
| 5007 | sp< AudioPatch> patchDesc; |
| 5008 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5009 | if (index >= 0) { |
| 5010 | patchDesc = mAudioPatches.valueAt(index); |
| 5011 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5012 | } |
| 5013 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5014 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5015 | &afPatchHandle, |
| 5016 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5017 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 5018 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5019 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5020 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5021 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5022 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5023 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5024 | } else { |
| 5025 | patchDesc->mPatch = patch; |
| 5026 | } |
| 5027 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5028 | if (patchHandle) { |
| 5029 | *patchHandle = patchDesc->mHandle; |
| 5030 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5031 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5032 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5033 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5034 | } |
| 5035 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5036 | |
| 5037 | // inform all input as well |
| 5038 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5039 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5040 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5041 | AudioParameter inputCmd = AudioParameter(); |
| 5042 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 5043 | inputDescriptor->mIoHandle, device); |
| 5044 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 5045 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 5046 | inputCmd.toString(), |
| 5047 | delayMs); |
| 5048 | } |
| 5049 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5050 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5051 | |
| 5052 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5053 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5054 | |
| 5055 | return muteWaitMs; |
| 5056 | } |
| 5057 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5058 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5059 | int delayMs, |
| 5060 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5061 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5062 | ssize_t index; |
| 5063 | if (patchHandle) { |
| 5064 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5065 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5066 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5067 | } |
| 5068 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5069 | return INVALID_OPERATION; |
| 5070 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5071 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5072 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5073 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5074 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5075 | removeAudioPatch(patchDesc->mHandle); |
| 5076 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5077 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5078 | return status; |
| 5079 | } |
| 5080 | |
| 5081 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 5082 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5083 | bool force, |
| 5084 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5085 | { |
| 5086 | status_t status = NO_ERROR; |
| 5087 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5088 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5089 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 5090 | inputDesc->mDevice = device; |
| 5091 | |
| 5092 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 5093 | if (!deviceList.isEmpty()) { |
| 5094 | struct audio_patch patch; |
| 5095 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5096 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 5097 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5098 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 5099 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5100 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5101 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5102 | patch.num_sinks = 1; |
| 5103 | //only one input device for now |
| 5104 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5105 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5106 | ssize_t index; |
| 5107 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5108 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5109 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5110 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5111 | } |
| 5112 | sp< AudioPatch> patchDesc; |
| 5113 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5114 | if (index >= 0) { |
| 5115 | patchDesc = mAudioPatches.valueAt(index); |
| 5116 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5117 | } |
| 5118 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5119 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5120 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5121 | 0); |
| 5122 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5123 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5124 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5125 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5126 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5127 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5128 | } else { |
| 5129 | patchDesc->mPatch = patch; |
| 5130 | } |
| 5131 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5132 | if (patchHandle) { |
| 5133 | *patchHandle = patchDesc->mHandle; |
| 5134 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5135 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5136 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5137 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5138 | } |
| 5139 | } |
| 5140 | } |
| 5141 | return status; |
| 5142 | } |
| 5143 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5144 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5145 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5146 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5147 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5148 | ssize_t index; |
| 5149 | if (patchHandle) { |
| 5150 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5151 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5152 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5153 | } |
| 5154 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5155 | return INVALID_OPERATION; |
| 5156 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5157 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5158 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5159 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5160 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5161 | removeAudioPatch(patchDesc->mHandle); |
| 5162 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5163 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5164 | return status; |
| 5165 | } |
| 5166 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5167 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5168 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5169 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5170 | audio_format_t& format, |
| 5171 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5172 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5173 | { |
| 5174 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5175 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5176 | // |
| 5177 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5178 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5179 | |
| 5180 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 5181 | { |
| 5182 | if (mHwModules[i]->mHandle == 0) { |
| 5183 | continue; |
| 5184 | } |
| 5185 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 5186 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5187 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5188 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5189 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5190 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5191 | format, |
| 5192 | &format /*updatedFormat*/, |
| 5193 | channelMask, |
| 5194 | &channelMask /*updatedChannelMask*/, |
| 5195 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5196 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5197 | return profile; |
| 5198 | } |
| 5199 | } |
| 5200 | } |
| 5201 | return NULL; |
| 5202 | } |
| 5203 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5204 | |
| 5205 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5206 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5207 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5208 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5209 | audio_devices_t selectedDeviceFromMix = |
| 5210 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5211 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5212 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5213 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5214 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5215 | return getDeviceForInputSource(inputSource); |
| 5216 | } |
| 5217 | |
| 5218 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5219 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5220 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5221 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5222 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5223 | return route->mDeviceDescriptor->type(); |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5228 | } |
| 5229 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5230 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5231 | int index, |
| 5232 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5233 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5234 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5235 | |
| 5236 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5237 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5238 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5239 | // the ringtone volume |
| 5240 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5241 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5242 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5243 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5244 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5245 | } |
| 5246 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5247 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5248 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5249 | // - always attenuate notifications volume by 6dB |
| 5250 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5251 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5252 | // - if music is playing, always limit the volume to current music volume, |
| 5253 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5254 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5255 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5256 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5257 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5258 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 5259 | AUDIO_DEVICE_OUT_USB_HEADSET)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5260 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5261 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5262 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5263 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5264 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5265 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5266 | // when the phone is ringing we must consider that music could have been paused just before |
| 5267 | // by the music application and behave as if music was active if the last music track was |
| 5268 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5269 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5270 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5271 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5272 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5273 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5274 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5275 | musicDevice), |
| 5276 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5277 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5278 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5279 | if (volumeDB > minVolDB) { |
| 5280 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5281 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5282 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5283 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5284 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5285 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5286 | // intended to be played |
| 5287 | if ((volumeDB > -96.0f) && |
| 5288 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5289 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5290 | stream, device, |
| 5291 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5292 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5293 | } |
| 5294 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5295 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5296 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5297 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5298 | } |
| 5299 | } |
| 5300 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5301 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5302 | } |
| 5303 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5304 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5305 | int index, |
| 5306 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5307 | audio_devices_t device, |
| 5308 | int delayMs, |
| 5309 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5310 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5311 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5312 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5313 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5314 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5315 | return NO_ERROR; |
| 5316 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5317 | audio_policy_forced_cfg_t forceUseForComm = |
| 5318 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5319 | // 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] | 5320 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5321 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5322 | 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] | 5323 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5324 | return INVALID_OPERATION; |
| 5325 | } |
| 5326 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5327 | if (device == AUDIO_DEVICE_NONE) { |
| 5328 | device = outputDesc->device(); |
| 5329 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5330 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5331 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5332 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5333 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5334 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5335 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5336 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5337 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5338 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5339 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5340 | float voiceVolume; |
| 5341 | // 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] | 5342 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5343 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5344 | } else { |
| 5345 | voiceVolume = 1.0; |
| 5346 | } |
| 5347 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5348 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5349 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5350 | mLastVoiceVolume = voiceVolume; |
| 5351 | } |
| 5352 | } |
| 5353 | |
| 5354 | return NO_ERROR; |
| 5355 | } |
| 5356 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5357 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5358 | audio_devices_t device, |
| 5359 | int delayMs, |
| 5360 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5361 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5362 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5363 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5364 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5365 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5366 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5367 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5368 | device, |
| 5369 | delayMs, |
| 5370 | force); |
| 5371 | } |
| 5372 | } |
| 5373 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5374 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5375 | bool on, |
| 5376 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5377 | int delayMs, |
| 5378 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5379 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5380 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5381 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5382 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5383 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5384 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5385 | } |
| 5386 | } |
| 5387 | } |
| 5388 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5389 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5390 | bool on, |
| 5391 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5392 | int delayMs, |
| 5393 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5394 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5395 | if (device == AUDIO_DEVICE_NONE) { |
| 5396 | device = outputDesc->device(); |
| 5397 | } |
| 5398 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5399 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5400 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5401 | |
| 5402 | if (on) { |
| 5403 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5404 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5405 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5406 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5407 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5408 | } |
| 5409 | } |
| 5410 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5411 | outputDesc->mMuteCount[stream]++; |
| 5412 | } else { |
| 5413 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5414 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5415 | return; |
| 5416 | } |
| 5417 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5418 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5419 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5420 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5421 | device, |
| 5422 | delayMs); |
| 5423 | } |
| 5424 | } |
| 5425 | } |
| 5426 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5427 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5428 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5429 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5430 | if(!hasPrimaryOutput()) { |
| 5431 | return; |
| 5432 | } |
| 5433 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5434 | // if the stream pertains to sonification strategy and we are in call we must |
| 5435 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5436 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5437 | // interfere with the device used for phone strategy |
| 5438 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5439 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5440 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5441 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5442 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5443 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5444 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5445 | stream, starting, outputDesc->mDevice, stateChange); |
| 5446 | if (outputDesc->mRefCount[stream]) { |
| 5447 | int muteCount = 1; |
| 5448 | if (stateChange) { |
| 5449 | muteCount = outputDesc->mRefCount[stream]; |
| 5450 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5451 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5452 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5453 | for (int i = 0; i < muteCount; i++) { |
| 5454 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5455 | } |
| 5456 | } else { |
| 5457 | ALOGV("handleIncallSonification() high visibility"); |
| 5458 | if (outputDesc->device() & |
| 5459 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5460 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5461 | for (int i = 0; i < muteCount; i++) { |
| 5462 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5463 | } |
| 5464 | } |
| 5465 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5466 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5467 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5468 | } else { |
| 5469 | mpClientInterface->stopTone(); |
| 5470 | } |
| 5471 | } |
| 5472 | } |
| 5473 | } |
| 5474 | } |
| 5475 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5476 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5477 | { |
| 5478 | // flags to stream type mapping |
| 5479 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5480 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5481 | } |
| 5482 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5483 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5484 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5485 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5486 | return AUDIO_STREAM_TTS; |
| 5487 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5488 | |
| 5489 | // usage to stream type mapping |
| 5490 | switch (attr->usage) { |
| 5491 | case AUDIO_USAGE_MEDIA: |
| 5492 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5493 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5494 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5495 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5496 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5497 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5498 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5499 | return AUDIO_STREAM_SYSTEM; |
| 5500 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5501 | return AUDIO_STREAM_VOICE_CALL; |
| 5502 | |
| 5503 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5504 | return AUDIO_STREAM_DTMF; |
| 5505 | |
| 5506 | case AUDIO_USAGE_ALARM: |
| 5507 | return AUDIO_STREAM_ALARM; |
| 5508 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5509 | return AUDIO_STREAM_RING; |
| 5510 | |
| 5511 | case AUDIO_USAGE_NOTIFICATION: |
| 5512 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5513 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5514 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5515 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5516 | return AUDIO_STREAM_NOTIFICATION; |
| 5517 | |
| 5518 | case AUDIO_USAGE_UNKNOWN: |
| 5519 | default: |
| 5520 | return AUDIO_STREAM_MUSIC; |
| 5521 | } |
| 5522 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5523 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5524 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5525 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5526 | // has flags that map to a strategy? |
| 5527 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5528 | return true; |
| 5529 | } |
| 5530 | |
| 5531 | // has known usage? |
| 5532 | switch (paa->usage) { |
| 5533 | case AUDIO_USAGE_UNKNOWN: |
| 5534 | case AUDIO_USAGE_MEDIA: |
| 5535 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5536 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5537 | case AUDIO_USAGE_ALARM: |
| 5538 | case AUDIO_USAGE_NOTIFICATION: |
| 5539 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5540 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5541 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5542 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5543 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5544 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5545 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5546 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5547 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5548 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5549 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5550 | break; |
| 5551 | default: |
| 5552 | return false; |
| 5553 | } |
| 5554 | return true; |
| 5555 | } |
| 5556 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5557 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5558 | routing_strategy strategy, uint32_t inPastMs, |
| 5559 | nsecs_t sysTime) const |
| 5560 | { |
| 5561 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5562 | sysTime = systemTime(); |
| 5563 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5564 | 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] | 5565 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5566 | (NUM_STRATEGIES == strategy)) && |
| 5567 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5568 | return true; |
| 5569 | } |
| 5570 | } |
| 5571 | return false; |
| 5572 | } |
| 5573 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5574 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5575 | { |
| 5576 | return mEngine->getForceUse(usage); |
| 5577 | } |
| 5578 | |
| 5579 | bool AudioPolicyManager::isInCall() |
| 5580 | { |
| 5581 | return isStateInCall(mEngine->getPhoneState()); |
| 5582 | } |
| 5583 | |
| 5584 | bool AudioPolicyManager::isStateInCall(int state) |
| 5585 | { |
| 5586 | return is_state_in_call(state); |
| 5587 | } |
| 5588 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5589 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5590 | { |
| 5591 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5592 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5593 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5594 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5595 | stopAudioSource(sourceDesc->getHandle()); |
| 5596 | } |
| 5597 | } |
| 5598 | |
| 5599 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5600 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5601 | bool release = false; |
| 5602 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5603 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5604 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5605 | source->ext.device.type == deviceDesc->type()) { |
| 5606 | release = true; |
| 5607 | } |
| 5608 | } |
| 5609 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5610 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5611 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5612 | sink->ext.device.type == deviceDesc->type()) { |
| 5613 | release = true; |
| 5614 | } |
| 5615 | } |
| 5616 | if (release) { |
| 5617 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5618 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5619 | } |
| 5620 | } |
| 5621 | } |
| 5622 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5623 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5624 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5625 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5626 | // TODO Set this based on Config properties. |
| 5627 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5628 | |
| 5629 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5630 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5631 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5632 | |
| 5633 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5634 | bool supportsAC3 = false; |
| 5635 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5636 | bool supportsIEC61937 = false; |
| 5637 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5638 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5639 | switch (format) { |
| 5640 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5641 | supportsAC3 = true; |
| 5642 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5643 | case AUDIO_FORMAT_E_AC3: |
| 5644 | case AUDIO_FORMAT_DTS: |
| 5645 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5646 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5647 | break; |
| 5648 | case AUDIO_FORMAT_IEC61937: |
| 5649 | supportsIEC61937 = true; |
| 5650 | break; |
| 5651 | default: |
| 5652 | break; |
| 5653 | } |
| 5654 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5655 | |
| 5656 | // Modify formats based on surround preferences. |
| 5657 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5658 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5659 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5660 | // Remove surround sound related formats. |
| 5661 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5662 | audio_format_t format = formats[formatIndex]; |
| 5663 | switch(format) { |
| 5664 | case AUDIO_FORMAT_AC3: |
| 5665 | case AUDIO_FORMAT_E_AC3: |
| 5666 | case AUDIO_FORMAT_DTS: |
| 5667 | case AUDIO_FORMAT_DTS_HD: |
| 5668 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5669 | formats.removeAt(formatIndex); |
| 5670 | break; |
| 5671 | default: |
| 5672 | formatIndex++; // keep it |
| 5673 | break; |
| 5674 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5675 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5676 | supportsAC3 = false; |
| 5677 | supportsOtherSurround = false; |
| 5678 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5679 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5680 | } else { // AUTO or ALWAYS |
| 5681 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5682 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5683 | && !supportsAC3) { |
| 5684 | formats.add(AUDIO_FORMAT_AC3); |
| 5685 | supportsAC3 = true; |
| 5686 | } |
| 5687 | |
| 5688 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5689 | // This assumes that if any of these formats are reported by the HAL |
| 5690 | // then the report is valid and should not be modified. |
| 5691 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5692 | && !supportsOtherSurround) { |
| 5693 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5694 | formats.add(AUDIO_FORMAT_DTS); |
| 5695 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5696 | supportsOtherSurround = true; |
| 5697 | } |
| 5698 | |
| 5699 | // Add support for IEC61937 if any raw surround supported. |
| 5700 | // The HAL could do this but add it here, just in case. |
| 5701 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5702 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5703 | supportsIEC61937 = true; |
| 5704 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5705 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | // Modify the list of channel masks supported. |
| 5709 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5710 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5711 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5712 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5713 | |
| 5714 | // If NEVER, then remove support for channelMasks > stereo. |
| 5715 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5716 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5717 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5718 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5719 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5720 | channelMasks.removeAt(maskIndex); |
| 5721 | } else { |
| 5722 | maskIndex++; |
| 5723 | } |
| 5724 | } |
| 5725 | // If ALWAYS, then make sure we at least support 5.1 |
| 5726 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5727 | bool supports5dot1 = false; |
| 5728 | // Are there any channel masks that can be considered "surround"? |
| 5729 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5730 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5731 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5732 | supports5dot1 = true; |
| 5733 | break; |
| 5734 | } |
| 5735 | } |
| 5736 | // If not then add 5.1 support. |
| 5737 | if (!supports5dot1) { |
| 5738 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5739 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5740 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5741 | } |
| 5742 | } |
| 5743 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5744 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5745 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5746 | AudioProfileVector &profiles) |
| 5747 | { |
| 5748 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5749 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5750 | // Format MUST be checked first to update the list of AudioProfile |
| 5751 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5752 | reply = mpClientInterface->getParameters( |
| 5753 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5754 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5755 | AudioParameter repliedParameters(reply); |
| 5756 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5757 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5758 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5759 | return; |
| 5760 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5761 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5762 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5763 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5764 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5765 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5766 | } |
| 5767 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5768 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5769 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5770 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5771 | ChannelsVector channelMasks; |
| 5772 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5773 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5774 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5775 | |
| 5776 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5777 | reply = mpClientInterface->getParameters( |
| 5778 | ioHandle, |
| 5779 | requestedParameters.toString() + ";" + |
| 5780 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5781 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5782 | AudioParameter repliedParameters(reply); |
| 5783 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5784 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5785 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5786 | } |
| 5787 | } |
| 5788 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5789 | reply = mpClientInterface->getParameters(ioHandle, |
| 5790 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5791 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5792 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5793 | AudioParameter repliedParameters(reply); |
| 5794 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5795 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5796 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5797 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5798 | filterSurroundChannelMasks(&channelMasks); |
| 5799 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5800 | } |
| 5801 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5802 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5803 | } |
| 5804 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5805 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5806 | }; // namespace android |