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 | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 641 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 642 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 643 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 644 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 645 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 646 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 647 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 648 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 649 | |
| 650 | // check for device and output changes triggered by new force usage |
| 651 | checkA2dpSuspend(); |
| 652 | checkOutputForAllStrategies(); |
| 653 | updateDevicesAndOutputs(); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 654 | |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 655 | //FIXME: workaround for truncated touch sounds |
| 656 | // to be removed when the problem is handled by system UI |
| 657 | uint32_t delayMs = 0; |
| 658 | uint32_t waitMs = 0; |
| 659 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { |
| 660 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; |
| 661 | } |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 662 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 663 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 664 | waitMs = updateCallRouting(newDevice, delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 665 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 666 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 667 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 668 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 669 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 670 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), |
| 671 | delayMs); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 672 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 673 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 674 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 678 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 679 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 680 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; |
| 681 | audio_devices_t newDevice = getNewInputDevice(activeDesc); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 682 | // 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] | 683 | if (activeDesc->mProfile->getSupportedDevices().types() & |
| 684 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { |
| 685 | setInputDevice(activeDesc->mIoHandle, newDevice); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 686 | } else { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 687 | closeInput(activeDesc->mIoHandle); |
Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 688 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 689 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 692 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | { |
| 694 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 695 | } |
| 696 | |
| 697 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 698 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 699 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 700 | audio_devices_t device, |
| 701 | uint32_t samplingRate, |
| 702 | audio_format_t format, |
| 703 | audio_channel_mask_t channelMask, |
| 704 | audio_output_flags_t flags) |
| 705 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 706 | // only retain flags that will drive the direct output profile selection |
| 707 | // if explicitly requested |
| 708 | static const uint32_t kRelevantFlags = |
| 709 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 710 | flags = |
| 711 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 712 | |
| 713 | sp<IOProfile> profile; |
| 714 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 715 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 716 | if (mHwModules[i]->mHandle == 0) { |
| 717 | continue; |
| 718 | } |
| 719 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 720 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 721 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 722 | samplingRate, NULL /*updatedSamplingRate*/, |
| 723 | format, NULL /*updatedFormat*/, |
| 724 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 725 | flags)) { |
| 726 | continue; |
| 727 | } |
| 728 | // reject profiles not corresponding to a device currently available |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 729 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 730 | continue; |
| 731 | } |
| 732 | // 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] | 733 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 734 | continue; |
| 735 | } |
| 736 | profile = curProfile; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 737 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 738 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 739 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 740 | } |
| 741 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 742 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 745 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 746 | uint32_t samplingRate, |
| 747 | audio_format_t format, |
| 748 | audio_channel_mask_t channelMask, |
| 749 | audio_output_flags_t flags, |
| 750 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 751 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 752 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 753 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 754 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 755 | device, stream, samplingRate, format, channelMask, flags); |
| 756 | |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 757 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, uid_t{0} /*Invalid uid*/, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 758 | stream, samplingRate,format, channelMask, |
| 759 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 762 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 763 | audio_io_handle_t *output, |
| 764 | audio_session_t session, |
| 765 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 766 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 767 | const audio_config_t *config, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 768 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 769 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 770 | audio_port_handle_t *portId) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 771 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 772 | audio_attributes_t attributes; |
| 773 | if (attr != NULL) { |
| 774 | if (!isValidAttributes(attr)) { |
| 775 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 776 | attr->usage, attr->content_type, attr->flags, |
| 777 | attr->tags); |
| 778 | return BAD_VALUE; |
| 779 | } |
| 780 | attributes = *attr; |
| 781 | } else { |
| 782 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 783 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 784 | return BAD_VALUE; |
| 785 | } |
| 786 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 787 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 788 | |
| 789 | // TODO: check for existing client for this port ID |
| 790 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 791 | *portId = AudioPort::getNextUniqueId(); |
| 792 | } |
| 793 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 794 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 795 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 796 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 797 | if (!audio_has_proportional_frames(config->format)) { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 798 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 799 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 800 | *stream = streamTypefromAttributesInt(&attributes); |
| 801 | *output = desc->mIoHandle; |
| 802 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 803 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 804 | } |
| 805 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 806 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 807 | return BAD_VALUE; |
| 808 | } |
| 809 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 810 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 811 | " session %d selectedDeviceId %d", |
| 812 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 813 | session, selectedDeviceId); |
| 814 | |
| 815 | *stream = streamTypefromAttributesInt(&attributes); |
| 816 | |
| 817 | // Explicit routing? |
| 818 | sp<DeviceDescriptor> deviceDesc; |
| 819 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 820 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 821 | deviceDesc = mAvailableOutputDevices[i]; |
| 822 | break; |
| 823 | } |
| 824 | } |
| 825 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 826 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 827 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 828 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 829 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 830 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 831 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 832 | } |
| 833 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 834 | 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] | 835 | device, config->sample_rate, config->format, config->channel_mask, flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 836 | |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 837 | *output = getOutputForDevice(device, session, uid, *stream, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 838 | config->sample_rate, config->format, config->channel_mask, |
| 839 | flags, &config->offload_info); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 840 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 841 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 842 | return INVALID_OPERATION; |
| 843 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 844 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 845 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 849 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 850 | audio_session_t session __unused, |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 851 | uid_t clientUid, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 852 | audio_stream_type_t stream, |
| 853 | uint32_t samplingRate, |
| 854 | audio_format_t format, |
| 855 | audio_channel_mask_t channelMask, |
| 856 | audio_output_flags_t flags, |
| 857 | const audio_offload_info_t *offloadInfo) |
| 858 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 859 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 860 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 861 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 862 | #ifdef AUDIO_POLICY_TEST |
| 863 | if (mCurOutput != 0) { |
| 864 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 865 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 866 | |
| 867 | if (mTestOutputs[mCurOutput] == 0) { |
| 868 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 869 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 870 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 871 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 872 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 873 | outputDesc->mFlags = |
| 874 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 875 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 876 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 877 | config.sample_rate = mTestSamplingRate; |
| 878 | config.channel_mask = mTestChannels; |
| 879 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 880 | if (offloadInfo != NULL) { |
| 881 | config.offload_info = *offloadInfo; |
| 882 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 883 | status = mpClientInterface->openOutput(0, |
| 884 | &mTestOutputs[mCurOutput], |
| 885 | &config, |
| 886 | &outputDesc->mDevice, |
| 887 | String8(""), |
| 888 | &outputDesc->mLatency, |
| 889 | outputDesc->mFlags); |
| 890 | if (status == NO_ERROR) { |
| 891 | outputDesc->mSamplingRate = config.sample_rate; |
| 892 | outputDesc->mFormat = config.format; |
| 893 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 894 | AudioParameter outputCmd = AudioParameter(); |
| 895 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 896 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 897 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 898 | } |
| 899 | } |
| 900 | return mTestOutputs[mCurOutput]; |
| 901 | } |
| 902 | #endif //AUDIO_POLICY_TEST |
| 903 | |
| 904 | // open a direct output if required by specified parameters |
| 905 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 906 | // and all common behaviors are driven by checking only the direct flag |
| 907 | // this should normally be set appropriately in the policy configuration file |
| 908 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 909 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 910 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 911 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 912 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 913 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 914 | // only allow deep buffering for music stream type |
| 915 | if (stream != AUDIO_STREAM_MUSIC) { |
| 916 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 917 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ |
| 918 | flags == AUDIO_OUTPUT_FLAG_NONE && |
| 919 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { |
| 920 | // use DEEP_BUFFER as default output for music stream type |
| 921 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 922 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 923 | if (stream == AUDIO_STREAM_TTS) { |
| 924 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 925 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 926 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 927 | sp<IOProfile> profile; |
| 928 | |
| 929 | // 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] | 930 | // and not explicitly requested |
| 931 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 932 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 933 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 934 | goto non_direct_output; |
| 935 | } |
| 936 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 937 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. |
| 938 | // This prevents creating an offloaded track and tearing it down immediately after start |
| 939 | // when audioflinger detects there is an active non offloadable effect. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 941 | // This may prevent offloading in rare situations where effects are left active by apps |
| 942 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 943 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 944 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 945 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 946 | profile = getProfileForDirectOutput(device, |
| 947 | samplingRate, |
| 948 | format, |
| 949 | channelMask, |
| 950 | (audio_output_flags_t)flags); |
| 951 | } |
| 952 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 953 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 954 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 955 | |
| 956 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 957 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 958 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 959 | outputDesc = desc; |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 960 | // reuse direct output if currently open by the same client |
| 961 | // and configured with same parameters |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 962 | if ((samplingRate == outputDesc->mSamplingRate) && |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 963 | audio_formats_match(format, outputDesc->mFormat) && |
| 964 | (channelMask == outputDesc->mChannelMask)) { |
| 965 | if (clientUid == outputDesc->mDirectClientUid) { |
| 966 | outputDesc->mDirectOpenCount++; |
| 967 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 968 | return mOutputs.keyAt(i); |
| 969 | } else { |
| 970 | ALOGV("getOutput() do not reuse direct output because current client (%ld) " |
| 971 | "is not the same as requesting client (%ld)", |
| 972 | (long)outputDesc->mDirectClientUid, (long)clientUid); |
| 973 | goto non_direct_output; |
| 974 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | } |
| 978 | // close direct output if currently open and configured with different parameters |
| 979 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 980 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 982 | |
| 983 | // if the selected profile is offloaded and no offload info was specified, |
| 984 | // create a default one |
| 985 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 986 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 987 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 988 | defaultOffloadInfo.sample_rate = samplingRate; |
| 989 | defaultOffloadInfo.channel_mask = channelMask; |
| 990 | defaultOffloadInfo.format = format; |
| 991 | defaultOffloadInfo.stream_type = stream; |
| 992 | defaultOffloadInfo.bit_rate = 0; |
| 993 | defaultOffloadInfo.duration_us = -1; |
| 994 | defaultOffloadInfo.has_video = true; // conservative |
| 995 | defaultOffloadInfo.is_streaming = true; // likely |
| 996 | offloadInfo = &defaultOffloadInfo; |
| 997 | } |
| 998 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 999 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1000 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1001 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1002 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1003 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1004 | config.sample_rate = samplingRate; |
| 1005 | config.channel_mask = channelMask; |
| 1006 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 1007 | if (offloadInfo != NULL) { |
| 1008 | config.offload_info = *offloadInfo; |
| 1009 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1010 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1011 | &output, |
| 1012 | &config, |
| 1013 | &outputDesc->mDevice, |
| 1014 | String8(""), |
| 1015 | &outputDesc->mLatency, |
| 1016 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1017 | |
| 1018 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1019 | if (status != NO_ERROR || |
| 1020 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1021 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1022 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1023 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 1024 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 1025 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 1026 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1027 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1028 | mpClientInterface->closeOutput(output); |
| 1029 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1030 | // 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] | 1031 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1032 | goto non_direct_output; |
| 1033 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1034 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1035 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1036 | outputDesc->mSamplingRate = config.sample_rate; |
| 1037 | outputDesc->mChannelMask = config.channel_mask; |
| 1038 | outputDesc->mFormat = config.format; |
| 1039 | outputDesc->mRefCount[stream] = 0; |
| 1040 | outputDesc->mStopTime[stream] = 0; |
| 1041 | outputDesc->mDirectOpenCount = 1; |
Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 1042 | outputDesc->mDirectClientUid = clientUid; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1043 | addOutput(output, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1044 | mPreviousOutputs = mOutputs; |
| 1045 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1046 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1047 | return output; |
| 1048 | } |
| 1049 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1050 | non_direct_output: |
Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1051 | |
| 1052 | // A request for HW A/V sync cannot fallback to a mixed output because time |
| 1053 | // stamps are embedded in audio data |
| 1054 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 1055 | return AUDIO_IO_HANDLE_NONE; |
| 1056 | } |
| 1057 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1058 | // ignoring channel mask due to downmix capability in mixer |
| 1059 | |
| 1060 | // open a non direct output |
| 1061 | |
| 1062 | // for non direct outputs, only PCM is supported |
| 1063 | if (audio_is_linear_pcm(format)) { |
| 1064 | // get which output is suitable for the specified stream. The actual |
| 1065 | // routing change will happen when startOutput() will be called |
| 1066 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1067 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1068 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1069 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1070 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1071 | } |
| 1072 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1073 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1074 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1075 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1076 | |
| 1077 | return output; |
| 1078 | } |
| 1079 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1080 | 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] | 1081 | audio_output_flags_t flags, |
| 1082 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1083 | { |
| 1084 | // select one output among several that provide a path to a particular device or set of |
| 1085 | // devices (the list was previously build by getOutputsForDevice()). |
| 1086 | // The priority is as follows: |
| 1087 | // 1: the output with the highest number of requested policy flags |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1088 | // 2: the output with the bit depth the closest to the requested one |
| 1089 | // 3: the primary output |
| 1090 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1091 | |
| 1092 | if (outputs.size() == 0) { |
| 1093 | return 0; |
| 1094 | } |
| 1095 | if (outputs.size() == 1) { |
| 1096 | return outputs[0]; |
| 1097 | } |
| 1098 | |
| 1099 | int maxCommonFlags = 0; |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1100 | audio_io_handle_t outputForFlags = 0; |
| 1101 | audio_io_handle_t outputForPrimary = 0; |
| 1102 | audio_io_handle_t outputForFormat = 0; |
| 1103 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; |
| 1104 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1105 | |
| 1106 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1107 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1108 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1109 | // if a valid format is specified, skip output if not compatible |
| 1110 | if (format != AUDIO_FORMAT_INVALID) { |
| 1111 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1112 | if (!audio_formats_match(format, outputDesc->mFormat)) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1113 | continue; |
| 1114 | } |
| 1115 | } else if (!audio_is_linear_pcm(format)) { |
| 1116 | continue; |
| 1117 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1118 | if (AudioPort::isBetterFormatMatch( |
| 1119 | outputDesc->mFormat, bestFormat, format)) { |
| 1120 | outputForFormat = outputs[i]; |
| 1121 | bestFormat = outputDesc->mFormat; |
| 1122 | } |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1125 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1126 | if (commonFlags >= maxCommonFlags) { |
| 1127 | if (commonFlags == maxCommonFlags) { |
| 1128 | if (AudioPort::isBetterFormatMatch( |
| 1129 | outputDesc->mFormat, bestFormatForFlags, format)) { |
| 1130 | outputForFlags = outputs[i]; |
| 1131 | bestFormatForFlags = outputDesc->mFormat; |
| 1132 | } |
| 1133 | } else { |
| 1134 | outputForFlags = outputs[i]; |
| 1135 | maxCommonFlags = commonFlags; |
| 1136 | bestFormatForFlags = outputDesc->mFormat; |
| 1137 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1138 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1139 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1140 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1141 | outputForPrimary = outputs[i]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1146 | if (outputForFlags != 0) { |
| 1147 | return outputForFlags; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1148 | } |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1149 | if (outputForFormat != 0) { |
| 1150 | return outputForFormat; |
| 1151 | } |
| 1152 | if (outputForPrimary != 0) { |
| 1153 | return outputForPrimary; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | return outputs[0]; |
| 1157 | } |
| 1158 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1159 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1160 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1161 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1162 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1163 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1164 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1165 | ssize_t index = mOutputs.indexOfKey(output); |
| 1166 | if (index < 0) { |
| 1167 | ALOGW("startOutput() unknown output %d", output); |
| 1168 | return BAD_VALUE; |
| 1169 | } |
| 1170 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1171 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1172 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1173 | // Routing? |
| 1174 | mOutputRoutes.incRouteActivity(session); |
| 1175 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1176 | audio_devices_t newDevice; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1177 | AudioMix *policyMix = NULL; |
| 1178 | const char *address = NULL; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1179 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1180 | policyMix = outputDesc->mPolicyMix; |
| 1181 | address = policyMix->mDeviceAddress.string(); |
| 1182 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 1183 | newDevice = policyMix->mDeviceType; |
| 1184 | } else { |
| 1185 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1186 | } |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1187 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1188 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1189 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1190 | } else { |
| 1191 | newDevice = AUDIO_DEVICE_NONE; |
| 1192 | } |
| 1193 | |
| 1194 | uint32_t delayMs = 0; |
| 1195 | |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1196 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1197 | |
| 1198 | if (status != NO_ERROR) { |
| 1199 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1200 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1201 | } |
| 1202 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1203 | // of type MIX_TYPE_RECORDERS |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1204 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && |
| 1205 | policyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1206 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1207 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1208 | address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1209 | "remote-submix"); |
| 1210 | } |
| 1211 | |
| 1212 | if (delayMs != 0) { |
| 1213 | usleep(delayMs * 1000); |
| 1214 | } |
| 1215 | |
| 1216 | return status; |
| 1217 | } |
| 1218 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1219 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1220 | audio_stream_type_t stream, |
| 1221 | audio_devices_t device, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1222 | const char *address, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1223 | uint32_t *delayMs) |
| 1224 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1225 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1226 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1227 | |
| 1228 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1229 | if (stream == AUDIO_STREAM_TTS) { |
| 1230 | ALOGV("\t found BEACON stream"); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1231 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1232 | return INVALID_OPERATION; |
| 1233 | } else { |
| 1234 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1235 | } |
| 1236 | } else { |
| 1237 | // some playback other than beacon starts |
| 1238 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1239 | } |
| 1240 | |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1241 | // 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] | 1242 | // check active before incrementing usage count |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1243 | bool force = !outputDesc->isActive() && |
| 1244 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1245 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1246 | // increment usage count for this stream on the requested output: |
| 1247 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1248 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1249 | outputDesc->changeRefCount(stream, 1); |
| 1250 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 1251 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1252 | selectOutputForMusicEffects(); |
| 1253 | } |
| 1254 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1255 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1256 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1257 | if (device == AUDIO_DEVICE_NONE) { |
| 1258 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1259 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 1260 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1261 | routing_strategy strategy = getStrategy(stream); |
| 1262 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1263 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1264 | (beaconMuteLatency > 0); |
| 1265 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1266 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1267 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1268 | if (desc != outputDesc) { |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1269 | // force a device change if any other output is: |
| 1270 | // - managed by the same hw module |
| 1271 | // - has a current device selection that differs from selected device. |
| 1272 | // - supports currently selected device |
| 1273 | // - has an active audio patch |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1274 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1275 | // change the device currently selected by the other active output. |
| 1276 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1277 | desc->device() != device && |
| 1278 | desc->supportedDevices() & device && |
| 1279 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1280 | force = true; |
| 1281 | } |
| 1282 | // 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] | 1283 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1284 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1285 | uint32_t latency = desc->latency(); |
| 1286 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1287 | waitMs = latency; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1291 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1292 | |
| 1293 | // handle special case for sonification while in call |
| 1294 | if (isInCall()) { |
| 1295 | handleIncallSonification(stream, true, false); |
| 1296 | } |
| 1297 | |
| 1298 | // apply volume rules for current stream and device if necessary |
| 1299 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1300 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1301 | outputDesc, |
| 1302 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1303 | |
| 1304 | // update the outputs if starting an output with a stream that can affect notification |
| 1305 | // routing |
| 1306 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1307 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1308 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1309 | if (strategy == STRATEGY_SONIFICATION) { |
| 1310 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1311 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1312 | |
| 1313 | if (waitMs > muteWaitMs) { |
| 1314 | *delayMs = waitMs - muteWaitMs; |
| 1315 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1316 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1317 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1318 | return NO_ERROR; |
| 1319 | } |
| 1320 | |
| 1321 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1322 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1323 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1324 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1325 | { |
| 1326 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1327 | ssize_t index = mOutputs.indexOfKey(output); |
| 1328 | if (index < 0) { |
| 1329 | ALOGW("stopOutput() unknown output %d", output); |
| 1330 | return BAD_VALUE; |
| 1331 | } |
| 1332 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1333 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1334 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1335 | if (outputDesc->mRefCount[stream] == 1) { |
| 1336 | // Automatically disable the remote submix input when output is stopped on a |
| 1337 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1338 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1339 | outputDesc->mPolicyMix != NULL && |
| 1340 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1341 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1342 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1343 | outputDesc->mPolicyMix->mDeviceAddress, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1344 | "remote-submix"); |
| 1345 | } |
| 1346 | } |
| 1347 | |
| 1348 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1349 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1350 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1351 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1352 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1353 | |
| 1354 | if (forceDeviceUpdate) { |
| 1355 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1356 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1359 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1362 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1363 | audio_stream_type_t stream, |
| 1364 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1365 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1366 | // always handle stream stop, check which stream type is stopping |
| 1367 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1368 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1369 | // handle special case for sonification while in call |
| 1370 | if (isInCall()) { |
| 1371 | handleIncallSonification(stream, false, false); |
| 1372 | } |
| 1373 | |
| 1374 | if (outputDesc->mRefCount[stream] > 0) { |
| 1375 | // decrement usage count of this stream on the output |
| 1376 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1377 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1378 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1379 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1380 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1381 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1382 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1383 | // the track stop() command is received and at that time the audio track buffer can |
| 1384 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1385 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1386 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1387 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1388 | |
| 1389 | // force restoring the device selection on other active outputs if it differs from the |
| 1390 | // one being selected for this output |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1391 | uint32_t delayMs = outputDesc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1392 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1393 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1394 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1395 | desc->isActive() && |
| 1396 | outputDesc->sharesHwModuleWith(desc) && |
| 1397 | (newDevice != desc->device())) { |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1398 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); |
| 1399 | bool force = desc->device() != newDevice2; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1400 | setOutputDevice(desc, |
Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1401 | newDevice2, |
| 1402 | force, |
Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1403 | delayMs); |
| 1404 | // re-apply device specific volume if not done by setOutputDevice() |
| 1405 | if (!force) { |
| 1406 | applyStreamVolumes(desc, newDevice2, delayMs); |
| 1407 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1411 | handleNotificationRoutingForStream(stream); |
| 1412 | } |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 1413 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1414 | selectOutputForMusicEffects(); |
| 1415 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1416 | return NO_ERROR; |
| 1417 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1418 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1419 | return INVALID_OPERATION; |
| 1420 | } |
| 1421 | } |
| 1422 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1423 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1424 | audio_stream_type_t stream __unused, |
| 1425 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1426 | { |
| 1427 | ALOGV("releaseOutput() %d", output); |
| 1428 | ssize_t index = mOutputs.indexOfKey(output); |
| 1429 | if (index < 0) { |
| 1430 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | #ifdef AUDIO_POLICY_TEST |
| 1435 | int testIndex = testOutputIndex(output); |
| 1436 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1437 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1438 | if (outputDesc->isActive()) { |
| 1439 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1440 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1441 | mTestOutputs[testIndex] = 0; |
| 1442 | } |
| 1443 | return; |
| 1444 | } |
| 1445 | #endif //AUDIO_POLICY_TEST |
| 1446 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1447 | // Routing |
| 1448 | mOutputRoutes.removeRoute(session); |
| 1449 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1450 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1451 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1452 | if (desc->mDirectOpenCount <= 0) { |
| 1453 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1454 | desc->mDirectOpenCount, output); |
| 1455 | return; |
| 1456 | } |
| 1457 | if (--desc->mDirectOpenCount == 0) { |
| 1458 | closeOutput(output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1459 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1465 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1466 | audio_io_handle_t *input, |
| 1467 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1468 | uid_t uid, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1469 | const audio_config_base_t *config, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1470 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1471 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1472 | input_type_t *inputType, |
| 1473 | audio_port_handle_t *portId) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1474 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1475 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1476 | "session %d, flags %#x", |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1477 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1478 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1479 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1480 | *inputType = API_INPUT_INVALID; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1481 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1482 | audio_devices_t device; |
| 1483 | // handle legacy remote submix case where the address was not always specified |
| 1484 | String8 address = String8(""); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1485 | audio_source_t inputSource = attr->source; |
| 1486 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1487 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1488 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1489 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1490 | inputSource = AUDIO_SOURCE_MIC; |
| 1491 | } |
| 1492 | halInputSource = inputSource; |
| 1493 | |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1494 | // TODO: check for existing client for this port ID |
| 1495 | if (*portId == AUDIO_PORT_HANDLE_NONE) { |
| 1496 | *portId = AudioPort::getNextUniqueId(); |
| 1497 | } |
| 1498 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1499 | // Explicit routing? |
| 1500 | sp<DeviceDescriptor> deviceDesc; |
| 1501 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1502 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1503 | deviceDesc = mAvailableInputDevices[i]; |
| 1504 | break; |
| 1505 | } |
| 1506 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1507 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1508 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1509 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1510 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1511 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1512 | if (ret != NO_ERROR) { |
| 1513 | return ret; |
| 1514 | } |
| 1515 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1516 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1517 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1518 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1519 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1520 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1521 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1522 | return BAD_VALUE; |
| 1523 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1524 | if (policyMix != NULL) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1525 | address = policyMix->mDeviceAddress; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1526 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1527 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1528 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1529 | // know about it and is therefore considered "legacy" |
| 1530 | *inputType = API_INPUT_LEGACY; |
| 1531 | } else { |
| 1532 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1533 | // an external policy |
| 1534 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1535 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1536 | } else if (audio_is_remote_submix_device(device)) { |
| 1537 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1538 | *inputType = API_INPUT_MIX_CAPTURE; |
Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1539 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 1540 | *inputType = API_INPUT_TELEPHONY_RX; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1541 | } else { |
| 1542 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1543 | } |
Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1544 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | *input = getInputForDevice(device, address, session, uid, inputSource, |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1548 | config->sample_rate, config->format, config->channel_mask, flags, |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1549 | policyMix); |
| 1550 | if (*input == AUDIO_IO_HANDLE_NONE) { |
| 1551 | mInputRoutes.removeRoute(session); |
| 1552 | return INVALID_OPERATION; |
| 1553 | } |
Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1554 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1555 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
| 1556 | return NO_ERROR; |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, |
| 1561 | String8 address, |
| 1562 | audio_session_t session, |
| 1563 | uid_t uid, |
| 1564 | audio_source_t inputSource, |
| 1565 | uint32_t samplingRate, |
| 1566 | audio_format_t format, |
| 1567 | audio_channel_mask_t channelMask, |
| 1568 | audio_input_flags_t flags, |
| 1569 | AudioMix *policyMix) |
| 1570 | { |
| 1571 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 1572 | audio_source_t halInputSource = inputSource; |
| 1573 | bool isSoundTrigger = false; |
| 1574 | |
| 1575 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
| 1576 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1577 | if (index >= 0) { |
| 1578 | input = mSoundTriggerSessions.valueFor(session); |
| 1579 | isSoundTrigger = true; |
| 1580 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1581 | ALOGV("SoundTrigger capture on session %d input %d", session, input); |
| 1582 | } else { |
| 1583 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1587 | // find a compatible input profile (not necessarily identical in parameters) |
| 1588 | sp<IOProfile> profile; |
| 1589 | // samplingRate and flags may be updated by getInputProfile |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1590 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1591 | audio_format_t profileFormat = format; |
| 1592 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1593 | audio_input_flags_t profileFlags = flags; |
| 1594 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1595 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1596 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1597 | profileFlags); |
| 1598 | if (profile != 0) { |
| 1599 | break; // success |
Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1600 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { |
| 1601 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1602 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1603 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1604 | } else { // fail |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1605 | ALOGW("getInputForDevice() could not find profile for device 0x%X," |
| 1606 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1607 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1608 | return input; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1609 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1610 | } |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1611 | // Pick input sampling rate if not specified by client |
| 1612 | if (samplingRate == 0) { |
| 1613 | samplingRate = profileSamplingRate; |
| 1614 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1615 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1616 | if (profile->getModuleHandle() == 0) { |
| 1617 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1618 | return input; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1621 | sp<AudioSession> audioSession = new AudioSession(session, |
| 1622 | inputSource, |
| 1623 | format, |
| 1624 | samplingRate, |
| 1625 | channelMask, |
| 1626 | flags, |
| 1627 | uid, |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1628 | isSoundTrigger, |
| 1629 | policyMix, mpClientInterface); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1630 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1631 | // reuse an open input if possible |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1632 | sp<AudioInputDescriptor> reusedInputDesc; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1633 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 1634 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1635 | // reuse input if: |
| 1636 | // - it shares the same profile |
| 1637 | // AND |
| 1638 | // - it is not a reroute submix input |
| 1639 | // AND |
| 1640 | // - it is: not used for sound trigger |
| 1641 | // OR |
| 1642 | // used for sound trigger and all clients use the same session ID |
| 1643 | // |
| 1644 | if ((profile == desc->mProfile) && |
| 1645 | (isSoundTrigger == desc->isSoundTrigger()) && |
| 1646 | !is_virtual_input_device(device)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1647 | |
| 1648 | sp<AudioSession> as = desc->getAudioSession(session); |
| 1649 | if (as != 0) { |
| 1650 | // do not allow unmatching properties on same session |
| 1651 | if (as->matches(audioSession)) { |
| 1652 | as->changeOpenCount(1); |
| 1653 | } else { |
| 1654 | ALOGW("getInputForDevice() record with different attributes" |
| 1655 | " exists for session %d", session); |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1656 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1657 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1658 | } else if (isSoundTrigger) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1659 | continue; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1660 | } |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1661 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1662 | // Reuse the already opened input stream on this profile if: |
| 1663 | // - the new capture source is background OR |
| 1664 | // - the path requested configurations match OR |
| 1665 | // - the new source priority is less than the highest source priority on this input |
| 1666 | // If the input stream cannot be reused, close it before opening a new stream |
| 1667 | // on the same profile for the new client so that the requested path configuration |
| 1668 | // can be selected. |
| 1669 | if (!isConcurrentSource(inputSource) && |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1670 | ((desc->mSamplingRate != samplingRate || |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1671 | desc->mChannelMask != channelMask || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1672 | !audio_formats_match(desc->mFormat, format)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1673 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1674 | source_priority(inputSource)))) { |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1675 | reusedInputDesc = desc; |
| 1676 | continue; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1677 | } else { |
| 1678 | desc->addAudioSession(session, audioSession); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1679 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); |
| 1680 | return mInputs.keyAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1681 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1682 | } |
| 1683 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1684 | |
Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1685 | if (reusedInputDesc != 0) { |
| 1686 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); |
| 1687 | for (size_t j = 0; j < sessions.size(); j++) { |
| 1688 | audio_session_t currentSession = sessions.keyAt(j); |
| 1689 | stopInput(reusedInputDesc->mIoHandle, currentSession); |
| 1690 | releaseInput(reusedInputDesc->mIoHandle, currentSession); |
| 1691 | } |
| 1692 | } |
| 1693 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1694 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1695 | config.sample_rate = profileSamplingRate; |
| 1696 | config.channel_mask = profileChannelMask; |
| 1697 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1698 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1699 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1700 | &input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1701 | &config, |
| 1702 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1703 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1704 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1705 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1706 | |
| 1707 | // only accept input with the exact requested set of parameters |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1708 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1709 | (profileSamplingRate != config.sample_rate) || |
Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1710 | !audio_formats_match(profileFormat, config.format) || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1711 | (profileChannelMask != config.channel_mask)) { |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1712 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" |
| 1713 | ", format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1714 | samplingRate, format, channelMask); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1715 | if (input != AUDIO_IO_HANDLE_NONE) { |
| 1716 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1717 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1718 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1719 | } |
| 1720 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1721 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1722 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1723 | inputDesc->mFormat = profileFormat; |
| 1724 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1725 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1726 | inputDesc->mPolicyMix = policyMix; |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1727 | inputDesc->addAudioSession(session, audioSession); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1728 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1729 | addInput(input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1730 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1731 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1732 | return input; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1735 | //static |
| 1736 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) |
| 1737 | { |
| 1738 | return (source == AUDIO_SOURCE_HOTWORD) || |
| 1739 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || |
| 1740 | (source == AUDIO_SOURCE_FM_TUNER); |
| 1741 | } |
| 1742 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1743 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, |
| 1744 | const sp<AudioSession>& audioSession) |
| 1745 | { |
| 1746 | // Do not allow capture if an active voice call is using a software patch and |
| 1747 | // the call TX source device is on the same HW module. |
| 1748 | // FIXME: would be better to refine to only inputs whose profile connects to the |
| 1749 | // call TX device but this information is not in the audio patch |
| 1750 | if (mCallTxPatch != 0 && |
| 1751 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
| 1755 | // starting concurrent capture is enabled if: |
| 1756 | // 1) capturing for re-routing |
| 1757 | // 2) capturing for HOTWORD source |
| 1758 | // 3) capturing for FM TUNER source |
| 1759 | // 3) All other active captures are either for re-routing or HOTWORD |
| 1760 | |
| 1761 | if (is_virtual_input_device(inputDesc->mDevice) || |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1762 | isConcurrentSource(audioSession->inputSource())) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1763 | return true; |
| 1764 | } |
| 1765 | |
| 1766 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); |
| 1767 | for (size_t i = 0; i < activeInputs.size(); i++) { |
| 1768 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; |
Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1769 | if (!isConcurrentSource(activeInput->inputSource(true)) && |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1770 | !is_virtual_input_device(activeInput->mDevice)) { |
| 1771 | return false; |
| 1772 | } |
| 1773 | } |
| 1774 | |
| 1775 | return true; |
| 1776 | } |
| 1777 | |
| 1778 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1779 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1780 | audio_session_t session, |
| 1781 | concurrency_type__mask_t *concurrency) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1782 | { |
| 1783 | ALOGV("startInput() input %d", input); |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1784 | *concurrency = API_INPUT_CONCURRENCY_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1785 | ssize_t index = mInputs.indexOfKey(input); |
| 1786 | if (index < 0) { |
| 1787 | ALOGW("startInput() unknown input %d", input); |
| 1788 | return BAD_VALUE; |
| 1789 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1790 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1791 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1792 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
| 1793 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1794 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1795 | return BAD_VALUE; |
| 1796 | } |
| 1797 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1798 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { |
| 1799 | ALOGW("startInput(%d) failed: other input already started", input); |
| 1800 | return INVALID_OPERATION; |
| 1801 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1802 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1803 | if (isInCall()) { |
| 1804 | *concurrency |= API_INPUT_CONCURRENCY_CALL; |
| 1805 | } |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1806 | if (mInputs.activeInputsCountOnDevices() != 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1807 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1810 | // increment activity count before calling getNewInputDevice() below as only active sessions |
| 1811 | // are considered for device selection |
| 1812 | audioSession->changeActiveCount(1); |
| 1813 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1814 | // Routing? |
| 1815 | mInputRoutes.incRouteActivity(session); |
| 1816 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1817 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1818 | // indicate active capture to sound trigger service if starting capture from a mic on |
| 1819 | // primary HW module |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1820 | audio_devices_t device = getNewInputDevice(inputDesc); |
Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1821 | setInputDevice(input, device, true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1822 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1823 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { |
| 1824 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1825 | if ((inputDesc->mPolicyMix != NULL) |
| 1826 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1827 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1828 | MIX_STATE_MIXING); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1829 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1830 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1831 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1832 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 1833 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1834 | SoundTrigger::setCaptureState(true); |
| 1835 | } |
| 1836 | |
| 1837 | // automatically enable the remote submix output when input is started if not |
| 1838 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1839 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1840 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1841 | String8 address = String8(""); |
| 1842 | if (inputDesc->mPolicyMix == NULL) { |
| 1843 | address = String8("0"); |
| 1844 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1845 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1846 | } |
| 1847 | if (address != "") { |
| 1848 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1849 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1850 | address, "remote-submix"); |
| 1851 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1852 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1853 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1854 | } |
| 1855 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1856 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1857 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1858 | return NO_ERROR; |
| 1859 | } |
| 1860 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1861 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1862 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1863 | { |
| 1864 | ALOGV("stopInput() input %d", input); |
| 1865 | ssize_t index = mInputs.indexOfKey(input); |
| 1866 | if (index < 0) { |
| 1867 | ALOGW("stopInput() unknown input %d", input); |
| 1868 | return BAD_VALUE; |
| 1869 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1870 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1871 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1872 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1873 | if (index < 0) { |
| 1874 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1875 | return BAD_VALUE; |
| 1876 | } |
| 1877 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1878 | if (audioSession->activeCount() == 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1879 | ALOGW("stopInput() input %d already stopped", input); |
| 1880 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1883 | audioSession->changeActiveCount(-1); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1884 | |
| 1885 | // Routing? |
| 1886 | mInputRoutes.decRouteActivity(session); |
| 1887 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1888 | if (audioSession->activeCount() == 0) { |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1889 | |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1890 | if (inputDesc->isActive()) { |
| 1891 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); |
| 1892 | } else { |
| 1893 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1894 | if ((inputDesc->mPolicyMix != NULL) |
| 1895 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
| 1896 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, |
| 1897 | MIX_STATE_IDLE); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1898 | } |
Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1899 | |
| 1900 | // automatically disable the remote submix output when input is stopped if not |
| 1901 | // used by a policy mix of type MIX_TYPE_RECORDERS |
| 1902 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
| 1903 | String8 address = String8(""); |
| 1904 | if (inputDesc->mPolicyMix == NULL) { |
| 1905 | address = String8("0"); |
| 1906 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1907 | address = inputDesc->mPolicyMix->mDeviceAddress; |
| 1908 | } |
| 1909 | if (address != "") { |
| 1910 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 1911 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1912 | address, "remote-submix"); |
| 1913 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1914 | } |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1915 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1916 | audio_devices_t device = inputDesc->mDevice; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1917 | resetInputDevice(input); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1918 | |
Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1919 | // indicate inactive capture to sound trigger service if stopping capture from a mic on |
| 1920 | // primary HW module |
| 1921 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); |
| 1922 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && |
| 1923 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1924 | SoundTrigger::setCaptureState(false); |
| 1925 | } |
| 1926 | inputDesc->clearPreemptedSessions(); |
Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1927 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1928 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1929 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1930 | } |
| 1931 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1932 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1933 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1934 | { |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1935 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1936 | ALOGV("releaseInput() %d", input); |
| 1937 | ssize_t index = mInputs.indexOfKey(input); |
| 1938 | if (index < 0) { |
| 1939 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1940 | return; |
| 1941 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1942 | |
| 1943 | // Routing |
| 1944 | mInputRoutes.removeRoute(session); |
| 1945 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1946 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1947 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1948 | |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1949 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); |
vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 1950 | if (audioSession == 0) { |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1951 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1952 | return; |
| 1953 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1954 | |
| 1955 | if (audioSession->openCount() == 0) { |
| 1956 | ALOGW("releaseInput() invalid open count %d on session %d", |
| 1957 | audioSession->openCount(), session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1958 | return; |
| 1959 | } |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1960 | |
| 1961 | if (audioSession->changeOpenCount(-1) == 0) { |
| 1962 | inputDesc->removeAudioSession(session); |
| 1963 | } |
| 1964 | |
Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 1965 | if (inputDesc->getOpenRefCount() > 0) { |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1966 | ALOGV("releaseInput() exit > 0"); |
| 1967 | return; |
| 1968 | } |
| 1969 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1970 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1971 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1972 | ALOGV("releaseInput() exit"); |
| 1973 | } |
| 1974 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1975 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1976 | bool patchRemoved = false; |
| 1977 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1978 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1979 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 1980 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1981 | if (patch_index >= 0) { |
| 1982 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 1983 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1984 | mAudioPatches.removeItemsAt(patch_index); |
| 1985 | patchRemoved = true; |
| 1986 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1987 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1988 | } |
| 1989 | mInputs.clear(); |
Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 1990 | SoundTrigger::setCaptureState(false); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1991 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1992 | |
| 1993 | if (patchRemoved) { |
| 1994 | mpClientInterface->onAudioPatchListUpdate(); |
| 1995 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1998 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1999 | int indexMin, |
| 2000 | int indexMax) |
| 2001 | { |
| 2002 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2003 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2004 | |
| 2005 | // initialize other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2006 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2007 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2008 | continue; |
| 2009 | } |
| 2010 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2011 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2014 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2015 | int index, |
| 2016 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2017 | { |
| 2018 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2019 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || |
| 2020 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2021 | return BAD_VALUE; |
| 2022 | } |
| 2023 | if (!audio_is_output_device(device)) { |
| 2024 | return BAD_VALUE; |
| 2025 | } |
| 2026 | |
| 2027 | // Force max volume if stream cannot be muted |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2028 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2029 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2030 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2031 | stream, device, index); |
| 2032 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2033 | // update other private stream volumes which follow this one |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2034 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2035 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2036 | continue; |
| 2037 | } |
| 2038 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); |
| 2039 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2040 | |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2041 | // update volume on all outputs and streams matching the following: |
| 2042 | // - The requested stream (or a stream matching for volume control) is active on the output |
| 2043 | // - The device (or devices) selected by the strategy corresponding to this stream includes |
| 2044 | // the requested device |
| 2045 | // - For non default requested device, currently selected device on the output is either the |
| 2046 | // requested device or one of the devices selected by the strategy |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2047 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if |
| 2048 | // no specific device volume value exists for currently selected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2049 | status_t status = NO_ERROR; |
| 2050 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2051 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 2052 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2053 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 2054 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2055 | continue; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2056 | } |
Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2057 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || |
| 2058 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2059 | continue; |
| 2060 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2061 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2062 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/); |
| 2063 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && |
| 2064 | ((curStreamDevice & device) == 0)) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2065 | continue; |
| 2066 | } |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2067 | bool applyVolume; |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2068 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2069 | curStreamDevice |= device; |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2070 | applyVolume = (curDevice & curStreamDevice) != 0; |
| 2071 | } else { |
| 2072 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( |
| 2073 | stream, Volume::getDeviceForVolume(curStreamDevice)); |
Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2074 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2075 | |
Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2076 | if (applyVolume) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2077 | //FIXME: workaround for truncated touch sounds |
| 2078 | // delayed volume change for system stream to be removed when the problem is |
| 2079 | // handled by system UI |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2080 | status_t volStatus = |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2081 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, |
| 2082 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2083 | if (volStatus != NO_ERROR) { |
| 2084 | status = volStatus; |
| 2085 | } |
| 2086 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2087 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2088 | } |
| 2089 | return status; |
| 2090 | } |
| 2091 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2092 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2093 | int *index, |
| 2094 | audio_devices_t device) |
| 2095 | { |
| 2096 | if (index == NULL) { |
| 2097 | return BAD_VALUE; |
| 2098 | } |
| 2099 | if (!audio_is_output_device(device)) { |
| 2100 | return BAD_VALUE; |
| 2101 | } |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2102 | // 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] | 2103 | // the strategy the stream belongs to. |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2104 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2105 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 2106 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2107 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2108 | |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2109 | *index = mVolumeCurves->getVolumeIndex(stream, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2110 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 2111 | return NO_ERROR; |
| 2112 | } |
| 2113 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 2114 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2115 | { |
| 2116 | // select one output among several suitable for global effects. |
| 2117 | // The priority is as follows: |
| 2118 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 2119 | // AudioFlinger will invalidate the track and the offloaded output |
| 2120 | // will be closed causing the effect to be moved to a PCM output. |
| 2121 | // 2: A deep buffer output |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 2122 | // 3: The primary output |
| 2123 | // 4: the first output in the list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2124 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2125 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2126 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 2127 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2128 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 2129 | if (outputs.size() == 0) { |
| 2130 | return AUDIO_IO_HANDLE_NONE; |
| 2131 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2132 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 2133 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2134 | bool activeOnly = true; |
| 2135 | |
| 2136 | while (output == AUDIO_IO_HANDLE_NONE) { |
| 2137 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; |
| 2138 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; |
| 2139 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; |
| 2140 | |
| 2141 | for (size_t i = 0; i < outputs.size(); i++) { |
| 2142 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
| 2143 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { |
| 2144 | continue; |
| 2145 | } |
| 2146 | ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x", |
| 2147 | activeOnly, i, desc->mFlags); |
| 2148 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 2149 | outputOffloaded = outputs[i]; |
| 2150 | } |
| 2151 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 2152 | outputDeepBuffer = outputs[i]; |
| 2153 | } |
| 2154 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { |
| 2155 | outputPrimary = outputs[i]; |
| 2156 | } |
| 2157 | } |
| 2158 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { |
| 2159 | output = outputOffloaded; |
| 2160 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { |
| 2161 | output = outputDeepBuffer; |
| 2162 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { |
| 2163 | output = outputPrimary; |
| 2164 | } else { |
| 2165 | output = outputs[0]; |
| 2166 | } |
| 2167 | activeOnly = false; |
| 2168 | } |
| 2169 | |
| 2170 | if (output != mMusicEffectOutput) { |
| 2171 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); |
| 2172 | mMusicEffectOutput = output; |
| 2173 | } |
| 2174 | |
| 2175 | ALOGV("selectOutputForMusicEffects selected output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2176 | return output; |
| 2177 | } |
| 2178 | |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 2179 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) |
| 2180 | { |
| 2181 | return selectOutputForMusicEffects(); |
| 2182 | } |
| 2183 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2184 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2185 | audio_io_handle_t io, |
| 2186 | uint32_t strategy, |
| 2187 | int session, |
| 2188 | int id) |
| 2189 | { |
| 2190 | ssize_t index = mOutputs.indexOfKey(io); |
| 2191 | if (index < 0) { |
| 2192 | index = mInputs.indexOfKey(io); |
| 2193 | if (index < 0) { |
| 2194 | ALOGW("registerEffect() unknown io %d", io); |
| 2195 | return INVALID_OPERATION; |
| 2196 | } |
| 2197 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2198 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2201 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2202 | { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2203 | bool active = false; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2204 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { |
| 2205 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2206 | continue; |
| 2207 | } |
| 2208 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); |
| 2209 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2210 | return active; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 2214 | { |
| 2215 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 2216 | } |
| 2217 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2218 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2219 | { |
| 2220 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2221 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2222 | if (inputDescriptor->isSourceActive(source)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2223 | return true; |
| 2224 | } |
| 2225 | } |
| 2226 | return false; |
| 2227 | } |
| 2228 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2229 | // Register a list of custom mixes with their attributes and format. |
| 2230 | // When a mix is registered, corresponding input and output profiles are |
| 2231 | // added to the remote submix hw module. The profile contains only the |
| 2232 | // parameters (sampling rate, format...) specified by the mix. |
| 2233 | // The corresponding input remote submix device is also connected. |
| 2234 | // |
| 2235 | // When a remote submix device is connected, the address is checked to select the |
| 2236 | // appropriate profile and the corresponding input or output stream is opened. |
| 2237 | // |
| 2238 | // When capture starts, getInputForAttr() will: |
| 2239 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2240 | // - 2 if none found, getDeviceForInputSource() will: |
| 2241 | // - 2.1 look for a mix matching the attributes source |
| 2242 | // - 2.2 if none found, default to device selection by policy rules |
| 2243 | // At this time, the corresponding output remote submix device is also connected |
| 2244 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 2245 | // after AudioTracks are invalidated |
| 2246 | // |
| 2247 | // When playback starts, getOutputForAttr() will: |
| 2248 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 2249 | // - 2 if none found, look for a mix matching the attributes usage |
| 2250 | // - 3 if none found, default to device and output selection by policy rules. |
| 2251 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2252 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2253 | { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2254 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); |
| 2255 | status_t res = NO_ERROR; |
| 2256 | |
| 2257 | sp<HwModule> rSubmixModule; |
| 2258 | // examine each mix's route type |
| 2259 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2260 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination |
| 2261 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { |
| 2262 | res = INVALID_OPERATION; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2263 | break; |
| 2264 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2265 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { |
| 2266 | // Loop back through "remote submix" |
| 2267 | if (rSubmixModule == 0) { |
| 2268 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2269 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2270 | && mHwModules[j]->mHandle != 0) { |
| 2271 | rSubmixModule = mHwModules[j]; |
| 2272 | break; |
| 2273 | } |
| 2274 | } |
| 2275 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2276 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2277 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2278 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2279 | if (rSubmixModule == 0) { |
| 2280 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); |
| 2281 | res = INVALID_OPERATION; |
| 2282 | break; |
| 2283 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2284 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2285 | String8 address = mixes[i].mDeviceAddress; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2286 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2287 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2288 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2289 | res = INVALID_OPERATION; |
| 2290 | break; |
| 2291 | } |
| 2292 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2293 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2294 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2295 | // stereo and let audio flinger do the channel conversion if needed. |
| 2296 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2297 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2298 | rSubmixModule->addOutputProfile(address, &outputConfig, |
| 2299 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2300 | rSubmixModule->addInputProfile(address, &inputConfig, |
| 2301 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2302 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2303 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
| 2304 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2305 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2306 | address.string(), "remote-submix"); |
| 2307 | } else { |
| 2308 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2309 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 2310 | address.string(), "remote-submix"); |
| 2311 | } |
| 2312 | } 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] | 2313 | String8 address = mixes[i].mDeviceAddress; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2314 | audio_devices_t device = mixes[i].mDeviceType; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2315 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", |
| 2316 | i, mixes.size(), device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2317 | |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2318 | bool foundOutput = false; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2319 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { |
| 2320 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); |
| 2321 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); |
| 2322 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) |
| 2323 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) |
| 2324 | && (patch->mPatch.sinks[0].ext.device.type == device) |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2325 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), |
| 2326 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2327 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { |
| 2328 | res = INVALID_OPERATION; |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2329 | } else { |
| 2330 | foundOutput = true; |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2331 | } |
| 2332 | break; |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | if (res != NO_ERROR) { |
| 2337 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", |
Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2338 | i, device, address.string()); |
| 2339 | res = INVALID_OPERATION; |
| 2340 | break; |
| 2341 | } else if (!foundOutput) { |
| 2342 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", |
| 2343 | i, device, address.string()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2344 | res = INVALID_OPERATION; |
| 2345 | break; |
| 2346 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2347 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2348 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2349 | if (res != NO_ERROR) { |
| 2350 | unregisterPolicyMixes(mixes); |
| 2351 | } |
| 2352 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2356 | { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2357 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2358 | status_t res = NO_ERROR; |
| 2359 | sp<HwModule> rSubmixModule; |
| 2360 | // examine each mix's route type |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2361 | for (size_t i = 0; i < mixes.size(); i++) { |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2362 | 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] | 2363 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2364 | if (rSubmixModule == 0) { |
| 2365 | for (size_t j = 0; i < mHwModules.size(); j++) { |
| 2366 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 |
| 2367 | && mHwModules[j]->mHandle != 0) { |
| 2368 | rSubmixModule = mHwModules[j]; |
| 2369 | break; |
| 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | if (rSubmixModule == 0) { |
| 2374 | res = INVALID_OPERATION; |
| 2375 | continue; |
| 2376 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2377 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2378 | String8 address = mixes[i].mDeviceAddress; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2379 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2380 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
| 2381 | res = INVALID_OPERATION; |
| 2382 | continue; |
| 2383 | } |
| 2384 | |
| 2385 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2386 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2387 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 2388 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2389 | address.string(), "remote-submix"); |
| 2390 | } |
| 2391 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2392 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 2393 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
| 2394 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 2395 | address.string(), "remote-submix"); |
| 2396 | } |
| 2397 | rSubmixModule->removeOutputProfile(address); |
| 2398 | rSubmixModule->removeInputProfile(address); |
| 2399 | |
| 2400 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { |
| 2401 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { |
| 2402 | res = INVALID_OPERATION; |
| 2403 | continue; |
| 2404 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2405 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2406 | } |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2407 | return res; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2408 | } |
| 2409 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2410 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2411 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2412 | { |
| 2413 | const size_t SIZE = 256; |
| 2414 | char buffer[SIZE]; |
| 2415 | String8 result; |
| 2416 | |
| 2417 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2418 | result.append(buffer); |
| 2419 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2420 | snprintf(buffer, SIZE, " Primary Output: %d\n", |
| 2421 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2422 | result.append(buffer); |
Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2423 | std::string stateLiteral; |
| 2424 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); |
| 2425 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2426 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2427 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2428 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2429 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2430 | 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] | 2431 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2432 | 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] | 2433 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2434 | 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] | 2435 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2436 | 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] | 2437 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2438 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2439 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2440 | result.append(buffer); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2441 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", |
| 2442 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); |
| 2443 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2444 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); |
| 2445 | result.append(buffer); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2446 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); |
| 2447 | result.append(buffer); |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2448 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2449 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2450 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2451 | mAvailableOutputDevices.dump(fd, String8("Available output")); |
| 2452 | mAvailableInputDevices.dump(fd, String8("Available input")); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2453 | mHwModules.dump(fd); |
| 2454 | mOutputs.dump(fd); |
| 2455 | mInputs.dump(fd); |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2456 | mVolumeCurves->dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2457 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2458 | mAudioPatches.dump(fd); |
Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2459 | mPolicyMixes.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2460 | |
| 2461 | return NO_ERROR; |
| 2462 | } |
| 2463 | |
| 2464 | // This function checks for the parameters which can be offloaded. |
| 2465 | // This can be enhanced depending on the capability of the DSP and policy |
| 2466 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2467 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2468 | { |
| 2469 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2470 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2471 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2472 | offloadInfo.format, |
| 2473 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2474 | offloadInfo.has_video); |
| 2475 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2476 | if (mMasterMono) { |
| 2477 | return false; // no offloading if mono is set. |
| 2478 | } |
| 2479 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2480 | // Check if offload has been disabled |
| 2481 | char propValue[PROPERTY_VALUE_MAX]; |
| 2482 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2483 | if (atoi(propValue) != 0) { |
| 2484 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2485 | return false; |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | // Check if stream type is music, then only allow offload as of now. |
| 2490 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2491 | { |
| 2492 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2493 | return false; |
| 2494 | } |
| 2495 | |
| 2496 | //TODO: enable audio offloading with video when ready |
Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2497 | const bool allowOffloadWithVideo = |
| 2498 | property_get_bool("audio.offload.video", false /* default_value */); |
| 2499 | if (offloadInfo.has_video && !allowOffloadWithVideo) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2500 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2501 | return false; |
| 2502 | } |
| 2503 | |
| 2504 | //If duration is less than minimum value defined in property, return false |
| 2505 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2506 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2507 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2508 | return false; |
| 2509 | } |
| 2510 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2511 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2512 | return false; |
| 2513 | } |
| 2514 | |
| 2515 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2516 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2517 | // detects there is an active non offloadable effect. |
| 2518 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2519 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2520 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2521 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2522 | return false; |
| 2523 | } |
| 2524 | |
| 2525 | // See if there is a profile to support this. |
| 2526 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2527 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2528 | offloadInfo.sample_rate, |
| 2529 | offloadInfo.format, |
| 2530 | offloadInfo.channel_mask, |
| 2531 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2532 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2533 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2534 | } |
| 2535 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2536 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2537 | audio_port_type_t type, |
| 2538 | unsigned int *num_ports, |
| 2539 | struct audio_port *ports, |
| 2540 | unsigned int *generation) |
| 2541 | { |
| 2542 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2543 | generation == NULL) { |
| 2544 | return BAD_VALUE; |
| 2545 | } |
| 2546 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2547 | if (ports == NULL) { |
| 2548 | *num_ports = 0; |
| 2549 | } |
| 2550 | |
| 2551 | size_t portsWritten = 0; |
| 2552 | size_t portsMax = *num_ports; |
| 2553 | *num_ports = 0; |
| 2554 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2555 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB |
| 2556 | // as they are used by stub HALs by convention |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2557 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2558 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 2559 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { |
| 2560 | continue; |
| 2561 | } |
| 2562 | if (portsWritten < portsMax) { |
| 2563 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2564 | } |
| 2565 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2566 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2567 | } |
| 2568 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2569 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2570 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { |
| 2571 | continue; |
| 2572 | } |
| 2573 | if (portsWritten < portsMax) { |
| 2574 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2575 | } |
| 2576 | (*num_ports)++; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2577 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2581 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2582 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2583 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2584 | } |
| 2585 | *num_ports += mInputs.size(); |
| 2586 | } |
| 2587 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2588 | size_t numOutputs = 0; |
| 2589 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2590 | if (!mOutputs[i]->isDuplicated()) { |
| 2591 | numOutputs++; |
| 2592 | if (portsWritten < portsMax) { |
| 2593 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2594 | } |
| 2595 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2596 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2597 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2598 | } |
| 2599 | } |
| 2600 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2601 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2602 | return NO_ERROR; |
| 2603 | } |
| 2604 | |
| 2605 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2606 | { |
| 2607 | return NO_ERROR; |
| 2608 | } |
| 2609 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2610 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2611 | audio_patch_handle_t *handle, |
| 2612 | uid_t uid) |
| 2613 | { |
| 2614 | ALOGV("createAudioPatch()"); |
| 2615 | |
| 2616 | if (handle == NULL || patch == NULL) { |
| 2617 | return BAD_VALUE; |
| 2618 | } |
| 2619 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2620 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2621 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2622 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2623 | return BAD_VALUE; |
| 2624 | } |
| 2625 | // only one source per audio patch supported for now |
| 2626 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2627 | return INVALID_OPERATION; |
| 2628 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2629 | |
| 2630 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2631 | return INVALID_OPERATION; |
| 2632 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2633 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2634 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2635 | return INVALID_OPERATION; |
| 2636 | } |
| 2637 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2638 | |
| 2639 | sp<AudioPatch> patchDesc; |
| 2640 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2641 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2642 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2643 | patch->sources[0].role, |
| 2644 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2645 | #if LOG_NDEBUG == 0 |
| 2646 | for (size_t i = 0; i < patch->num_sinks; i++) { |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2647 | 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] | 2648 | patch->sinks[i].role, |
| 2649 | patch->sinks[i].type); |
| 2650 | } |
| 2651 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2652 | |
| 2653 | if (index >= 0) { |
| 2654 | patchDesc = mAudioPatches.valueAt(index); |
| 2655 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2656 | mUidCached, patchDesc->mUid, uid); |
| 2657 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2658 | return INVALID_OPERATION; |
| 2659 | } |
| 2660 | } else { |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2661 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2665 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2666 | if (outputDesc == NULL) { |
| 2667 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2668 | return BAD_VALUE; |
| 2669 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2670 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2671 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2672 | if (patchDesc != 0) { |
| 2673 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2674 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2675 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2676 | return BAD_VALUE; |
| 2677 | } |
| 2678 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2679 | DeviceVector devices; |
| 2680 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2681 | // Only support mix to devices connection |
| 2682 | // TODO add support for mix to mix connection |
| 2683 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2684 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2685 | return INVALID_OPERATION; |
| 2686 | } |
| 2687 | sp<DeviceDescriptor> devDesc = |
| 2688 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2689 | if (devDesc == 0) { |
| 2690 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2691 | return BAD_VALUE; |
| 2692 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2693 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2694 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2695 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2696 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2697 | NULL, // updatedSamplingRate |
| 2698 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2699 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2700 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2701 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2702 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2703 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2704 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2705 | return INVALID_OPERATION; |
| 2706 | } |
| 2707 | devices.add(devDesc); |
| 2708 | } |
| 2709 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2710 | return INVALID_OPERATION; |
| 2711 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2712 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2713 | // TODO: reconfigure output format and channels here |
| 2714 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2715 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2716 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2717 | index = mAudioPatches.indexOfKey(*handle); |
| 2718 | if (index >= 0) { |
| 2719 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2720 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2721 | } |
| 2722 | patchDesc = mAudioPatches.valueAt(index); |
| 2723 | patchDesc->mUid = uid; |
| 2724 | ALOGV("createAudioPatch() success"); |
| 2725 | } else { |
| 2726 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2727 | return INVALID_OPERATION; |
| 2728 | } |
| 2729 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2730 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2731 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2732 | // only one sink supported when connecting an input device to a mix |
| 2733 | if (patch->num_sinks > 1) { |
| 2734 | return INVALID_OPERATION; |
| 2735 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2736 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2737 | if (inputDesc == NULL) { |
| 2738 | return BAD_VALUE; |
| 2739 | } |
| 2740 | if (patchDesc != 0) { |
| 2741 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2742 | return BAD_VALUE; |
| 2743 | } |
| 2744 | } |
| 2745 | sp<DeviceDescriptor> devDesc = |
| 2746 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2747 | if (devDesc == 0) { |
| 2748 | return BAD_VALUE; |
| 2749 | } |
| 2750 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2751 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2752 | devDesc->mAddress, |
| 2753 | patch->sinks[0].sample_rate, |
| 2754 | NULL, /*updatedSampleRate*/ |
| 2755 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2756 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2757 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2758 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2759 | // FIXME for the parameter type, |
| 2760 | // and the NONE |
| 2761 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2762 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2763 | return INVALID_OPERATION; |
| 2764 | } |
| 2765 | // TODO: reconfigure output format and channels here |
| 2766 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2767 | devDesc->type(), inputDesc->mIoHandle); |
| 2768 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2769 | index = mAudioPatches.indexOfKey(*handle); |
| 2770 | if (index >= 0) { |
| 2771 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2772 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2773 | } |
| 2774 | patchDesc = mAudioPatches.valueAt(index); |
| 2775 | patchDesc->mUid = uid; |
| 2776 | ALOGV("createAudioPatch() success"); |
| 2777 | } else { |
| 2778 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2779 | return INVALID_OPERATION; |
| 2780 | } |
| 2781 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2782 | // device to device connection |
| 2783 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2784 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2785 | return BAD_VALUE; |
| 2786 | } |
| 2787 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2788 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2789 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2790 | if (srcDeviceDesc == 0) { |
| 2791 | return BAD_VALUE; |
| 2792 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2793 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2794 | //update source and sink with our own data as the data passed in the patch may |
| 2795 | // be incomplete. |
| 2796 | struct audio_patch newPatch = *patch; |
| 2797 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2798 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2799 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2800 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2801 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2802 | return INVALID_OPERATION; |
| 2803 | } |
| 2804 | |
| 2805 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2806 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2807 | if (sinkDeviceDesc == 0) { |
| 2808 | return BAD_VALUE; |
| 2809 | } |
| 2810 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2811 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2812 | // create a software bridge in PatchPanel if: |
| 2813 | // - source and sink devices are on differnt HW modules OR |
| 2814 | // - audio HAL version is < 3.0 |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2815 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2816 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2817 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2818 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2819 | return INVALID_OPERATION; |
| 2820 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2821 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2822 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2823 | // if the sink device is reachable via an opened output stream, request to go via |
| 2824 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2825 | audio_io_handle_t output = selectOutput(outputs, |
| 2826 | AUDIO_OUTPUT_FLAG_NONE, |
| 2827 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2828 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2829 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2830 | if (outputDesc->isDuplicated()) { |
| 2831 | return INVALID_OPERATION; |
| 2832 | } |
| 2833 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2834 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2835 | newPatch.num_sources = 2; |
| 2836 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2837 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2838 | } |
| 2839 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2840 | |
| 2841 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2842 | if (index >= 0) { |
| 2843 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2844 | } |
| 2845 | |
| 2846 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2847 | &afPatchHandle, |
| 2848 | 0); |
| 2849 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2850 | status, afPatchHandle); |
| 2851 | if (status == NO_ERROR) { |
| 2852 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2853 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2854 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2855 | } else { |
| 2856 | patchDesc->mPatch = newPatch; |
| 2857 | } |
| 2858 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2859 | *handle = patchDesc->mHandle; |
| 2860 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2861 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2862 | } else { |
| 2863 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2864 | status); |
| 2865 | return INVALID_OPERATION; |
| 2866 | } |
| 2867 | } else { |
| 2868 | return BAD_VALUE; |
| 2869 | } |
| 2870 | } else { |
| 2871 | return BAD_VALUE; |
| 2872 | } |
| 2873 | return NO_ERROR; |
| 2874 | } |
| 2875 | |
| 2876 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2877 | uid_t uid) |
| 2878 | { |
| 2879 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2880 | |
| 2881 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2882 | |
| 2883 | if (index < 0) { |
| 2884 | return BAD_VALUE; |
| 2885 | } |
| 2886 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2887 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2888 | mUidCached, patchDesc->mUid, uid); |
| 2889 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2890 | return INVALID_OPERATION; |
| 2891 | } |
| 2892 | |
| 2893 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2894 | patchDesc->mUid = mUidCached; |
| 2895 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2896 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2897 | if (outputDesc == NULL) { |
| 2898 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2899 | return BAD_VALUE; |
| 2900 | } |
| 2901 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2902 | setOutputDevice(outputDesc, |
| 2903 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2904 | true, |
| 2905 | 0, |
| 2906 | NULL); |
| 2907 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2908 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2909 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2910 | if (inputDesc == NULL) { |
| 2911 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2912 | return BAD_VALUE; |
| 2913 | } |
| 2914 | setInputDevice(inputDesc->mIoHandle, |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2915 | getNewInputDevice(inputDesc), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2916 | true, |
| 2917 | NULL); |
| 2918 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2919 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2920 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2921 | status, patchDesc->mAfPatchHandle); |
| 2922 | removeAudioPatch(patchDesc->mHandle); |
| 2923 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2924 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2925 | } else { |
| 2926 | return BAD_VALUE; |
| 2927 | } |
| 2928 | } else { |
| 2929 | return BAD_VALUE; |
| 2930 | } |
| 2931 | return NO_ERROR; |
| 2932 | } |
| 2933 | |
| 2934 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2935 | struct audio_patch *patches, |
| 2936 | unsigned int *generation) |
| 2937 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2938 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2939 | return BAD_VALUE; |
| 2940 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2941 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2942 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2943 | } |
| 2944 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2945 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2946 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2947 | ALOGV("setAudioPortConfig()"); |
| 2948 | |
| 2949 | if (config == NULL) { |
| 2950 | return BAD_VALUE; |
| 2951 | } |
| 2952 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2953 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2954 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2955 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2958 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2959 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2960 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2961 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2962 | if (outputDesc == NULL) { |
| 2963 | return BAD_VALUE; |
| 2964 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2965 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2966 | "setAudioPortConfig() called on duplicated output %d", |
| 2967 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2968 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2969 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2970 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2971 | if (inputDesc == NULL) { |
| 2972 | return BAD_VALUE; |
| 2973 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2974 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2975 | } else { |
| 2976 | return BAD_VALUE; |
| 2977 | } |
| 2978 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2979 | sp<DeviceDescriptor> deviceDesc; |
| 2980 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2981 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2982 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2983 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2984 | } else { |
| 2985 | return BAD_VALUE; |
| 2986 | } |
| 2987 | if (deviceDesc == NULL) { |
| 2988 | return BAD_VALUE; |
| 2989 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2990 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2991 | } else { |
| 2992 | return BAD_VALUE; |
| 2993 | } |
| 2994 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2995 | struct audio_port_config backupConfig; |
| 2996 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2997 | if (status == NO_ERROR) { |
| 2998 | struct audio_port_config newConfig; |
| 2999 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 3000 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3001 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3002 | if (status != NO_ERROR) { |
| 3003 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3004 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3005 | |
| 3006 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3007 | } |
| 3008 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3009 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 3010 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3011 | clearAudioSources(uid); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3012 | clearAudioPatches(uid); |
| 3013 | clearSessionRoutes(uid); |
| 3014 | } |
| 3015 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3016 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 3017 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3018 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3019 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 3020 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3021 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | } |
| 3025 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3026 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 3027 | audio_io_handle_t ouptutToSkip) |
| 3028 | { |
| 3029 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3030 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 3031 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 3032 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 3033 | continue; |
| 3034 | } |
| 3035 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 3036 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 3037 | continue; |
| 3038 | } |
| 3039 | // If the default device for this strategy is on another output mix, |
| 3040 | // invalidate all tracks in this strategy to force re connection. |
| 3041 | // Otherwise select new device on the output mix. |
| 3042 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3043 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3044 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 3045 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 3046 | } |
| 3047 | } |
| 3048 | } else { |
| 3049 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 3050 | setOutputDevice(outputDesc, newDevice, false); |
| 3051 | } |
| 3052 | } |
| 3053 | } |
| 3054 | |
| 3055 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 3056 | { |
| 3057 | // remove output routes associated with this uid |
| 3058 | SortedVector<routing_strategy> affectedStrategies; |
| 3059 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 3060 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 3061 | if (route->mUid == uid) { |
| 3062 | mOutputRoutes.removeItemsAt(i); |
| 3063 | if (route->mDeviceDescriptor != 0) { |
| 3064 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 3065 | } |
| 3066 | } |
| 3067 | } |
| 3068 | // reroute outputs if necessary |
| 3069 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 3070 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 3071 | } |
| 3072 | |
| 3073 | // remove input routes associated with this uid |
| 3074 | SortedVector<audio_source_t> affectedSources; |
| 3075 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 3076 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 3077 | if (route->mUid == uid) { |
| 3078 | mInputRoutes.removeItemsAt(i); |
| 3079 | if (route->mDeviceDescriptor != 0) { |
| 3080 | affectedSources.add(route->mSource); |
| 3081 | } |
| 3082 | } |
| 3083 | } |
| 3084 | // reroute inputs if necessary |
| 3085 | SortedVector<audio_io_handle_t> inputsToClose; |
| 3086 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3087 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3088 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3089 | inputsToClose.add(inputDesc->mIoHandle); |
| 3090 | } |
| 3091 | } |
| 3092 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 3093 | closeInput(inputsToClose[i]); |
| 3094 | } |
| 3095 | } |
| 3096 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3097 | void AudioPolicyManager::clearAudioSources(uid_t uid) |
| 3098 | { |
| 3099 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 3100 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3101 | if (sourceDesc->mUid == uid) { |
| 3102 | stopAudioSource(mAudioSources.keyAt(i)); |
| 3103 | } |
| 3104 | } |
| 3105 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3106 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3107 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 3108 | audio_io_handle_t *ioHandle, |
| 3109 | audio_devices_t *device) |
| 3110 | { |
Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3111 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); |
| 3112 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3113 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3114 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3115 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3116 | } |
| 3117 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3118 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, |
| 3119 | const audio_attributes_t *attributes, |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3120 | audio_patch_handle_t *handle, |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3121 | uid_t uid) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3122 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3123 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); |
| 3124 | if (source == NULL || attributes == NULL || handle == NULL) { |
| 3125 | return BAD_VALUE; |
| 3126 | } |
| 3127 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3128 | *handle = AUDIO_PATCH_HANDLE_NONE; |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3129 | |
| 3130 | if (source->role != AUDIO_PORT_ROLE_SOURCE || |
| 3131 | source->type != AUDIO_PORT_TYPE_DEVICE) { |
| 3132 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); |
| 3133 | return INVALID_OPERATION; |
| 3134 | } |
| 3135 | |
| 3136 | sp<DeviceDescriptor> srcDeviceDesc = |
| 3137 | mAvailableInputDevices.getDevice(source->ext.device.type, |
| 3138 | String8(source->ext.device.address)); |
| 3139 | if (srcDeviceDesc == 0) { |
| 3140 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); |
| 3141 | return BAD_VALUE; |
| 3142 | } |
| 3143 | sp<AudioSourceDescriptor> sourceDesc = |
| 3144 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); |
| 3145 | |
| 3146 | struct audio_patch dummyPatch; |
| 3147 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); |
| 3148 | sourceDesc->mPatchDesc = patchDesc; |
| 3149 | |
| 3150 | status_t status = connectAudioSource(sourceDesc); |
| 3151 | if (status == NO_ERROR) { |
| 3152 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); |
| 3153 | *handle = sourceDesc->getHandle(); |
| 3154 | } |
| 3155 | return status; |
| 3156 | } |
| 3157 | |
| 3158 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3159 | { |
| 3160 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3161 | |
| 3162 | // make sure we only have one patch per source. |
| 3163 | disconnectAudioSource(sourceDesc); |
| 3164 | |
| 3165 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3166 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3167 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; |
| 3168 | |
| 3169 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); |
| 3170 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 3171 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); |
| 3172 | |
| 3173 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 3174 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; |
| 3175 | |
| 3176 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == |
| 3177 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3178 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3179 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { |
| 3180 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); |
| 3181 | // create patch between src device and output device |
| 3182 | // create Hwoutput and add to mHwOutputs |
| 3183 | } else { |
| 3184 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); |
| 3185 | audio_io_handle_t output = |
| 3186 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); |
| 3187 | if (output == AUDIO_IO_HANDLE_NONE) { |
| 3188 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); |
| 3189 | return INVALID_OPERATION; |
| 3190 | } |
| 3191 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 3192 | if (outputDesc->isDuplicated()) { |
| 3193 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); |
| 3194 | return INVALID_OPERATION; |
| 3195 | } |
| 3196 | // create a special patch with no sink and two sources: |
| 3197 | // - the second source indicates to PatchPanel through which output mix this patch should |
| 3198 | // be connected as well as the stream type for volume control |
| 3199 | // - the sink is defined by whatever output device is currently selected for the output |
| 3200 | // though which this patch is routed. |
| 3201 | patch->num_sinks = 0; |
| 3202 | patch->num_sources = 2; |
| 3203 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); |
| 3204 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); |
| 3205 | patch->sources[1].ext.mix.usecase.stream = stream; |
| 3206 | status_t status = mpClientInterface->createAudioPatch(patch, |
| 3207 | &afPatchHandle, |
| 3208 | 0); |
| 3209 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, |
| 3210 | status, afPatchHandle); |
| 3211 | if (status != NO_ERROR) { |
| 3212 | ALOGW("%s patch panel could not connect device patch, error %d", |
| 3213 | __FUNCTION__, status); |
| 3214 | return INVALID_OPERATION; |
| 3215 | } |
| 3216 | uint32_t delayMs = 0; |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3217 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3218 | |
| 3219 | if (status != NO_ERROR) { |
| 3220 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); |
| 3221 | return status; |
| 3222 | } |
| 3223 | sourceDesc->mSwOutput = outputDesc; |
| 3224 | if (delayMs != 0) { |
| 3225 | usleep(delayMs * 1000); |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; |
| 3230 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); |
| 3231 | |
| 3232 | return NO_ERROR; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3233 | } |
| 3234 | |
Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3235 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3236 | { |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3237 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); |
| 3238 | ALOGV("%s handle %d", __FUNCTION__, handle); |
| 3239 | if (sourceDesc == 0) { |
| 3240 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); |
| 3241 | return BAD_VALUE; |
| 3242 | } |
| 3243 | status_t status = disconnectAudioSource(sourceDesc); |
| 3244 | |
| 3245 | mAudioSources.removeItem(handle); |
| 3246 | return status; |
| 3247 | } |
| 3248 | |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3249 | status_t AudioPolicyManager::setMasterMono(bool mono) |
| 3250 | { |
| 3251 | if (mMasterMono == mono) { |
| 3252 | return NO_ERROR; |
| 3253 | } |
| 3254 | mMasterMono = mono; |
| 3255 | // if enabling mono we close all offloaded devices, which will invalidate the |
| 3256 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible |
| 3257 | // for recreating the new AudioTrack as non-offloaded PCM. |
| 3258 | // |
| 3259 | // If disabling mono, we leave all tracks as is: we don't know which clients |
| 3260 | // and tracks are able to be recreated as offloaded. The next "song" should |
| 3261 | // play back offloaded. |
| 3262 | if (mMasterMono) { |
| 3263 | Vector<audio_io_handle_t> offloaded; |
| 3264 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3265 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3266 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3267 | offloaded.push(desc->mIoHandle); |
| 3268 | } |
| 3269 | } |
| 3270 | for (size_t i = 0; i < offloaded.size(); ++i) { |
| 3271 | closeOutput(offloaded[i]); |
| 3272 | } |
| 3273 | } |
| 3274 | // update master mono for all remaining outputs |
| 3275 | for (size_t i = 0; i < mOutputs.size(); ++i) { |
| 3276 | updateMono(mOutputs.keyAt(i)); |
| 3277 | } |
| 3278 | return NO_ERROR; |
| 3279 | } |
| 3280 | |
| 3281 | status_t AudioPolicyManager::getMasterMono(bool *mono) |
| 3282 | { |
| 3283 | *mono = mMasterMono; |
| 3284 | return NO_ERROR; |
| 3285 | } |
| 3286 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3287 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) |
| 3288 | { |
| 3289 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); |
| 3290 | |
| 3291 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); |
| 3292 | if (patchDesc == 0) { |
| 3293 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, |
| 3294 | sourceDesc->mPatchDesc->mHandle); |
| 3295 | return BAD_VALUE; |
| 3296 | } |
| 3297 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); |
| 3298 | |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3299 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3300 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); |
| 3301 | if (swOutputDesc != 0) { |
| 3302 | stopSource(swOutputDesc, stream, false); |
| 3303 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3304 | } else { |
| 3305 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); |
| 3306 | if (hwOutputDesc != 0) { |
| 3307 | // release patch between src device and output device |
| 3308 | // close Hwoutput and remove from mHwOutputs |
| 3309 | } else { |
| 3310 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); |
| 3311 | } |
| 3312 | } |
| 3313 | return NO_ERROR; |
| 3314 | } |
| 3315 | |
| 3316 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( |
| 3317 | audio_io_handle_t output, routing_strategy strategy) |
| 3318 | { |
| 3319 | sp<AudioSourceDescriptor> source; |
| 3320 | for (size_t i = 0; i < mAudioSources.size(); i++) { |
| 3321 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 3322 | routing_strategy sourceStrategy = |
| 3323 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); |
| 3324 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); |
| 3325 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { |
| 3326 | source = sourceDesc; |
| 3327 | break; |
| 3328 | } |
| 3329 | } |
| 3330 | return source; |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3331 | } |
| 3332 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3333 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3334 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3335 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3336 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 3337 | { |
| 3338 | return android_atomic_inc(&mAudioPortGeneration); |
| 3339 | } |
| 3340 | |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3341 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3342 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. |
| 3343 | static const char *kConfigLocationList[] = |
| 3344 | {"/odm/etc", "/vendor/etc", "/system/etc"}; |
| 3345 | static const int kConfigLocationListSize = |
| 3346 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); |
| 3347 | |
| 3348 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { |
| 3349 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; |
| 3350 | status_t ret; |
| 3351 | |
| 3352 | for (int i = 0; i < kConfigLocationListSize; i++) { |
| 3353 | PolicySerializer serializer; |
| 3354 | snprintf(audioPolicyXmlConfigFile, |
| 3355 | sizeof(audioPolicyXmlConfigFile), |
| 3356 | "%s/%s", |
| 3357 | kConfigLocationList[i], |
| 3358 | AUDIO_POLICY_XML_CONFIG_FILE_NAME); |
| 3359 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); |
| 3360 | if (ret == NO_ERROR) { |
| 3361 | break; |
| 3362 | } |
| 3363 | } |
| 3364 | return ret; |
| 3365 | } |
| 3366 | #endif |
| 3367 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3368 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3369 | : |
| 3370 | #ifdef AUDIO_POLICY_TEST |
| 3371 | Thread(false), |
| 3372 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3373 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3374 | mA2dpSuspended(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3375 | mAudioPortGeneration(1), |
| 3376 | mBeaconMuteRefCount(0), |
| 3377 | mBeaconPlayingRefCount(0), |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3378 | mBeaconMuted(false), |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3379 | mTtsOutputAvailable(false), |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 3380 | mMasterMono(false), |
| 3381 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3382 | { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3383 | mUidCached = getuid(); |
| 3384 | mpClientInterface = clientInterface; |
| 3385 | |
| 3386 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the |
| 3387 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. |
| 3388 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. |
| 3389 | bool speakerDrcEnabled = false; |
| 3390 | |
| 3391 | #ifdef USE_XML_AUDIO_POLICY_CONF |
| 3392 | mVolumeCurves = new VolumeCurvesCollection(); |
| 3393 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3394 | mDefaultOutputDevice, speakerDrcEnabled, |
| 3395 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); |
Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3396 | if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3397 | #else |
| 3398 | mVolumeCurves = new StreamDescriptorCollection(); |
| 3399 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, |
| 3400 | mDefaultOutputDevice, speakerDrcEnabled); |
| 3401 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && |
| 3402 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { |
| 3403 | #endif |
| 3404 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 3405 | config.setDefault(); |
| 3406 | } |
| 3407 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 3408 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); |
| 3409 | |
| 3410 | // 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] | 3411 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 3412 | if (!engineInstance) { |
| 3413 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 3414 | return; |
| 3415 | } |
| 3416 | // Retrieve the Policy Manager Interface |
| 3417 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 3418 | if (mEngine == NULL) { |
| 3419 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 3420 | return; |
| 3421 | } |
| 3422 | mEngine->setObserver(this); |
| 3423 | status_t status = mEngine->initCheck(); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3424 | (void) status; |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3425 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 3426 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3427 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3428 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3429 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 3430 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3431 | for (size_t i = 0; i < mHwModules.size(); i++) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3432 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3433 | if (mHwModules[i]->mHandle == 0) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3434 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3435 | continue; |
| 3436 | } |
| 3437 | // open all output streams needed to access attached devices |
| 3438 | // except for direct output streams that are only opened when they are actually |
| 3439 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3440 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3441 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3442 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3443 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3444 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3445 | if (!outProfile->hasSupportedDevices()) { |
| 3446 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3447 | continue; |
| 3448 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3449 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3450 | mTtsOutputAvailable = true; |
| 3451 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3452 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3453 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3454 | continue; |
| 3455 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3456 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3457 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 3458 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3459 | } else { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3460 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3461 | // outputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3462 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3463 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3464 | if ((profileType & outputDeviceTypes) == 0) { |
| 3465 | continue; |
| 3466 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3467 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 3468 | mpClientInterface); |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3469 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); |
| 3470 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); |
| 3471 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress |
| 3472 | : String8(""); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3473 | |
| 3474 | outputDesc->mDevice = profileType; |
| 3475 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3476 | config.sample_rate = outputDesc->mSamplingRate; |
| 3477 | config.channel_mask = outputDesc->mChannelMask; |
| 3478 | config.format = outputDesc->mFormat; |
| 3479 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3480 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3481 | &output, |
| 3482 | &config, |
| 3483 | &outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3484 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3485 | &outputDesc->mLatency, |
| 3486 | outputDesc->mFlags); |
| 3487 | |
| 3488 | if (status != NO_ERROR) { |
| 3489 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 3490 | outputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3491 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3492 | } else { |
| 3493 | outputDesc->mSamplingRate = config.sample_rate; |
| 3494 | outputDesc->mChannelMask = config.channel_mask; |
| 3495 | outputDesc->mFormat = config.format; |
| 3496 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3497 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3498 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3499 | // 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] | 3500 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3501 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3502 | } |
| 3503 | } |
| 3504 | if (mPrimaryOutput == 0 && |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3505 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3506 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3507 | } |
| 3508 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3509 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3510 | outputDesc->mDevice, |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3511 | true, |
| 3512 | 0, |
| 3513 | NULL, |
| 3514 | address.string()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3515 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3516 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3517 | // open input streams needed to access attached devices to validate |
| 3518 | // mAvailableInputDevices list |
| 3519 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3520 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3521 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3522 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3523 | if (!inProfile->hasSupportedDevices()) { |
| 3524 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3525 | continue; |
| 3526 | } |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3527 | // chose first device present in profile's SupportedDevices also part of |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3528 | // inputDeviceTypes |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3529 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); |
| 3530 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3531 | if ((profileType & inputDeviceTypes) == 0) { |
| 3532 | continue; |
| 3533 | } |
Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3534 | sp<AudioInputDescriptor> inputDesc = |
| 3535 | new AudioInputDescriptor(inProfile); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3536 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3537 | inputDesc->mDevice = profileType; |
| 3538 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3539 | // find the address |
| 3540 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3541 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3542 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3543 | : String8(""); |
| 3544 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3545 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3546 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3547 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3548 | config.sample_rate = inputDesc->mSamplingRate; |
| 3549 | config.channel_mask = inputDesc->mChannelMask; |
| 3550 | config.format = inputDesc->mFormat; |
| 3551 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3552 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3553 | &input, |
| 3554 | &config, |
| 3555 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3556 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3557 | AUDIO_SOURCE_MIC, |
| 3558 | AUDIO_INPUT_FLAG_NONE); |
| 3559 | |
| 3560 | if (status == NO_ERROR) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3561 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); |
| 3562 | for (size_t k = 0; k < supportedDevices.size(); k++) { |
| 3563 | ssize_t index = mAvailableInputDevices.indexOf(supportedDevices[k]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3564 | // 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] | 3565 | if (index >= 0) { |
| 3566 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; |
| 3567 | if (!devDesc->isAttached()) { |
| 3568 | devDesc->attach(mHwModules[i]); |
| 3569 | devDesc->importAudioPort(inProfile); |
| 3570 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3571 | } |
| 3572 | } |
| 3573 | mpClientInterface->closeInput(input); |
| 3574 | } else { |
| 3575 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3576 | inputDesc->mDevice, |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3577 | mHwModules[i]->getName()); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3578 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3579 | } |
| 3580 | } |
| 3581 | // make sure all attached devices have been allocated a unique ID |
| 3582 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3583 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3584 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3585 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3586 | continue; |
| 3587 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3588 | // The device is now validated and can be appended to the available devices of the engine |
| 3589 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 3590 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3591 | i++; |
| 3592 | } |
| 3593 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3594 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3595 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3596 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3597 | continue; |
| 3598 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3599 | // The device is now validated and can be appended to the available devices of the engine |
| 3600 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 3601 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3602 | i++; |
| 3603 | } |
| 3604 | // make sure default device is reachable |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3605 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3606 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3607 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3608 | |
| 3609 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3610 | |
| 3611 | updateDevicesAndOutputs(); |
| 3612 | |
| 3613 | #ifdef AUDIO_POLICY_TEST |
| 3614 | if (mPrimaryOutput != 0) { |
| 3615 | AudioParameter outputCmd = AudioParameter(); |
| 3616 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3617 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3618 | |
| 3619 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3620 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3621 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3622 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3623 | mTestLatencyMs = 0; |
| 3624 | mCurOutput = 0; |
| 3625 | mDirectOutput = false; |
| 3626 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3627 | mTestOutputs[i] = 0; |
| 3628 | } |
| 3629 | |
| 3630 | const size_t SIZE = 256; |
| 3631 | char buffer[SIZE]; |
| 3632 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3633 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3634 | } |
| 3635 | #endif //AUDIO_POLICY_TEST |
| 3636 | } |
| 3637 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3638 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3639 | { |
| 3640 | #ifdef AUDIO_POLICY_TEST |
| 3641 | exit(); |
| 3642 | #endif //AUDIO_POLICY_TEST |
| 3643 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3644 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3645 | } |
| 3646 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3647 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3648 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3649 | mAvailableOutputDevices.clear(); |
| 3650 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3651 | mOutputs.clear(); |
| 3652 | mInputs.clear(); |
| 3653 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3654 | } |
| 3655 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3656 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3657 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3658 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3659 | } |
| 3660 | |
| 3661 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3662 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3663 | { |
| 3664 | ALOGV("entering threadLoop()"); |
| 3665 | while (!exitPending()) |
| 3666 | { |
| 3667 | String8 command; |
| 3668 | int valueInt; |
| 3669 | String8 value; |
| 3670 | |
| 3671 | Mutex::Autolock _l(mLock); |
| 3672 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3673 | |
| 3674 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3675 | AudioParameter param = AudioParameter(command); |
| 3676 | |
| 3677 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3678 | valueInt != 0) { |
| 3679 | ALOGV("Test command %s received", command.string()); |
| 3680 | String8 target; |
| 3681 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3682 | target = "Manager"; |
| 3683 | } |
| 3684 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3685 | param.remove(String8("test_cmd_policy_output")); |
| 3686 | mCurOutput = valueInt; |
| 3687 | } |
| 3688 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3689 | param.remove(String8("test_cmd_policy_direct")); |
| 3690 | if (value == "false") { |
| 3691 | mDirectOutput = false; |
| 3692 | } else if (value == "true") { |
| 3693 | mDirectOutput = true; |
| 3694 | } |
| 3695 | } |
| 3696 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3697 | param.remove(String8("test_cmd_policy_input")); |
| 3698 | mTestInput = valueInt; |
| 3699 | } |
| 3700 | |
| 3701 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3702 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3703 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3704 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3705 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3706 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3707 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3708 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3709 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3710 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3711 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3712 | if (target == "Manager") { |
| 3713 | mTestFormat = format; |
| 3714 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3715 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3716 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3717 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3718 | } |
| 3719 | } |
| 3720 | } |
| 3721 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3722 | param.remove(String8("test_cmd_policy_channels")); |
| 3723 | int channels = 0; |
| 3724 | |
| 3725 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3726 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3727 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3728 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3729 | } |
| 3730 | if (channels != 0) { |
| 3731 | if (target == "Manager") { |
| 3732 | mTestChannels = channels; |
| 3733 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3734 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3735 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3736 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3737 | } |
| 3738 | } |
| 3739 | } |
| 3740 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3741 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3742 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3743 | int samplingRate = valueInt; |
| 3744 | if (target == "Manager") { |
| 3745 | mTestSamplingRate = samplingRate; |
| 3746 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3747 | AudioParameter outputParam = AudioParameter(); |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3748 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3749 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3750 | } |
| 3751 | } |
| 3752 | } |
| 3753 | |
| 3754 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3755 | param.remove(String8("test_cmd_policy_reopen")); |
| 3756 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3757 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3758 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3759 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3760 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3761 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3762 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3763 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3764 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3765 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3766 | config.sample_rate = outputDesc->mSamplingRate; |
| 3767 | config.channel_mask = outputDesc->mChannelMask; |
| 3768 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3769 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3770 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3771 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3772 | &config, |
| 3773 | &outputDesc->mDevice, |
| 3774 | String8(""), |
| 3775 | &outputDesc->mLatency, |
| 3776 | outputDesc->mFlags); |
| 3777 | if (status != NO_ERROR) { |
| 3778 | ALOGE("Failed to reopen hardware output stream, " |
| 3779 | "samplingRate: %d, format %d, channels %d", |
| 3780 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3781 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3782 | outputDesc->mSamplingRate = config.sample_rate; |
| 3783 | outputDesc->mChannelMask = config.channel_mask; |
| 3784 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3785 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3786 | AudioParameter outputCmd = AudioParameter(); |
| 3787 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3788 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3789 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | |
| 3794 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3795 | } |
| 3796 | } |
| 3797 | return false; |
| 3798 | } |
| 3799 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3800 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3801 | { |
| 3802 | { |
| 3803 | AutoMutex _l(mLock); |
| 3804 | requestExit(); |
| 3805 | mWaitWorkCV.signal(); |
| 3806 | } |
| 3807 | requestExitAndWait(); |
| 3808 | } |
| 3809 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3810 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3811 | { |
| 3812 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3813 | if (output == mTestOutputs[i]) return i; |
| 3814 | } |
| 3815 | return 0; |
| 3816 | } |
| 3817 | #endif //AUDIO_POLICY_TEST |
| 3818 | |
| 3819 | // --- |
| 3820 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3821 | void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3822 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3823 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3824 | mOutputs.add(output, outputDesc); |
Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3825 | updateMono(output); // update mono status when adding to output list |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 3826 | selectOutputForMusicEffects(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3827 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3830 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3831 | { |
| 3832 | mOutputs.removeItem(output); |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 3833 | selectOutputForMusicEffects(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3834 | } |
| 3835 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3836 | void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3837 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3838 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3839 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3840 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3841 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3842 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3843 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3844 | const audio_devices_t device /*in*/, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3845 | const String8& address /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3846 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3847 | sp<DeviceDescriptor> devDesc = |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3848 | desc->mProfile->getSupportedDeviceByAddress(device, address); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3849 | if (devDesc != 0) { |
| 3850 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3851 | desc->mIoHandle, address.string()); |
| 3852 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3853 | } |
| 3854 | } |
| 3855 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3856 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3857 | audio_policy_dev_state_t state, |
| 3858 | SortedVector<audio_io_handle_t>& outputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3859 | const String8& address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3860 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3861 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3862 | sp<SwAudioOutputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3863 | |
| 3864 | if (audio_device_is_digital(device)) { |
| 3865 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3866 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3867 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3868 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3869 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3870 | // first list already open outputs that can be routed to this device |
| 3871 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3872 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3873 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3874 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3875 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3876 | outputs.add(mOutputs.keyAt(i)); |
| 3877 | } else { |
| 3878 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3879 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3880 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3881 | } |
| 3882 | } |
| 3883 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3884 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3885 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3886 | { |
| 3887 | if (mHwModules[i]->mHandle == 0) { |
| 3888 | continue; |
| 3889 | } |
| 3890 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3891 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3892 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3893 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3894 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3895 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3896 | profiles.add(profile); |
| 3897 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3898 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3899 | } |
| 3900 | } |
| 3901 | } |
| 3902 | |
Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3903 | ALOGV(" found %zu profiles, %zu outputs", profiles.size(), outputs.size()); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3904 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3905 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3906 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3907 | return BAD_VALUE; |
| 3908 | } |
| 3909 | |
| 3910 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3911 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3912 | 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] | 3913 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | |
| 3915 | // nothing to do if one output is already opened for this profile |
| 3916 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3917 | for (j = 0; j < outputs.size(); j++) { |
| 3918 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3919 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3920 | // matching profile: save the sample rates, format and channel masks supported |
| 3921 | // by the profile in our device descriptor |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 3922 | if (audio_device_is_digital(device)) { |
| 3923 | devDesc->importAudioPort(profile); |
| 3924 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3925 | break; |
| 3926 | } |
| 3927 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3928 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3929 | continue; |
| 3930 | } |
| 3931 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3932 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3933 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3934 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3935 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3936 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3937 | config.sample_rate = desc->mSamplingRate; |
| 3938 | config.channel_mask = desc->mChannelMask; |
| 3939 | config.format = desc->mFormat; |
| 3940 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3941 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3942 | config.offload_info.format = desc->mFormat; |
| 3943 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3944 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3945 | &output, |
| 3946 | &config, |
| 3947 | &desc->mDevice, |
| 3948 | address, |
| 3949 | &desc->mLatency, |
| 3950 | desc->mFlags); |
| 3951 | if (status == NO_ERROR) { |
| 3952 | desc->mSamplingRate = config.sample_rate; |
| 3953 | desc->mChannelMask = config.channel_mask; |
| 3954 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3955 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3956 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3957 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3958 | char *param = audio_device_address_to_parameter(device, address); |
| 3959 | mpClientInterface->setParameters(output, String8(param)); |
| 3960 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3961 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 3962 | updateAudioProfiles(device, output, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3963 | if (!profile->hasValidAudioProfile()) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3964 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3965 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3966 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3967 | } else if (profile->hasDynamicAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3968 | mpClientInterface->closeOutput(output); |
Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 3969 | output = AUDIO_IO_HANDLE_NONE; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 3970 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3971 | config.offload_info.sample_rate = config.sample_rate; |
| 3972 | config.offload_info.channel_mask = config.channel_mask; |
| 3973 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3974 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3975 | &output, |
| 3976 | &config, |
| 3977 | &desc->mDevice, |
| 3978 | address, |
| 3979 | &desc->mLatency, |
| 3980 | desc->mFlags); |
| 3981 | if (status == NO_ERROR) { |
| 3982 | desc->mSamplingRate = config.sample_rate; |
| 3983 | desc->mChannelMask = config.channel_mask; |
| 3984 | desc->mFormat = config.format; |
| 3985 | } else { |
| 3986 | output = AUDIO_IO_HANDLE_NONE; |
| 3987 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3988 | } |
| 3989 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3990 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3991 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3992 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3993 | sp<AudioPolicyMix> policyMix; |
| 3994 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3995 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3996 | address.string()); |
| 3997 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3998 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3999 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4000 | |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4001 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 4002 | hasPrimaryOutput()) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4003 | // no duplicated output for direct outputs and |
| 4004 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4005 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4007 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4008 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4009 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4010 | //TODO: configure audio effect output stage here |
| 4011 | |
| 4012 | // 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] | 4013 | duplicatedOutput = |
| 4014 | mpClientInterface->openDuplicateOutput(output, |
| 4015 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4016 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4017 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4018 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 4019 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 4020 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 4021 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4022 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 4023 | dupOutputDesc->mFormat = desc->mFormat; |
| 4024 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 4025 | dupOutputDesc->mLatency = desc->mLatency; |
| 4026 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4027 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4028 | } else { |
| 4029 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4030 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4031 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4032 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4033 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4034 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4035 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4036 | } |
| 4037 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4038 | } else { |
| 4039 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4040 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4041 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4042 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4043 | profiles.removeAt(profile_index); |
| 4044 | profile_index--; |
| 4045 | } else { |
| 4046 | outputs.add(output); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4047 | // Load digital format info only for digital devices |
| 4048 | if (audio_device_is_digital(device)) { |
| 4049 | devDesc->importAudioPort(profile); |
| 4050 | } |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4051 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4052 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4053 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 4054 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4055 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4056 | NULL/*patch handle*/, address.string()); |
| 4057 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4058 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | if (profiles.isEmpty()) { |
| 4063 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 4064 | return BAD_VALUE; |
| 4065 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4066 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4067 | // check if one opened output is not needed any more after disconnecting one device |
| 4068 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4069 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4070 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4071 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4072 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4073 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4074 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4075 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4076 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 4077 | mOutputs.keyAt(i)); |
| 4078 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4079 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4080 | } |
| 4081 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4082 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4083 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4084 | { |
| 4085 | if (mHwModules[i]->mHandle == 0) { |
| 4086 | continue; |
| 4087 | } |
| 4088 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 4089 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4090 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4091 | if (profile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4092 | ALOGV("checkOutputsForDevice(): " |
| 4093 | "clearing direct output profile %zu on module %zu", j, i); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4094 | profile->clearAudioProfiles(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4095 | } |
| 4096 | } |
| 4097 | } |
| 4098 | } |
| 4099 | return NO_ERROR; |
| 4100 | } |
| 4101 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4102 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4103 | audio_policy_dev_state_t state, |
| 4104 | SortedVector<audio_io_handle_t>& inputs, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4105 | const String8& address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4106 | { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4107 | audio_devices_t device = devDesc->type(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4108 | sp<AudioInputDescriptor> desc; |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4109 | |
| 4110 | if (audio_device_is_digital(device)) { |
| 4111 | // erase all current sample rates, formats and channel masks |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4112 | devDesc->clearAudioProfiles(); |
Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4115 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 4116 | // first list already open inputs that can be routed to this device |
| 4117 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4118 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4119 | if (desc->mProfile->supportDevice(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4120 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 4121 | inputs.add(mInputs.keyAt(input_index)); |
| 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4126 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4127 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 4128 | { |
| 4129 | if (mHwModules[module_idx]->mHandle == 0) { |
| 4130 | continue; |
| 4131 | } |
| 4132 | for (size_t profile_index = 0; |
| 4133 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 4134 | profile_index++) |
| 4135 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4136 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 4137 | |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4138 | if (profile->supportDevice(device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4139 | if (!device_distinguishes_on_address(device) || |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4140 | profile->supportDeviceAddress(address)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4141 | profiles.add(profile); |
| 4142 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 4143 | profile_index, module_idx); |
| 4144 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | } |
| 4148 | |
| 4149 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 4150 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4151 | return BAD_VALUE; |
| 4152 | } |
| 4153 | |
| 4154 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 4155 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 4156 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 4157 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4158 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4159 | // nothing to do if one input is already opened for this profile |
| 4160 | size_t input_index; |
| 4161 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4162 | desc = mInputs.valueAt(input_index); |
| 4163 | if (desc->mProfile == profile) { |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4164 | if (audio_device_is_digital(device)) { |
| 4165 | devDesc->importAudioPort(profile); |
| 4166 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4167 | break; |
| 4168 | } |
| 4169 | } |
| 4170 | if (input_index != mInputs.size()) { |
| 4171 | continue; |
| 4172 | } |
| 4173 | |
| 4174 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 4175 | desc = new AudioInputDescriptor(profile); |
| 4176 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4177 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 4178 | config.sample_rate = desc->mSamplingRate; |
| 4179 | config.channel_mask = desc->mChannelMask; |
| 4180 | config.format = desc->mFormat; |
| 4181 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4182 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4183 | &input, |
| 4184 | &config, |
| 4185 | &desc->mDevice, |
| 4186 | address, |
| 4187 | AUDIO_SOURCE_MIC, |
| 4188 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4189 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4190 | if (status == NO_ERROR) { |
| 4191 | desc->mSamplingRate = config.sample_rate; |
| 4192 | desc->mChannelMask = config.channel_mask; |
| 4193 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4194 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4195 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4196 | char *param = audio_device_address_to_parameter(device, address); |
| 4197 | mpClientInterface->setParameters(input, String8(param)); |
| 4198 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4199 | } |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4200 | updateAudioProfiles(device, input, profile->getAudioProfiles()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4201 | if (!profile->hasValidAudioProfile()) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4202 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 4203 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4204 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
| 4207 | if (input != 0) { |
| 4208 | addInput(input, desc); |
| 4209 | } |
| 4210 | } // endif input != 0 |
| 4211 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4212 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4213 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4214 | profiles.removeAt(profile_index); |
| 4215 | profile_index--; |
| 4216 | } else { |
| 4217 | inputs.add(input); |
Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4218 | if (audio_device_is_digital(device)) { |
| 4219 | devDesc->importAudioPort(profile); |
| 4220 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4221 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 4222 | } |
| 4223 | } // end scan profiles |
| 4224 | |
| 4225 | if (profiles.isEmpty()) { |
| 4226 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 4227 | return BAD_VALUE; |
| 4228 | } |
| 4229 | } else { |
| 4230 | // Disconnect |
| 4231 | // check if one opened input is not needed any more after disconnecting one device |
| 4232 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 4233 | desc = mInputs.valueAt(input_index); |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4234 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4235 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 4236 | mInputs.keyAt(input_index)); |
| 4237 | inputs.add(mInputs.keyAt(input_index)); |
| 4238 | } |
| 4239 | } |
| 4240 | // Clear any profiles associated with the disconnected device. |
| 4241 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 4242 | if (mHwModules[module_index]->mHandle == 0) { |
| 4243 | continue; |
| 4244 | } |
| 4245 | for (size_t profile_index = 0; |
| 4246 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 4247 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4248 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4249 | if (profile->supportDevice(device)) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4250 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4251 | profile_index, module_index); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4252 | profile->clearAudioProfiles(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4253 | } |
| 4254 | } |
| 4255 | } |
| 4256 | } // end disconnect |
| 4257 | |
| 4258 | return NO_ERROR; |
| 4259 | } |
| 4260 | |
| 4261 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4262 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4263 | { |
| 4264 | ALOGV("closeOutput(%d)", output); |
| 4265 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4266 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4267 | if (outputDesc == NULL) { |
| 4268 | ALOGW("closeOutput() unknown output %d", output); |
| 4269 | return; |
| 4270 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4271 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4272 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4273 | // look for duplicated outputs connected to the output being removed. |
| 4274 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4275 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4276 | if (dupOutputDesc->isDuplicated() && |
| 4277 | (dupOutputDesc->mOutput1 == outputDesc || |
| 4278 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4279 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4280 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 4281 | outputDesc2 = dupOutputDesc->mOutput2; |
| 4282 | } else { |
| 4283 | outputDesc2 = dupOutputDesc->mOutput1; |
| 4284 | } |
| 4285 | // As all active tracks on duplicated output will be deleted, |
| 4286 | // and as they were also referenced on the other output, the reference |
| 4287 | // count for their stream type must be adjusted accordingly on |
| 4288 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4289 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4290 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4291 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4292 | } |
| 4293 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 4294 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 4295 | |
| 4296 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4297 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4298 | } |
| 4299 | } |
| 4300 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4301 | nextAudioPortGeneration(); |
| 4302 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4303 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4304 | if (index >= 0) { |
| 4305 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4306 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4307 | mAudioPatches.removeItemsAt(index); |
| 4308 | mpClientInterface->onAudioPatchListUpdate(); |
| 4309 | } |
| 4310 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4311 | AudioParameter param; |
| 4312 | param.add(String8("closing"), String8("true")); |
| 4313 | mpClientInterface->setParameters(output, param.toString()); |
| 4314 | |
| 4315 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4316 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4317 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 4321 | { |
| 4322 | ALOGV("closeInput(%d)", input); |
| 4323 | |
| 4324 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 4325 | if (inputDesc == NULL) { |
| 4326 | ALOGW("closeInput() unknown input %d", input); |
| 4327 | return; |
| 4328 | } |
| 4329 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4330 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4331 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4332 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4333 | if (index >= 0) { |
| 4334 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4335 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4336 | mAudioPatches.removeItemsAt(index); |
| 4337 | mpClientInterface->onAudioPatchListUpdate(); |
| 4338 | } |
| 4339 | |
| 4340 | mpClientInterface->closeInput(input); |
| 4341 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4342 | } |
| 4343 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4344 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 4345 | audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4346 | const SwAudioOutputCollection& openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4347 | { |
| 4348 | SortedVector<audio_io_handle_t> outputs; |
| 4349 | |
| 4350 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 4351 | for (size_t i = 0; i < openOutputs.size(); i++) { |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4352 | ALOGVV("output %zu isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4353 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 4354 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4355 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 4356 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 4357 | outputs.add(openOutputs.keyAt(i)); |
| 4358 | } |
| 4359 | } |
| 4360 | return outputs; |
| 4361 | } |
| 4362 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4363 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4364 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | { |
| 4366 | if (outputs1.size() != outputs2.size()) { |
| 4367 | return false; |
| 4368 | } |
| 4369 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 4370 | if (outputs1[i] != outputs2[i]) { |
| 4371 | return false; |
| 4372 | } |
| 4373 | } |
| 4374 | return true; |
| 4375 | } |
| 4376 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4377 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4378 | { |
| 4379 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4380 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 4381 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 4382 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 4383 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4384 | // also take into account external policy-related changes: add all outputs which are |
| 4385 | // associated with policies in the "before" and "after" output vectors |
| 4386 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 4387 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4388 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4389 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4390 | srcOutputs.add(desc->mIoHandle); |
| 4391 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 4392 | } |
| 4393 | } |
| 4394 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4395 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4396 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 4397 | dstOutputs.add(desc->mIoHandle); |
| 4398 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 4399 | } |
| 4400 | } |
| 4401 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4402 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 4403 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 4404 | strategy, srcOutputs[0], dstOutputs[0]); |
| 4405 | // mute strategy while moving tracks from one output to another |
| 4406 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4407 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4408 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4409 | setStrategyMute(strategy, true, desc); |
| 4410 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4411 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4412 | sp<AudioSourceDescriptor> source = |
| 4413 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); |
| 4414 | if (source != 0){ |
| 4415 | connectAudioSource(source); |
| 4416 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4417 | } |
| 4418 | |
| 4419 | // Move effects associated to this strategy from previous output to new output |
| 4420 | if (strategy == STRATEGY_MEDIA) { |
Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame^] | 4421 | selectOutputForMusicEffects(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4422 | } |
| 4423 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4424 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4425 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4426 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4427 | } |
| 4428 | } |
| 4429 | } |
| 4430 | } |
| 4431 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4432 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4433 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4434 | 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] | 4435 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4436 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4437 | 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] | 4438 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4439 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4440 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4441 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4442 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4443 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4444 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4447 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4448 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4449 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4450 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4451 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4452 | return; |
| 4453 | } |
| 4454 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4455 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4456 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4457 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4458 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4459 | |
| 4460 | // if suspended, restore A2DP output if: |
| 4461 | // ((SCO device is NOT connected) || |
| 4462 | // ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && |
| 4463 | // (phone state is NOT in call) && (phone state is NOT ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4464 | // |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4465 | // if not suspended, suspend A2DP output if: |
| 4466 | // (SCO device is connected) && |
| 4467 | // ((forced usage for communication is SCO) || (forced usage for record is SCO) || |
| 4468 | // ((phone state is in call) || (phone state is ringing))) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4469 | // |
| 4470 | if (mA2dpSuspended) { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4471 | if (!isScoConnected || |
| 4472 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != |
| 4473 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4474 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != |
| 4475 | AUDIO_POLICY_FORCE_BT_SCO) && |
| 4476 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4477 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4478 | |
| 4479 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4480 | mA2dpSuspended = false; |
| 4481 | } |
| 4482 | } else { |
Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4483 | if (isScoConnected && |
| 4484 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == |
| 4485 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4486 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == |
| 4487 | AUDIO_POLICY_FORCE_BT_SCO) || |
| 4488 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4489 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4490 | |
| 4491 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4492 | mA2dpSuspended = true; |
| 4493 | } |
| 4494 | } |
| 4495 | } |
| 4496 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4497 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 4498 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4499 | { |
| 4500 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4501 | |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4502 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4503 | if (index >= 0) { |
| 4504 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4505 | if (patchDesc->mUid != mUidCached) { |
| 4506 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4507 | outputDesc->device(), outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4508 | return outputDesc->device(); |
| 4509 | } |
| 4510 | } |
| 4511 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4512 | // 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] | 4513 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4514 | // use device for strategy enforced audible |
| 4515 | // 2: we are in call or the strategy phone is active on the output: |
| 4516 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4517 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4518 | // use the device for strategy enforced audible |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4519 | // 4: the strategy sonification is active on the output: |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4520 | // use device for strategy sonification |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4521 | // 5: the strategy accessibility is active on the output: |
| 4522 | // use device for strategy accessibility |
Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4523 | // 6: the strategy "respectful" sonification is active on the output: |
| 4524 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4525 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4526 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4527 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4528 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4529 | // 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] | 4530 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4531 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4532 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4533 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4534 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4535 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4537 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4538 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4539 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4540 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4541 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
| 4542 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4543 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4544 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4545 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4546 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4547 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4548 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4549 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4550 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4551 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4552 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4553 | } |
| 4554 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4555 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4556 | return device; |
| 4557 | } |
| 4558 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4559 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4560 | { |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4561 | audio_devices_t device = AUDIO_DEVICE_NONE; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4562 | |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4563 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4564 | if (index >= 0) { |
| 4565 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4566 | if (patchDesc->mUid != mUidCached) { |
| 4567 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4568 | inputDesc->mDevice, inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4569 | return inputDesc->mDevice; |
| 4570 | } |
| 4571 | } |
| 4572 | |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4573 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); |
| 4574 | if (isInCall()) { |
| 4575 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
| 4576 | } else if (source != AUDIO_SOURCE_DEFAULT) { |
| 4577 | device = getDeviceAndMixForInputSource(source); |
| 4578 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4579 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4580 | return device; |
| 4581 | } |
| 4582 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4583 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, |
| 4584 | audio_stream_type_t stream2) { |
Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4585 | return (stream1 == stream2); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4586 | } |
| 4587 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4588 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4589 | return (uint32_t)getStrategy(stream); |
| 4590 | } |
| 4591 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4592 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4593 | // By checking the range of stream before calling getStrategy, we avoid |
| 4594 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4595 | // 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] | 4596 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4597 | return AUDIO_DEVICE_NONE; |
| 4598 | } |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4599 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4600 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { |
| 4601 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4602 | continue; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4603 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4604 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4605 | audio_devices_t curDevices = |
Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4606 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4607 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); |
| 4608 | for (size_t i = 0; i < outputs.size(); i++) { |
| 4609 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4610 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4611 | curDevices |= outputDesc->device(); |
| 4612 | } |
| 4613 | } |
| 4614 | devices |= curDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4615 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4616 | |
| 4617 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4618 | and doesn't really need to.*/ |
| 4619 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4620 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4621 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4622 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4623 | return devices; |
| 4624 | } |
| 4625 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4626 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 4627 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4628 | 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] | 4629 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4630 | } |
| 4631 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4632 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4633 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4634 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4635 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4636 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4637 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4638 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4639 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4640 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4641 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4642 | } |
| 4643 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4644 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4645 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4646 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4647 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4648 | updateDevicesAndOutputs(); |
| 4649 | break; |
| 4650 | default: |
| 4651 | break; |
| 4652 | } |
| 4653 | } |
| 4654 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4655 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4656 | |
| 4657 | // skip beacon mute management if a dedicated TTS output is available |
| 4658 | if (mTtsOutputAvailable) { |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4662 | switch(event) { |
| 4663 | case STARTING_OUTPUT: |
| 4664 | mBeaconMuteRefCount++; |
| 4665 | break; |
| 4666 | case STOPPING_OUTPUT: |
| 4667 | if (mBeaconMuteRefCount > 0) { |
| 4668 | mBeaconMuteRefCount--; |
| 4669 | } |
| 4670 | break; |
| 4671 | case STARTING_BEACON: |
| 4672 | mBeaconPlayingRefCount++; |
| 4673 | break; |
| 4674 | case STOPPING_BEACON: |
| 4675 | if (mBeaconPlayingRefCount > 0) { |
| 4676 | mBeaconPlayingRefCount--; |
| 4677 | } |
| 4678 | break; |
| 4679 | } |
| 4680 | |
| 4681 | if (mBeaconMuteRefCount > 0) { |
| 4682 | // any playback causes beacon to be muted |
| 4683 | return setBeaconMute(true); |
| 4684 | } else { |
| 4685 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4686 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4687 | } |
| 4688 | } |
| 4689 | |
| 4690 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4691 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4692 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4693 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4694 | if (mBeaconMuted != mute) { |
| 4695 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4696 | ALOGV("\t muting %d", mute); |
| 4697 | uint32_t maxLatency = 0; |
| 4698 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4699 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4700 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4701 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4702 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4703 | const uint32_t latency = desc->latency() * 2; |
| 4704 | if (latency > maxLatency) { |
| 4705 | maxLatency = latency; |
| 4706 | } |
| 4707 | } |
| 4708 | mBeaconMuted = mute; |
| 4709 | return maxLatency; |
| 4710 | } |
| 4711 | return 0; |
| 4712 | } |
| 4713 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4714 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4715 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4716 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4717 | // Routing |
| 4718 | // see if we have an explicit route |
| 4719 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4720 | // (getStrategy(stream)). |
| 4721 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4722 | // and activity count is non-zero |
| 4723 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4724 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4725 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4726 | routing_strategy routeStrategy = getStrategy(route->mStreamType); |
| 4727 | if ((routeStrategy == strategy) && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4728 | return route->mDeviceDescriptor->type(); |
| 4729 | } |
| 4730 | } |
| 4731 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4732 | if (fromCache) { |
| 4733 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4734 | strategy, mDeviceForStrategy[strategy]); |
| 4735 | return mDeviceForStrategy[strategy]; |
| 4736 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4737 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4738 | } |
| 4739 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4740 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4741 | { |
| 4742 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4743 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4744 | } |
| 4745 | mPreviousOutputs = mOutputs; |
| 4746 | } |
| 4747 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4748 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4749 | audio_devices_t prevDevice, |
| 4750 | uint32_t delayMs) |
| 4751 | { |
| 4752 | // mute/unmute strategies using an incompatible device combination |
| 4753 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4754 | // if unmuting, unmute only after the specified delay |
| 4755 | if (outputDesc->isDuplicated()) { |
| 4756 | return 0; |
| 4757 | } |
| 4758 | |
| 4759 | uint32_t muteWaitMs = 0; |
| 4760 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4761 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4762 | |
| 4763 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4764 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4765 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4766 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4767 | bool doMute = false; |
| 4768 | |
| 4769 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4770 | doMute = true; |
| 4771 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4772 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4773 | doMute = true; |
| 4774 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4775 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4776 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4777 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4778 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4779 | // skip output if it does not share any device with current output |
| 4780 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4781 | == AUDIO_DEVICE_NONE) { |
| 4782 | continue; |
| 4783 | } |
Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4784 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4785 | mute ? "muting" : "unmuting", i, curDevice); |
| 4786 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4787 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4788 | if (mute) { |
| 4789 | // FIXME: should not need to double latency if volume could be applied |
| 4790 | // immediately by the audioflinger mixer. We must account for the delay |
| 4791 | // between now and the next time the audioflinger thread for this output |
| 4792 | // will process a buffer (which corresponds to one buffer size, |
| 4793 | // usually 1/2 or 1/4 of the latency). |
| 4794 | if (muteWaitMs < desc->latency() * 2) { |
| 4795 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4796 | } |
| 4797 | } |
| 4798 | } |
| 4799 | } |
| 4800 | } |
| 4801 | } |
| 4802 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4803 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4804 | // different per device volumes |
| 4805 | if (outputDesc->isActive() && (device != prevDevice)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4806 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; |
| 4807 | // temporary mute duration is conservatively set to 4 times the reported latency |
| 4808 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; |
| 4809 | if (muteWaitMs < tempMuteWaitMs) { |
| 4810 | muteWaitMs = tempMuteWaitMs; |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4811 | } |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4812 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4813 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4814 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4815 | // make sure that we do not start the temporary mute period too early in case of |
| 4816 | // delayed device change |
| 4817 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4818 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4819 | delayMs + tempMuteDurationMs, device); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4820 | } |
| 4821 | } |
| 4822 | } |
| 4823 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4824 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4825 | if (muteWaitMs > delayMs) { |
| 4826 | muteWaitMs -= delayMs; |
| 4827 | usleep(muteWaitMs * 1000); |
| 4828 | return muteWaitMs; |
| 4829 | } |
| 4830 | return 0; |
| 4831 | } |
| 4832 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4833 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4834 | audio_devices_t device, |
| 4835 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4836 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4837 | audio_patch_handle_t *patchHandle, |
| 4838 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4839 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4840 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4841 | AudioParameter param; |
| 4842 | uint32_t muteWaitMs; |
| 4843 | |
| 4844 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4845 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4846 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4847 | return muteWaitMs; |
| 4848 | } |
| 4849 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4850 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4851 | if ((device != AUDIO_DEVICE_NONE) && |
| 4852 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4853 | return 0; |
| 4854 | } |
| 4855 | |
| 4856 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4857 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4858 | |
| 4859 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4860 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4861 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4862 | |
| 4863 | if (device != AUDIO_DEVICE_NONE) { |
| 4864 | outputDesc->mDevice = device; |
| 4865 | } |
| 4866 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4867 | |
| 4868 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4869 | // the requested device is AUDIO_DEVICE_NONE |
| 4870 | // OR the requested device is the same as current device |
| 4871 | // AND force is not specified |
| 4872 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4873 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4874 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4875 | !force && |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4876 | outputDesc->getPatchHandle() != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4877 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4878 | return muteWaitMs; |
| 4879 | } |
| 4880 | |
| 4881 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4882 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4883 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4884 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4885 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4886 | } else { |
Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4887 | DeviceVector deviceList; |
| 4888 | if ((address == NULL) || (strlen(address) == 0)) { |
| 4889 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); |
| 4890 | } else { |
| 4891 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
| 4892 | } |
| 4893 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4894 | if (!deviceList.isEmpty()) { |
| 4895 | struct audio_patch patch; |
| 4896 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4897 | patch.num_sources = 1; |
| 4898 | patch.num_sinks = 0; |
| 4899 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4900 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4901 | patch.num_sinks++; |
| 4902 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4903 | ssize_t index; |
| 4904 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4905 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4906 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4907 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4908 | } |
| 4909 | sp< AudioPatch> patchDesc; |
| 4910 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4911 | if (index >= 0) { |
| 4912 | patchDesc = mAudioPatches.valueAt(index); |
| 4913 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4914 | } |
| 4915 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4916 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4917 | &afPatchHandle, |
| 4918 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4919 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4920 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4921 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4922 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4923 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4924 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4925 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4926 | } else { |
| 4927 | patchDesc->mPatch = patch; |
| 4928 | } |
| 4929 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4930 | if (patchHandle) { |
| 4931 | *patchHandle = patchDesc->mHandle; |
| 4932 | } |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4933 | outputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4934 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4935 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4936 | } |
| 4937 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4938 | |
| 4939 | // inform all input as well |
| 4940 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4941 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4942 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4943 | AudioParameter inputCmd = AudioParameter(); |
| 4944 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4945 | inputDescriptor->mIoHandle, device); |
| 4946 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4947 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4948 | inputCmd.toString(), |
| 4949 | delayMs); |
| 4950 | } |
| 4951 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4952 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4953 | |
| 4954 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4955 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4956 | |
| 4957 | return muteWaitMs; |
| 4958 | } |
| 4959 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4960 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4961 | int delayMs, |
| 4962 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4963 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4964 | ssize_t index; |
| 4965 | if (patchHandle) { |
| 4966 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4967 | } else { |
Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4968 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4969 | } |
| 4970 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4971 | return INVALID_OPERATION; |
| 4972 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4973 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4974 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4975 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 4976 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4977 | removeAudioPatch(patchDesc->mHandle); |
| 4978 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4979 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4980 | return status; |
| 4981 | } |
| 4982 | |
| 4983 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4984 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4985 | bool force, |
| 4986 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4987 | { |
| 4988 | status_t status = NO_ERROR; |
| 4989 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4990 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4991 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4992 | inputDesc->mDevice = device; |
| 4993 | |
| 4994 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4995 | if (!deviceList.isEmpty()) { |
| 4996 | struct audio_patch patch; |
| 4997 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4998 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4999 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5000 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 5001 | !inputDesc->isSoundTrigger()) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5002 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 5003 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5004 | patch.num_sinks = 1; |
| 5005 | //only one input device for now |
| 5006 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5007 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5008 | ssize_t index; |
| 5009 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 5010 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5011 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5012 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5013 | } |
| 5014 | sp< AudioPatch> patchDesc; |
| 5015 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 5016 | if (index >= 0) { |
| 5017 | patchDesc = mAudioPatches.valueAt(index); |
| 5018 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 5019 | } |
| 5020 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5021 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5022 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5023 | 0); |
| 5024 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5025 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5026 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5027 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5028 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5029 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 5030 | } else { |
| 5031 | patchDesc->mPatch = patch; |
| 5032 | } |
| 5033 | patchDesc->mAfPatchHandle = afPatchHandle; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5034 | if (patchHandle) { |
| 5035 | *patchHandle = patchDesc->mHandle; |
| 5036 | } |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5037 | inputDesc->setPatchHandle(patchDesc->mHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5038 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5039 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5040 | } |
| 5041 | } |
| 5042 | } |
| 5043 | return status; |
| 5044 | } |
| 5045 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5046 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 5047 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5048 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5049 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5050 | ssize_t index; |
| 5051 | if (patchHandle) { |
| 5052 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 5053 | } else { |
Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5054 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5055 | } |
| 5056 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5057 | return INVALID_OPERATION; |
| 5058 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5059 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 5060 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5061 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5062 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5063 | removeAudioPatch(patchDesc->mHandle); |
| 5064 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5065 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5066 | return status; |
| 5067 | } |
| 5068 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5069 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5070 | const String8& address, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5071 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5072 | audio_format_t& format, |
| 5073 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5074 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5075 | { |
| 5076 | // Choose an input profile based on the requested capture parameters: select the first available |
| 5077 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5078 | // |
| 5079 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 5080 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5081 | |
| 5082 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 5083 | { |
| 5084 | if (mHwModules[i]->mHandle == 0) { |
| 5085 | continue; |
| 5086 | } |
| 5087 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 5088 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5089 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5090 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5091 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5092 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5093 | format, |
| 5094 | &format /*updatedFormat*/, |
| 5095 | channelMask, |
| 5096 | &channelMask /*updatedChannelMask*/, |
| 5097 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5098 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5099 | return profile; |
| 5100 | } |
| 5101 | } |
| 5102 | } |
| 5103 | return NULL; |
| 5104 | } |
| 5105 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5106 | |
| 5107 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5108 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5109 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5110 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 5111 | audio_devices_t selectedDeviceFromMix = |
| 5112 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5113 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5114 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 5115 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5116 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5117 | return getDeviceForInputSource(inputSource); |
| 5118 | } |
| 5119 | |
| 5120 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5121 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5122 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 5123 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5124 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5125 | return route->mDeviceDescriptor->type(); |
| 5126 | } |
| 5127 | } |
| 5128 | |
| 5129 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5130 | } |
| 5131 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5132 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5133 | int index, |
| 5134 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5136 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); |
Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5137 | |
| 5138 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much |
| 5139 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch |
| 5140 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to |
| 5141 | // the ringtone volume |
| 5142 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) |
| 5143 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) |
| 5144 | && isStreamActive(AUDIO_STREAM_RING, 0)) { |
| 5145 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); |
| 5146 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; |
| 5147 | } |
| 5148 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5149 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5150 | // to avoid sound level bursts in user's ears: |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5151 | // - always attenuate notifications volume by 6dB |
| 5152 | // - attenuate ring tones volume by 6dB unless music is not playing and |
| 5153 | // speaker is part of the select devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5154 | // - if music is playing, always limit the volume to current music volume, |
| 5155 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5156 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5157 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5158 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5159 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5160 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 5161 | AUDIO_DEVICE_OUT_USB_HEADSET)) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5162 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5163 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5164 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5165 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5166 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5167 | mVolumeCurves->canBeMuted(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5168 | // when the phone is ringing we must consider that music could have been paused just before |
| 5169 | // by the music application and behave as if music was active if the last music track was |
| 5170 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5171 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5172 | mLimitRingtoneVolume) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5173 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5174 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5175 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5176 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, |
| 5177 | musicDevice), |
| 5178 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5179 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 5180 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5181 | if (volumeDB > minVolDB) { |
| 5182 | volumeDB = minVolDB; |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5183 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5184 | } |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5185 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5186 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { |
| 5187 | // on A2DP, also ensure notification volume is not too low compared to media when |
| 5188 | // intended to be played |
| 5189 | if ((volumeDB > -96.0f) && |
| 5190 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { |
| 5191 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", |
| 5192 | stream, device, |
| 5193 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); |
| 5194 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; |
| 5195 | } |
| 5196 | } |
Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5197 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || |
| 5198 | stream_strategy != STRATEGY_SONIFICATION) { |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5199 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5200 | } |
| 5201 | } |
| 5202 | |
Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5203 | return volumeDB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5204 | } |
| 5205 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5206 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5207 | int index, |
| 5208 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5209 | audio_devices_t device, |
| 5210 | int delayMs, |
| 5211 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5212 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5213 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5214 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5215 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5216 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5217 | return NO_ERROR; |
| 5218 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5219 | audio_policy_forced_cfg_t forceUseForComm = |
| 5220 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5221 | // 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] | 5222 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5223 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5224 | 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] | 5225 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5226 | return INVALID_OPERATION; |
| 5227 | } |
| 5228 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5229 | if (device == AUDIO_DEVICE_NONE) { |
| 5230 | device = outputDesc->device(); |
| 5231 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5232 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5233 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5234 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5235 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5236 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5237 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5238 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5239 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5240 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5241 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5242 | float voiceVolume; |
| 5243 | // 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] | 5244 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5245 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | } else { |
| 5247 | voiceVolume = 1.0; |
| 5248 | } |
| 5249 | |
Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5250 | if (voiceVolume != mLastVoiceVolume) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5251 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5252 | mLastVoiceVolume = voiceVolume; |
| 5253 | } |
| 5254 | } |
| 5255 | |
| 5256 | return NO_ERROR; |
| 5257 | } |
| 5258 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5259 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 5260 | audio_devices_t device, |
| 5261 | int delayMs, |
| 5262 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5263 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5264 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5265 | |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5266 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5267 | checkAndSetVolume((audio_stream_type_t)stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5268 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5269 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5270 | device, |
| 5271 | delayMs, |
| 5272 | force); |
| 5273 | } |
| 5274 | } |
| 5275 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5276 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5277 | bool on, |
| 5278 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5279 | int delayMs, |
| 5280 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5281 | { |
Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5282 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", |
| 5283 | strategy, on, outputDesc->getId()); |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5284 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5285 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5286 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5287 | } |
| 5288 | } |
| 5289 | } |
| 5290 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5291 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5292 | bool on, |
| 5293 | const sp<AudioOutputDescriptor>& outputDesc, |
| 5294 | int delayMs, |
| 5295 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5296 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5297 | if (device == AUDIO_DEVICE_NONE) { |
| 5298 | device = outputDesc->device(); |
| 5299 | } |
| 5300 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5301 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 5302 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5303 | |
| 5304 | if (on) { |
| 5305 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5306 | if (mVolumeCurves->canBeMuted(stream) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5307 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5308 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5309 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5310 | } |
| 5311 | } |
| 5312 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5313 | outputDesc->mMuteCount[stream]++; |
| 5314 | } else { |
| 5315 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5316 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5317 | return; |
| 5318 | } |
| 5319 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5320 | checkAndSetVolume(stream, |
François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5321 | mVolumeCurves->getVolumeIndex(stream, device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5322 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5323 | device, |
| 5324 | delayMs); |
| 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5329 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5330 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5331 | { |
Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5332 | if(!hasPrimaryOutput()) { |
| 5333 | return; |
| 5334 | } |
| 5335 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5336 | // if the stream pertains to sonification strategy and we are in call we must |
| 5337 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5338 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5339 | // interfere with the device used for phone strategy |
| 5340 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5341 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5342 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5343 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5344 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5345 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5346 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5347 | stream, starting, outputDesc->mDevice, stateChange); |
| 5348 | if (outputDesc->mRefCount[stream]) { |
| 5349 | int muteCount = 1; |
| 5350 | if (stateChange) { |
| 5351 | muteCount = outputDesc->mRefCount[stream]; |
| 5352 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5353 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5354 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5355 | for (int i = 0; i < muteCount; i++) { |
| 5356 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5357 | } |
| 5358 | } else { |
| 5359 | ALOGV("handleIncallSonification() high visibility"); |
| 5360 | if (outputDesc->device() & |
| 5361 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5362 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5363 | for (int i = 0; i < muteCount; i++) { |
| 5364 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5365 | } |
| 5366 | } |
| 5367 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5368 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5369 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5370 | } else { |
| 5371 | mpClientInterface->stopTone(); |
| 5372 | } |
| 5373 | } |
| 5374 | } |
| 5375 | } |
| 5376 | } |
| 5377 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5378 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5379 | { |
| 5380 | // flags to stream type mapping |
| 5381 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5382 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5383 | } |
| 5384 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5385 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5386 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5387 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5388 | return AUDIO_STREAM_TTS; |
| 5389 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5390 | |
| 5391 | // usage to stream type mapping |
| 5392 | switch (attr->usage) { |
| 5393 | case AUDIO_USAGE_MEDIA: |
| 5394 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5395 | case AUDIO_USAGE_ASSISTANT: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5396 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5397 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5398 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5399 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5400 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5401 | return AUDIO_STREAM_SYSTEM; |
| 5402 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5403 | return AUDIO_STREAM_VOICE_CALL; |
| 5404 | |
| 5405 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5406 | return AUDIO_STREAM_DTMF; |
| 5407 | |
| 5408 | case AUDIO_USAGE_ALARM: |
| 5409 | return AUDIO_STREAM_ALARM; |
| 5410 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5411 | return AUDIO_STREAM_RING; |
| 5412 | |
| 5413 | case AUDIO_USAGE_NOTIFICATION: |
| 5414 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5415 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5416 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5417 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5418 | return AUDIO_STREAM_NOTIFICATION; |
| 5419 | |
| 5420 | case AUDIO_USAGE_UNKNOWN: |
| 5421 | default: |
| 5422 | return AUDIO_STREAM_MUSIC; |
| 5423 | } |
| 5424 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5425 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5426 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 5427 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5428 | // has flags that map to a strategy? |
| 5429 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5430 | return true; |
| 5431 | } |
| 5432 | |
| 5433 | // has known usage? |
| 5434 | switch (paa->usage) { |
| 5435 | case AUDIO_USAGE_UNKNOWN: |
| 5436 | case AUDIO_USAGE_MEDIA: |
| 5437 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5438 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5439 | case AUDIO_USAGE_ALARM: |
| 5440 | case AUDIO_USAGE_NOTIFICATION: |
| 5441 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5442 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5443 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5444 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5445 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5446 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5447 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5448 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5449 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5450 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5451 | case AUDIO_USAGE_ASSISTANT: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5452 | break; |
| 5453 | default: |
| 5454 | return false; |
| 5455 | } |
| 5456 | return true; |
| 5457 | } |
| 5458 | |
Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5459 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5460 | routing_strategy strategy, uint32_t inPastMs, |
| 5461 | nsecs_t sysTime) const |
| 5462 | { |
| 5463 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 5464 | sysTime = systemTime(); |
| 5465 | } |
Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5466 | 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] | 5467 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 5468 | (NUM_STRATEGIES == strategy)) && |
| 5469 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 5470 | return true; |
| 5471 | } |
| 5472 | } |
| 5473 | return false; |
| 5474 | } |
| 5475 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5476 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 5477 | { |
| 5478 | return mEngine->getForceUse(usage); |
| 5479 | } |
| 5480 | |
| 5481 | bool AudioPolicyManager::isInCall() |
| 5482 | { |
| 5483 | return isStateInCall(mEngine->getPhoneState()); |
| 5484 | } |
| 5485 | |
| 5486 | bool AudioPolicyManager::isStateInCall(int state) |
| 5487 | { |
| 5488 | return is_state_in_call(state); |
| 5489 | } |
| 5490 | |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5491 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) |
| 5492 | { |
| 5493 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--) { |
| 5494 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); |
| 5495 | if (sourceDesc->mDevice->equals(deviceDesc)) { |
| 5496 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); |
| 5497 | stopAudioSource(sourceDesc->getHandle()); |
| 5498 | } |
| 5499 | } |
| 5500 | |
| 5501 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
| 5502 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 5503 | bool release = false; |
| 5504 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++) { |
| 5505 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; |
| 5506 | if (source->type == AUDIO_PORT_TYPE_DEVICE && |
| 5507 | source->ext.device.type == deviceDesc->type()) { |
| 5508 | release = true; |
| 5509 | } |
| 5510 | } |
| 5511 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++) { |
| 5512 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; |
| 5513 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && |
| 5514 | sink->ext.device.type == deviceDesc->type()) { |
| 5515 | release = true; |
| 5516 | } |
| 5517 | } |
| 5518 | if (release) { |
| 5519 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); |
| 5520 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); |
| 5521 | } |
| 5522 | } |
| 5523 | } |
| 5524 | |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5525 | // Modify the list of surround sound formats supported. |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5526 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { |
| 5527 | FormatVector &formats = *formatsPtr; |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5528 | // TODO Set this based on Config properties. |
| 5529 | const bool alwaysForceAC3 = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5530 | |
| 5531 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5532 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5533 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5534 | |
| 5535 | // Analyze original support for various formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5536 | bool supportsAC3 = false; |
| 5537 | bool supportsOtherSurround = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5538 | bool supportsIEC61937 = false; |
| 5539 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { |
| 5540 | audio_format_t format = formats[formatIndex]; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5541 | switch (format) { |
| 5542 | case AUDIO_FORMAT_AC3: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5543 | supportsAC3 = true; |
| 5544 | break; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5545 | case AUDIO_FORMAT_E_AC3: |
| 5546 | case AUDIO_FORMAT_DTS: |
| 5547 | case AUDIO_FORMAT_DTS_HD: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5548 | supportsOtherSurround = true; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5549 | break; |
| 5550 | case AUDIO_FORMAT_IEC61937: |
| 5551 | supportsIEC61937 = true; |
| 5552 | break; |
| 5553 | default: |
| 5554 | break; |
| 5555 | } |
| 5556 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5557 | |
| 5558 | // Modify formats based on surround preferences. |
| 5559 | // If NEVER, remove support for surround formats. |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5560 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5561 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { |
| 5562 | // Remove surround sound related formats. |
| 5563 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { |
| 5564 | audio_format_t format = formats[formatIndex]; |
| 5565 | switch(format) { |
| 5566 | case AUDIO_FORMAT_AC3: |
| 5567 | case AUDIO_FORMAT_E_AC3: |
| 5568 | case AUDIO_FORMAT_DTS: |
| 5569 | case AUDIO_FORMAT_DTS_HD: |
| 5570 | case AUDIO_FORMAT_IEC61937: |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5571 | formats.removeAt(formatIndex); |
| 5572 | break; |
| 5573 | default: |
| 5574 | formatIndex++; // keep it |
| 5575 | break; |
| 5576 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5577 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5578 | supportsAC3 = false; |
| 5579 | supportsOtherSurround = false; |
| 5580 | supportsIEC61937 = false; |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5581 | } |
Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5582 | } else { // AUTO or ALWAYS |
| 5583 | // Most TVs support AC3 even if they do not report it in the EDID. |
| 5584 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) |
| 5585 | && !supportsAC3) { |
| 5586 | formats.add(AUDIO_FORMAT_AC3); |
| 5587 | supportsAC3 = true; |
| 5588 | } |
| 5589 | |
| 5590 | // If ALWAYS, add support for raw surround formats if all are missing. |
| 5591 | // This assumes that if any of these formats are reported by the HAL |
| 5592 | // then the report is valid and should not be modified. |
| 5593 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) |
| 5594 | && !supportsOtherSurround) { |
| 5595 | formats.add(AUDIO_FORMAT_E_AC3); |
| 5596 | formats.add(AUDIO_FORMAT_DTS); |
| 5597 | formats.add(AUDIO_FORMAT_DTS_HD); |
| 5598 | supportsOtherSurround = true; |
| 5599 | } |
| 5600 | |
| 5601 | // Add support for IEC61937 if any raw surround supported. |
| 5602 | // The HAL could do this but add it here, just in case. |
| 5603 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { |
| 5604 | formats.add(AUDIO_FORMAT_IEC61937); |
| 5605 | supportsIEC61937 = true; |
| 5606 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5607 | } |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | // Modify the list of channel masks supported. |
| 5611 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { |
| 5612 | ChannelsVector &channelMasks = *channelMasksPtr; |
| 5613 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( |
| 5614 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); |
| 5615 | |
| 5616 | // If NEVER, then remove support for channelMasks > stereo. |
| 5617 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { |
| 5618 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { |
| 5619 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5620 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { |
| 5621 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); |
| 5622 | channelMasks.removeAt(maskIndex); |
| 5623 | } else { |
| 5624 | maskIndex++; |
| 5625 | } |
| 5626 | } |
| 5627 | // If ALWAYS, then make sure we at least support 5.1 |
| 5628 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { |
| 5629 | bool supports5dot1 = false; |
| 5630 | // Are there any channel masks that can be considered "surround"? |
| 5631 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { |
| 5632 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; |
| 5633 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { |
| 5634 | supports5dot1 = true; |
| 5635 | break; |
| 5636 | } |
| 5637 | } |
| 5638 | // If not then add 5.1 support. |
| 5639 | if (!supports5dot1) { |
| 5640 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); |
| 5641 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); |
| 5642 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5643 | } |
| 5644 | } |
| 5645 | |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5646 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, |
| 5647 | audio_io_handle_t ioHandle, |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5648 | AudioProfileVector &profiles) |
| 5649 | { |
| 5650 | String8 reply; |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5651 | |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5652 | // Format MUST be checked first to update the list of AudioProfile |
| 5653 | if (profiles.hasDynamicFormat()) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5654 | reply = mpClientInterface->getParameters( |
| 5655 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5656 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5657 | AudioParameter repliedParameters(reply); |
| 5658 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5659 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5660 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); |
| 5661 | return; |
| 5662 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5663 | FormatVector formats = formatsFromString(reply.string()); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5664 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5665 | filterSurroundFormats(&formats); |
Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5666 | } |
Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5667 | profiles.setFormats(formats); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5668 | } |
| 5669 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); |
| 5670 | |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5671 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5672 | audio_format_t format = supportedFormats[formatIndex]; |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5673 | ChannelsVector channelMasks; |
| 5674 | SampleRateVector samplingRates; |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5675 | AudioParameter requestedParameters; |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5676 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5677 | |
| 5678 | if (profiles.hasDynamicRateFor(format)) { |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5679 | reply = mpClientInterface->getParameters( |
| 5680 | ioHandle, |
| 5681 | requestedParameters.toString() + ";" + |
| 5682 | AudioParameter::keyStreamSupportedSamplingRates); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5683 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5684 | AudioParameter repliedParameters(reply); |
| 5685 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5686 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5687 | samplingRates = samplingRatesFromString(reply.string()); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5688 | } |
| 5689 | } |
| 5690 | if (profiles.hasDynamicChannelsFor(format)) { |
| 5691 | reply = mpClientInterface->getParameters(ioHandle, |
| 5692 | requestedParameters.toString() + ";" + |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5693 | AudioParameter::keyStreamSupportedChannels); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5694 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5695 | AudioParameter repliedParameters(reply); |
| 5696 | if (repliedParameters.get( |
Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5697 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { |
Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5698 | channelMasks = channelMasksFromString(reply.string()); |
Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5699 | if (device == AUDIO_DEVICE_OUT_HDMI) { |
| 5700 | filterSurroundChannelMasks(&channelMasks); |
| 5701 | } |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5702 | } |
| 5703 | } |
Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5704 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); |
François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5705 | } |
| 5706 | } |
Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5707 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5708 | }; // namespace android |