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 | 56ec4ff | 2015-01-23 16:45:18 -0800 | [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 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 27 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 28 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 29 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 30 | #include <AudioPolicyManagerInterface.h> |
| 31 | #include <AudioPolicyEngineInstance.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 32 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 33 | #include <utils/Log.h> |
| 34 | #include <hardware/audio.h> |
| 35 | #include <hardware/audio_effect.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 36 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 37 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 38 | #include <soundtrigger/SoundTrigger.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 39 | #include "AudioPolicyManager.h" |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 40 | #include "audio_policy_conf.h" |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 41 | #include <ConfigParsingUtils.h> |
| 42 | #include <policy.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 43 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 44 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 45 | |
| 46 | // ---------------------------------------------------------------------------- |
| 47 | // AudioPolicyInterface implementation |
| 48 | // ---------------------------------------------------------------------------- |
| 49 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 50 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 51 | audio_policy_dev_state_t state, |
| 52 | const char *device_address, |
| 53 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 54 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 55 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 59 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 60 | const char *device_address, |
| 61 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 62 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 63 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 64 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 65 | |
| 66 | // connect/disconnect only 1 device at a time |
| 67 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 68 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 69 | sp<DeviceDescriptor> devDesc = |
| 70 | mHwModules.getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 71 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 72 | // handle output devices |
| 73 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 74 | SortedVector <audio_io_handle_t> outputs; |
| 75 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 76 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 77 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 78 | // save a copy of the opened output descriptors before any output is opened or closed |
| 79 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 80 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 81 | switch (state) |
| 82 | { |
| 83 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 84 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 85 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 86 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 87 | return INVALID_OPERATION; |
| 88 | } |
| 89 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 90 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 91 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 92 | index = mAvailableOutputDevices.add(devDesc); |
| 93 | if (index >= 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 94 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 95 | if (module == 0) { |
| 96 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 97 | device); |
| 98 | mAvailableOutputDevices.remove(devDesc); |
| 99 | return INVALID_OPERATION; |
| 100 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 101 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 102 | } else { |
| 103 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 106 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 107 | mAvailableOutputDevices.remove(devDesc); |
| 108 | return INVALID_OPERATION; |
| 109 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 110 | // Propagate device availability to Engine |
| 111 | mEngine->setDeviceConnectionState(devDesc, state); |
| 112 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 113 | // outputs should never be empty here |
| 114 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 115 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 116 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 117 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 118 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 119 | // Send connect to HALs |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 120 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 121 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 122 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 123 | |
| 124 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 125 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 126 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 127 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 128 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 129 | return INVALID_OPERATION; |
| 130 | } |
| 131 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 132 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 133 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 134 | // Send Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 135 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 136 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 137 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 138 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 139 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 140 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 141 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 142 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 143 | |
| 144 | // Propagate device availability to Engine |
| 145 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 146 | } break; |
| 147 | |
| 148 | default: |
| 149 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 150 | return BAD_VALUE; |
| 151 | } |
| 152 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 153 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 154 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 155 | checkA2dpSuspend(); |
| 156 | checkOutputForAllStrategies(); |
| 157 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 158 | if (!outputs.isEmpty()) { |
| 159 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 160 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | // close unused outputs after device disconnection or direct outputs that have been |
| 162 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 163 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 164 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 165 | (desc->mDirectOpenCount == 0))) { |
| 166 | closeOutput(outputs[i]); |
| 167 | } |
| 168 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 169 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 170 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | updateDevicesAndOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 174 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 175 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 176 | updateCallRouting(newDevice); |
| 177 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 178 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 179 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 180 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { |
| 181 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 182 | // do not force device change on duplicated output because if device is 0, it will |
| 183 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 184 | // a valid device selection on those outputs. |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 185 | bool force = !desc->isDuplicated() |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 186 | && (!device_distinguishes_on_address(device) |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 187 | // always force when disconnecting (a non-duplicated device) |
| 188 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 189 | setOutputDevice(desc, newDevice, force, 0); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 190 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 193 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 194 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 195 | } // end if is output device |
| 196 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 197 | // handle input devices |
| 198 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 199 | SortedVector <audio_io_handle_t> inputs; |
| 200 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 201 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 202 | switch (state) |
| 203 | { |
| 204 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 205 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 206 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 207 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 208 | return INVALID_OPERATION; |
| 209 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 210 | sp<HwModule> module = mHwModules.getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 211 | if (module == NULL) { |
| 212 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 213 | device); |
| 214 | return INVALID_OPERATION; |
| 215 | } |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 216 | if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 217 | return INVALID_OPERATION; |
| 218 | } |
| 219 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 220 | index = mAvailableInputDevices.add(devDesc); |
| 221 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 222 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 223 | } else { |
| 224 | return NO_MEMORY; |
| 225 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 226 | |
| 227 | // Set connect to HALs |
| 228 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 229 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 230 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 231 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 232 | // Propagate device availability to Engine |
| 233 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 234 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 235 | |
| 236 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 237 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 238 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 239 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 240 | return INVALID_OPERATION; |
| 241 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 242 | |
| 243 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 244 | |
| 245 | // Set Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 246 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 247 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 248 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 249 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 250 | checkInputsForDevice(device, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 251 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 252 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 253 | // Propagate device availability to Engine |
| 254 | mEngine->setDeviceConnectionState(devDesc, state); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 255 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 256 | |
| 257 | default: |
| 258 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 259 | return BAD_VALUE; |
| 260 | } |
| 261 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 262 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 264 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 265 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 266 | updateCallRouting(newDevice); |
| 267 | } |
| 268 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 269 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 270 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 271 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 272 | |
| 273 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 274 | return BAD_VALUE; |
| 275 | } |
| 276 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 277 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 278 | const char *device_address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 279 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 280 | sp<DeviceDescriptor> devDesc = mHwModules.getDeviceDescriptor(device, device_address, ""); |
| 281 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 282 | DeviceVector *deviceVector; |
| 283 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 284 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 285 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 286 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 287 | deviceVector = &mAvailableInputDevices; |
| 288 | } else { |
| 289 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 290 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 291 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 292 | return deviceVector->getDeviceConnectionState(devDesc); |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 295 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 296 | { |
| 297 | bool createTxPatch = false; |
| 298 | struct audio_patch patch; |
| 299 | patch.num_sources = 1; |
| 300 | patch.num_sinks = 1; |
| 301 | status_t status; |
| 302 | audio_patch_handle_t afPatchHandle; |
| 303 | DeviceVector deviceList; |
| 304 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 305 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 306 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 307 | |
| 308 | // release existing RX patch if any |
| 309 | if (mCallRxPatch != 0) { |
| 310 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 311 | mCallRxPatch.clear(); |
| 312 | } |
| 313 | // release TX patch if any |
| 314 | if (mCallTxPatch != 0) { |
| 315 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 316 | mCallTxPatch.clear(); |
| 317 | } |
| 318 | |
| 319 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 320 | // via setOutputDevice() on primary output. |
| 321 | // Otherwise, create two audio patches for TX and RX path. |
| 322 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 323 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 324 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 325 | // of it due to legacy implementation. If not, create a patch. |
| 326 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 327 | == AUDIO_DEVICE_NONE) { |
| 328 | createTxPatch = true; |
| 329 | } |
| 330 | } else { |
| 331 | // create RX path audio patch |
| 332 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 333 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 334 | "updateCallRouting() selected device not in output device list"); |
| 335 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 336 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 337 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 338 | "updateCallRouting() no telephony RX device"); |
| 339 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 340 | |
| 341 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 342 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 343 | |
| 344 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 345 | SortedVector<audio_io_handle_t> outputs = |
| 346 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 347 | audio_io_handle_t output = selectOutput(outputs, |
| 348 | AUDIO_OUTPUT_FLAG_NONE, |
| 349 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 350 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 351 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 352 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 353 | "updateCallRouting() RX device output is duplicated"); |
| 354 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 355 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 356 | patch.num_sources = 2; |
| 357 | } |
| 358 | |
| 359 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 360 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 361 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 362 | status); |
| 363 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 364 | mCallRxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 365 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 366 | mCallRxPatch->mUid = mUidCached; |
| 367 | } |
| 368 | createTxPatch = true; |
| 369 | } |
| 370 | if (createTxPatch) { |
| 371 | |
| 372 | struct audio_patch patch; |
| 373 | patch.num_sources = 1; |
| 374 | patch.num_sinks = 1; |
| 375 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 376 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 377 | "updateCallRouting() selected device not in input device list"); |
| 378 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 379 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 380 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 381 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 382 | "updateCallRouting() no telephony TX device"); |
| 383 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 384 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 385 | |
| 386 | SortedVector<audio_io_handle_t> outputs = |
| 387 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 388 | audio_io_handle_t output = selectOutput(outputs, |
| 389 | AUDIO_OUTPUT_FLAG_NONE, |
| 390 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 391 | // request to reuse existing output stream if one is already opened to reach the TX |
| 392 | // path output device |
| 393 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 394 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 395 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 396 | "updateCallRouting() RX device output is duplicated"); |
| 397 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 398 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 399 | patch.num_sources = 2; |
| 400 | } |
| 401 | |
| 402 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 403 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 404 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 405 | status); |
| 406 | if (status == NO_ERROR) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 407 | mCallTxPatch = new AudioPatch(&patch, mUidCached); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 408 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 409 | mCallTxPatch->mUid = mUidCached; |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 414 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 415 | { |
| 416 | ALOGV("setPhoneState() state %d", state); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 417 | // store previous phone state for management of sonification strategy below |
| 418 | int oldState = mEngine->getPhoneState(); |
| 419 | |
| 420 | if (mEngine->setPhoneState(state) != NO_ERROR) { |
| 421 | ALOGW("setPhoneState() invalid or same state %d", state); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 422 | return; |
| 423 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 424 | /// Opens: can these line be executed after the switch of volume curves??? |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 425 | // if leaving call state, handle special case of active streams |
| 426 | // pertaining to sonification strategy see handleIncallSonification() |
| 427 | if (isInCall()) { |
| 428 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 429 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 430 | if (stream == AUDIO_STREAM_PATCH) { |
| 431 | continue; |
| 432 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 433 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 434 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 435 | |
| 436 | // force reevaluating accessibility routing when call starts |
| 437 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 438 | } |
| 439 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 440 | /** |
| 441 | * Switching to or from incall state or switching between telephony and VoIP lead to force |
| 442 | * routing command. |
| 443 | */ |
| 444 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) |
| 445 | || (is_state_in_call(state) && (state != oldState))); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 446 | |
| 447 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | checkA2dpSuspend(); |
| 449 | checkOutputForAllStrategies(); |
| 450 | updateDevicesAndOutputs(); |
| 451 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 452 | sp<SwAudioOutputDescriptor> hwOutputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 453 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 454 | int delayMs = 0; |
| 455 | if (isStateInCall(state)) { |
| 456 | nsecs_t sysTime = systemTime(); |
| 457 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 458 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 459 | // mute media and sonification strategies and delay device switch by the largest |
| 460 | // latency of any output where either strategy is active. |
| 461 | // 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] | 462 | if ((isStrategyActive(desc, STRATEGY_MEDIA, |
| 463 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 464 | sysTime) || |
| 465 | isStrategyActive(desc, STRATEGY_SONIFICATION, |
| 466 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 467 | sysTime)) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 468 | (delayMs < (int)desc->latency()*2)) { |
| 469 | delayMs = desc->latency()*2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 470 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 471 | setStrategyMute(STRATEGY_MEDIA, true, desc); |
| 472 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 473 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 474 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); |
| 475 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 476 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 477 | } |
| 478 | } |
| 479 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 480 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 481 | // the device returned is not necessarily reachable via this output |
| 482 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 483 | // force routing command to audio hardware when ending call |
| 484 | // even if no device change is needed |
| 485 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 486 | rxDevice = hwOutputDesc->device(); |
| 487 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 488 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 489 | if (state == AUDIO_MODE_IN_CALL) { |
| 490 | updateCallRouting(rxDevice, delayMs); |
| 491 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 492 | if (mCallRxPatch != 0) { |
| 493 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 494 | mCallRxPatch.clear(); |
| 495 | } |
| 496 | if (mCallTxPatch != 0) { |
| 497 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 498 | mCallTxPatch.clear(); |
| 499 | } |
| 500 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 501 | } else { |
| 502 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 503 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 504 | // if entering in call state, handle special case of active streams |
| 505 | // pertaining to sonification strategy see handleIncallSonification() |
| 506 | if (isStateInCall(state)) { |
| 507 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 508 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 509 | if (stream == AUDIO_STREAM_PATCH) { |
| 510 | continue; |
| 511 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 512 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | // 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] | 517 | if (state == AUDIO_MODE_RINGTONE && |
| 518 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 519 | mLimitRingtoneVolume = true; |
| 520 | } else { |
| 521 | mLimitRingtoneVolume = false; |
| 522 | } |
| 523 | } |
| 524 | |
Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 525 | audio_mode_t AudioPolicyManager::getPhoneState() { |
| 526 | return mEngine->getPhoneState(); |
| 527 | } |
| 528 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 529 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 530 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 531 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 532 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 533 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 534 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { |
| 535 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); |
| 536 | return; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 537 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 538 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || |
| 539 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || |
| 540 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 541 | |
| 542 | // check for device and output changes triggered by new force usage |
| 543 | checkA2dpSuspend(); |
| 544 | checkOutputForAllStrategies(); |
| 545 | updateDevicesAndOutputs(); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 546 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 547 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 548 | updateCallRouting(newDevice); |
| 549 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 550 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 551 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 552 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); |
| 553 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { |
| 554 | setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 555 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 557 | applyStreamVolumes(outputDesc, newDevice, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 561 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 562 | if (activeInput != 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 563 | setInputDevice(activeInput, getNewInputDevice(activeInput)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | } |
| 567 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 568 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 569 | { |
| 570 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 571 | } |
| 572 | |
| 573 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 574 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 575 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 576 | audio_devices_t device, |
| 577 | uint32_t samplingRate, |
| 578 | audio_format_t format, |
| 579 | audio_channel_mask_t channelMask, |
| 580 | audio_output_flags_t flags) |
| 581 | { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame^] | 582 | // only retain flags that will drive the direct output profile selection |
| 583 | // if explicitly requested |
| 584 | static const uint32_t kRelevantFlags = |
| 585 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 586 | flags = |
| 587 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); |
| 588 | |
| 589 | sp<IOProfile> profile; |
| 590 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 591 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 592 | if (mHwModules[i]->mHandle == 0) { |
| 593 | continue; |
| 594 | } |
| 595 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame^] | 596 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; |
| 597 | if (!curProfile->isCompatibleProfile(device, String8(""), |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 598 | samplingRate, NULL /*updatedSamplingRate*/, |
| 599 | format, NULL /*updatedFormat*/, |
| 600 | channelMask, NULL /*updatedChannelMask*/, |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame^] | 601 | flags)) { |
| 602 | continue; |
| 603 | } |
| 604 | // reject profiles not corresponding to a device currently available |
| 605 | if ((mAvailableOutputDevices.types() & curProfile->mSupportedDevices.types()) == 0) { |
| 606 | continue; |
| 607 | } |
| 608 | // if several profiles are compatible, give priority to one with offload capability |
| 609 | if (profile != 0 && ((curProfile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { |
| 610 | continue; |
| 611 | } |
| 612 | profile = curProfile; |
| 613 | if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 614 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 615 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 616 | } |
| 617 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame^] | 618 | return profile; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 621 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 622 | uint32_t samplingRate, |
| 623 | audio_format_t format, |
| 624 | audio_channel_mask_t channelMask, |
| 625 | audio_output_flags_t flags, |
| 626 | const audio_offload_info_t *offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 628 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 629 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 630 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 631 | device, stream, samplingRate, format, channelMask, flags); |
| 632 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 633 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 634 | stream, samplingRate,format, channelMask, |
| 635 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 638 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 639 | audio_io_handle_t *output, |
| 640 | audio_session_t session, |
| 641 | audio_stream_type_t *stream, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 642 | uid_t uid, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 643 | uint32_t samplingRate, |
| 644 | audio_format_t format, |
| 645 | audio_channel_mask_t channelMask, |
| 646 | audio_output_flags_t flags, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 647 | audio_port_handle_t selectedDeviceId, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 648 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 649 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 650 | audio_attributes_t attributes; |
| 651 | if (attr != NULL) { |
| 652 | if (!isValidAttributes(attr)) { |
| 653 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 654 | attr->usage, attr->content_type, attr->flags, |
| 655 | attr->tags); |
| 656 | return BAD_VALUE; |
| 657 | } |
| 658 | attributes = *attr; |
| 659 | } else { |
| 660 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 661 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 662 | return BAD_VALUE; |
| 663 | } |
| 664 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 665 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 666 | sp<SwAudioOutputDescriptor> desc; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 667 | if (mPolicyMixes.getOutputForAttr(attributes, desc) == NO_ERROR) { |
| 668 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); |
| 669 | if (!audio_is_linear_pcm(format)) { |
| 670 | return BAD_VALUE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 671 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 672 | *stream = streamTypefromAttributesInt(&attributes); |
| 673 | *output = desc->mIoHandle; |
| 674 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 675 | return NO_ERROR; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 676 | } |
| 677 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 678 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 679 | return BAD_VALUE; |
| 680 | } |
| 681 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 682 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" |
| 683 | " session %d selectedDeviceId %d", |
| 684 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, |
| 685 | session, selectedDeviceId); |
| 686 | |
| 687 | *stream = streamTypefromAttributesInt(&attributes); |
| 688 | |
| 689 | // Explicit routing? |
| 690 | sp<DeviceDescriptor> deviceDesc; |
| 691 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
| 692 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { |
| 693 | deviceDesc = mAvailableOutputDevices[i]; |
| 694 | break; |
| 695 | } |
| 696 | } |
| 697 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 698 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 699 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 700 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 701 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 702 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 703 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 704 | } |
| 705 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 706 | ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 707 | device, samplingRate, format, channelMask, flags); |
| 708 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 709 | *output = getOutputForDevice(device, session, *stream, |
| 710 | samplingRate, format, channelMask, |
| 711 | flags, offloadInfo); |
| 712 | if (*output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 713 | mOutputRoutes.removeRoute(session); |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 714 | return INVALID_OPERATION; |
| 715 | } |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 716 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 717 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 721 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 722 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 723 | audio_stream_type_t stream, |
| 724 | uint32_t samplingRate, |
| 725 | audio_format_t format, |
| 726 | audio_channel_mask_t channelMask, |
| 727 | audio_output_flags_t flags, |
| 728 | const audio_offload_info_t *offloadInfo) |
| 729 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 730 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 731 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 732 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 733 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 734 | #ifdef AUDIO_POLICY_TEST |
| 735 | if (mCurOutput != 0) { |
| 736 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 737 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 738 | |
| 739 | if (mTestOutputs[mCurOutput] == 0) { |
| 740 | ALOGV("getOutput() opening test output"); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 741 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, |
| 742 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 743 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 744 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 745 | outputDesc->mFlags = |
| 746 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 747 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 748 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 749 | config.sample_rate = mTestSamplingRate; |
| 750 | config.channel_mask = mTestChannels; |
| 751 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 752 | if (offloadInfo != NULL) { |
| 753 | config.offload_info = *offloadInfo; |
| 754 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 755 | status = mpClientInterface->openOutput(0, |
| 756 | &mTestOutputs[mCurOutput], |
| 757 | &config, |
| 758 | &outputDesc->mDevice, |
| 759 | String8(""), |
| 760 | &outputDesc->mLatency, |
| 761 | outputDesc->mFlags); |
| 762 | if (status == NO_ERROR) { |
| 763 | outputDesc->mSamplingRate = config.sample_rate; |
| 764 | outputDesc->mFormat = config.format; |
| 765 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 766 | AudioParameter outputCmd = AudioParameter(); |
| 767 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 768 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 769 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 770 | } |
| 771 | } |
| 772 | return mTestOutputs[mCurOutput]; |
| 773 | } |
| 774 | #endif //AUDIO_POLICY_TEST |
| 775 | |
| 776 | // open a direct output if required by specified parameters |
| 777 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 778 | // and all common behaviors are driven by checking only the direct flag |
| 779 | // this should normally be set appropriately in the policy configuration file |
| 780 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 781 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 782 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 783 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 784 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 785 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 786 | // only allow deep buffering for music stream type |
| 787 | if (stream != AUDIO_STREAM_MUSIC) { |
| 788 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 789 | } |
Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 790 | if (stream == AUDIO_STREAM_TTS) { |
| 791 | flags = AUDIO_OUTPUT_FLAG_TTS; |
| 792 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 793 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 794 | sp<IOProfile> profile; |
| 795 | |
| 796 | // 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] | 797 | // and not explicitly requested |
| 798 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 799 | audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 800 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 801 | goto non_direct_output; |
| 802 | } |
| 803 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 804 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 805 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 806 | // detects there is an active non offloadable effect. |
| 807 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 808 | // This may prevent offloading in rare situations where effects are left active by apps |
| 809 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 810 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 811 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 812 | !mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 813 | profile = getProfileForDirectOutput(device, |
| 814 | samplingRate, |
| 815 | format, |
| 816 | channelMask, |
| 817 | (audio_output_flags_t)flags); |
| 818 | } |
| 819 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 820 | if (profile != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 821 | sp<SwAudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 822 | |
| 823 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 824 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 825 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 826 | outputDesc = desc; |
| 827 | // reuse direct output if currently open and configured with same parameters |
| 828 | if ((samplingRate == outputDesc->mSamplingRate) && |
| 829 | (format == outputDesc->mFormat) && |
| 830 | (channelMask == outputDesc->mChannelMask)) { |
| 831 | outputDesc->mDirectOpenCount++; |
| 832 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 833 | return mOutputs.keyAt(i); |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | // close direct output if currently open and configured with different parameters |
| 838 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 839 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 840 | } |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame^] | 841 | |
| 842 | // if the selected profile is offloaded and no offload info was specified, |
| 843 | // create a default one |
| 844 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; |
| 845 | if ((profile->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { |
| 846 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
| 847 | defaultOffloadInfo.sample_rate = samplingRate; |
| 848 | defaultOffloadInfo.channel_mask = channelMask; |
| 849 | defaultOffloadInfo.format = format; |
| 850 | defaultOffloadInfo.stream_type = stream; |
| 851 | defaultOffloadInfo.bit_rate = 0; |
| 852 | defaultOffloadInfo.duration_us = -1; |
| 853 | defaultOffloadInfo.has_video = true; // conservative |
| 854 | defaultOffloadInfo.is_streaming = true; // likely |
| 855 | offloadInfo = &defaultOffloadInfo; |
| 856 | } |
| 857 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 858 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 859 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 860 | outputDesc->mLatency = 0; |
Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame^] | 861 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 862 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 863 | config.sample_rate = samplingRate; |
| 864 | config.channel_mask = channelMask; |
| 865 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 866 | if (offloadInfo != NULL) { |
| 867 | config.offload_info = *offloadInfo; |
| 868 | } |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 869 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 870 | &output, |
| 871 | &config, |
| 872 | &outputDesc->mDevice, |
| 873 | String8(""), |
| 874 | &outputDesc->mLatency, |
| 875 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 876 | |
| 877 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 878 | if (status != NO_ERROR || |
| 879 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
| 880 | (format != AUDIO_FORMAT_DEFAULT && format != config.format) || |
| 881 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 882 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 883 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 884 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 885 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 886 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 887 | mpClientInterface->closeOutput(output); |
| 888 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 889 | // fall back to mixer output if possible when the direct output could not be open |
| 890 | if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) { |
| 891 | goto non_direct_output; |
| 892 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 893 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 894 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 895 | outputDesc->mSamplingRate = config.sample_rate; |
| 896 | outputDesc->mChannelMask = config.channel_mask; |
| 897 | outputDesc->mFormat = config.format; |
| 898 | outputDesc->mRefCount[stream] = 0; |
| 899 | outputDesc->mStopTime[stream] = 0; |
| 900 | outputDesc->mDirectOpenCount = 1; |
| 901 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 902 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 903 | addOutput(output, outputDesc); |
| 904 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 905 | if (dstOutput == output) { |
| 906 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 907 | } |
| 908 | mPreviousOutputs = mOutputs; |
| 909 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 910 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 911 | return output; |
| 912 | } |
| 913 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 914 | non_direct_output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 915 | // ignoring channel mask due to downmix capability in mixer |
| 916 | |
| 917 | // open a non direct output |
| 918 | |
| 919 | // for non direct outputs, only PCM is supported |
| 920 | if (audio_is_linear_pcm(format)) { |
| 921 | // get which output is suitable for the specified stream. The actual |
| 922 | // routing change will happen when startOutput() will be called |
| 923 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 924 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 925 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 926 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 927 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 928 | } |
| 929 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 930 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 931 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 932 | ALOGV(" getOutputForDevice() returns output %d", output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 933 | |
| 934 | return output; |
| 935 | } |
| 936 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 937 | 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] | 938 | audio_output_flags_t flags, |
| 939 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 | { |
| 941 | // select one output among several that provide a path to a particular device or set of |
| 942 | // devices (the list was previously build by getOutputsForDevice()). |
| 943 | // The priority is as follows: |
| 944 | // 1: the output with the highest number of requested policy flags |
| 945 | // 2: the primary output |
| 946 | // 3: the first output in the list |
| 947 | |
| 948 | if (outputs.size() == 0) { |
| 949 | return 0; |
| 950 | } |
| 951 | if (outputs.size() == 1) { |
| 952 | return outputs[0]; |
| 953 | } |
| 954 | |
| 955 | int maxCommonFlags = 0; |
| 956 | audio_io_handle_t outputFlags = 0; |
| 957 | audio_io_handle_t outputPrimary = 0; |
| 958 | |
| 959 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 960 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 961 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 962 | // if a valid format is specified, skip output if not compatible |
| 963 | if (format != AUDIO_FORMAT_INVALID) { |
| 964 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 965 | if (format != outputDesc->mFormat) { |
| 966 | continue; |
| 967 | } |
| 968 | } else if (!audio_is_linear_pcm(format)) { |
| 969 | continue; |
| 970 | } |
| 971 | } |
| 972 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 973 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 974 | if (commonFlags > maxCommonFlags) { |
| 975 | outputFlags = outputs[i]; |
| 976 | maxCommonFlags = commonFlags; |
| 977 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 978 | } |
| 979 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 980 | outputPrimary = outputs[i]; |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | if (outputFlags != 0) { |
| 986 | return outputFlags; |
| 987 | } |
| 988 | if (outputPrimary != 0) { |
| 989 | return outputPrimary; |
| 990 | } |
| 991 | |
| 992 | return outputs[0]; |
| 993 | } |
| 994 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 995 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 996 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 997 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 998 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 999 | ALOGV("startOutput() output %d, stream %d, session %d", |
| 1000 | output, stream, session); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1001 | ssize_t index = mOutputs.indexOfKey(output); |
| 1002 | if (index < 0) { |
| 1003 | ALOGW("startOutput() unknown output %d", output); |
| 1004 | return BAD_VALUE; |
| 1005 | } |
| 1006 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1007 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
| 1008 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1009 | // Routing? |
| 1010 | mOutputRoutes.incRouteActivity(session); |
| 1011 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1012 | audio_devices_t newDevice; |
| 1013 | if (outputDesc->mPolicyMix != NULL) { |
| 1014 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1015 | } else if (mOutputRoutes.hasRouteChanged(session)) { |
| 1016 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1017 | checkStrategyRoute(getStrategy(stream), output); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1018 | } else { |
| 1019 | newDevice = AUDIO_DEVICE_NONE; |
| 1020 | } |
| 1021 | |
| 1022 | uint32_t delayMs = 0; |
| 1023 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1024 | status_t status = startSource(outputDesc, stream, newDevice, &delayMs); |
| 1025 | |
| 1026 | if (status != NO_ERROR) { |
| 1027 | mOutputRoutes.decRouteActivity(session); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1028 | return status; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1029 | } |
| 1030 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1031 | // of type MIX_TYPE_RECORDERS |
| 1032 | if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL && |
| 1033 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1034 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1035 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
| 1036 | outputDesc->mPolicyMix->mRegistrationId, |
| 1037 | "remote-submix"); |
| 1038 | } |
| 1039 | |
| 1040 | if (delayMs != 0) { |
| 1041 | usleep(delayMs * 1000); |
| 1042 | } |
| 1043 | |
| 1044 | return status; |
| 1045 | } |
| 1046 | |
| 1047 | status_t AudioPolicyManager::startSource(sp<AudioOutputDescriptor> outputDesc, |
| 1048 | audio_stream_type_t stream, |
| 1049 | audio_devices_t device, |
| 1050 | uint32_t *delayMs) |
| 1051 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1052 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1053 | uint32_t beaconMuteLatency = 0; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1054 | |
| 1055 | *delayMs = 0; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1056 | if (stream == AUDIO_STREAM_TTS) { |
| 1057 | ALOGV("\t found BEACON stream"); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1058 | if (mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1059 | return INVALID_OPERATION; |
| 1060 | } else { |
| 1061 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1062 | } |
| 1063 | } else { |
| 1064 | // some playback other than beacon starts |
| 1065 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1066 | } |
| 1067 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1068 | // increment usage count for this stream on the requested output: |
| 1069 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1070 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1071 | outputDesc->changeRefCount(stream, 1); |
| 1072 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1073 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1074 | // starting an output being rerouted? |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1075 | if (device == AUDIO_DEVICE_NONE) { |
| 1076 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1077 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1078 | routing_strategy strategy = getStrategy(stream); |
| 1079 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1080 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1081 | (beaconMuteLatency > 0); |
| 1082 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1083 | bool force = false; |
| 1084 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1085 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1086 | if (desc != outputDesc) { |
| 1087 | // force a device change if any other output is managed by the same hw |
| 1088 | // module and has a current device selection that differs from selected device. |
| 1089 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1090 | // change the device currently selected by the other active output. |
| 1091 | if (outputDesc->sharesHwModuleWith(desc) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1092 | desc->device() != device) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1093 | force = true; |
| 1094 | } |
| 1095 | // 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] | 1096 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1097 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1098 | uint32_t latency = desc->latency(); |
| 1099 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1100 | waitMs = latency; |
| 1101 | } |
| 1102 | } |
| 1103 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1104 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1105 | |
| 1106 | // handle special case for sonification while in call |
| 1107 | if (isInCall()) { |
| 1108 | handleIncallSonification(stream, true, false); |
| 1109 | } |
| 1110 | |
| 1111 | // apply volume rules for current stream and device if necessary |
| 1112 | checkAndSetVolume(stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1113 | mStreams.valueFor(stream).getVolumeIndex(device), |
| 1114 | outputDesc, |
| 1115 | device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1116 | |
| 1117 | // update the outputs if starting an output with a stream that can affect notification |
| 1118 | // routing |
| 1119 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1120 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1121 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1122 | if (strategy == STRATEGY_SONIFICATION) { |
| 1123 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1124 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1125 | } |
| 1126 | return NO_ERROR; |
| 1127 | } |
| 1128 | |
| 1129 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1130 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1131 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1132 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1133 | { |
| 1134 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1135 | ssize_t index = mOutputs.indexOfKey(output); |
| 1136 | if (index < 0) { |
| 1137 | ALOGW("stopOutput() unknown output %d", output); |
| 1138 | return BAD_VALUE; |
| 1139 | } |
| 1140 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1141 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1142 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1143 | if (outputDesc->mRefCount[stream] == 1) { |
| 1144 | // Automatically disable the remote submix input when output is stopped on a |
| 1145 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1146 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1147 | outputDesc->mPolicyMix != NULL && |
| 1148 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1149 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
| 1150 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
| 1151 | outputDesc->mPolicyMix->mRegistrationId, |
| 1152 | "remote-submix"); |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | // Routing? |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1157 | bool forceDeviceUpdate = false; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1158 | if (outputDesc->mRefCount[stream] > 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1159 | int activityCount = mOutputRoutes.decRouteActivity(session); |
| 1160 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); |
| 1161 | |
| 1162 | if (forceDeviceUpdate) { |
| 1163 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); |
| 1164 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1167 | return stopSource(outputDesc, stream, forceDeviceUpdate); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | status_t AudioPolicyManager::stopSource(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1171 | audio_stream_type_t stream, |
| 1172 | bool forceDeviceUpdate) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1173 | { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1174 | // always handle stream stop, check which stream type is stopping |
| 1175 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1176 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1177 | // handle special case for sonification while in call |
| 1178 | if (isInCall()) { |
| 1179 | handleIncallSonification(stream, false, false); |
| 1180 | } |
| 1181 | |
| 1182 | if (outputDesc->mRefCount[stream] > 0) { |
| 1183 | // decrement usage count of this stream on the output |
| 1184 | outputDesc->changeRefCount(stream, -1); |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1185 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1186 | // store time at which the stream was stopped - see isStreamActive() |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1187 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1188 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1189 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1190 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1191 | // the track stop() command is received and at that time the audio track buffer can |
| 1192 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1193 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1194 | // audio path (audio DSP, CODEC ...) |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1195 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1196 | |
| 1197 | // force restoring the device selection on other active outputs if it differs from the |
| 1198 | // one being selected for this output |
| 1199 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1200 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1201 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1202 | if (desc != outputDesc && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1203 | desc->isActive() && |
| 1204 | outputDesc->sharesHwModuleWith(desc) && |
| 1205 | (newDevice != desc->device())) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1206 | setOutputDevice(desc, |
| 1207 | getNewOutputDevice(desc, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1208 | true, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1209 | outputDesc->latency()*2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1213 | handleNotificationRoutingForStream(stream); |
| 1214 | } |
| 1215 | return NO_ERROR; |
| 1216 | } else { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1217 | ALOGW("stopOutput() refcount is already 0"); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1218 | return INVALID_OPERATION; |
| 1219 | } |
| 1220 | } |
| 1221 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1222 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1223 | audio_stream_type_t stream __unused, |
| 1224 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1225 | { |
| 1226 | ALOGV("releaseOutput() %d", output); |
| 1227 | ssize_t index = mOutputs.indexOfKey(output); |
| 1228 | if (index < 0) { |
| 1229 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | #ifdef AUDIO_POLICY_TEST |
| 1234 | int testIndex = testOutputIndex(output); |
| 1235 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1236 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1237 | if (outputDesc->isActive()) { |
| 1238 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1239 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1240 | mTestOutputs[testIndex] = 0; |
| 1241 | } |
| 1242 | return; |
| 1243 | } |
| 1244 | #endif //AUDIO_POLICY_TEST |
| 1245 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1246 | // Routing |
| 1247 | mOutputRoutes.removeRoute(session); |
| 1248 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1249 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1250 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1251 | if (desc->mDirectOpenCount <= 0) { |
| 1252 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1253 | desc->mDirectOpenCount, output); |
| 1254 | return; |
| 1255 | } |
| 1256 | if (--desc->mDirectOpenCount == 0) { |
| 1257 | closeOutput(output); |
| 1258 | // If effects where present on the output, audioflinger moved them to the primary |
| 1259 | // output by default: move them back to the appropriate output. |
| 1260 | audio_io_handle_t dstOutput = getOutputForEffect(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1261 | if (dstOutput != mPrimaryOutput->mIoHandle) { |
| 1262 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, |
| 1263 | mPrimaryOutput->mIoHandle, dstOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1264 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1265 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1271 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1272 | audio_io_handle_t *input, |
| 1273 | audio_session_t session, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1274 | uid_t uid, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1275 | uint32_t samplingRate, |
| 1276 | audio_format_t format, |
| 1277 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1278 | audio_input_flags_t flags, |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1279 | audio_port_handle_t selectedDeviceId, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1280 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1281 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1282 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1283 | "session %d, flags %#x", |
| 1284 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1285 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1286 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1287 | *inputType = API_INPUT_INVALID; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1288 | audio_devices_t device; |
| 1289 | // handle legacy remote submix case where the address was not always specified |
| 1290 | String8 address = String8(""); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1291 | bool isSoundTrigger = false; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1292 | audio_source_t inputSource = attr->source; |
| 1293 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1294 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1295 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1296 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1297 | inputSource = AUDIO_SOURCE_MIC; |
| 1298 | } |
| 1299 | halInputSource = inputSource; |
| 1300 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1301 | // Explicit routing? |
| 1302 | sp<DeviceDescriptor> deviceDesc; |
| 1303 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 1304 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { |
| 1305 | deviceDesc = mAvailableInputDevices[i]; |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1309 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1310 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1311 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1312 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1313 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1314 | if (ret != NO_ERROR) { |
| 1315 | return ret; |
| 1316 | } |
| 1317 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1318 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1319 | address = String8(attr->tags + strlen("addr=")); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1320 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1321 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1322 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1323 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1324 | return BAD_VALUE; |
| 1325 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1326 | if (policyMix != NULL) { |
| 1327 | address = policyMix->mRegistrationId; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1328 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1329 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1330 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1331 | // know about it and is therefore considered "legacy" |
| 1332 | *inputType = API_INPUT_LEGACY; |
| 1333 | } else { |
| 1334 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1335 | // an external policy |
| 1336 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1337 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1338 | } else if (audio_is_remote_submix_device(device)) { |
| 1339 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1340 | *inputType = API_INPUT_MIX_CAPTURE; |
| 1341 | } else { |
| 1342 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1343 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1344 | // adapt channel selection to input source |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1345 | switch (inputSource) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1346 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1347 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; |
| 1348 | break; |
| 1349 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1350 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1351 | break; |
| 1352 | case AUDIO_SOURCE_VOICE_CALL: |
| 1353 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1354 | break; |
| 1355 | default: |
| 1356 | break; |
| 1357 | } |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1358 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1359 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1360 | if (index >= 0) { |
| 1361 | *input = mSoundTriggerSessions.valueFor(session); |
| 1362 | isSoundTrigger = true; |
| 1363 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1364 | ALOGV("SoundTrigger capture on session %d input %d", session, *input); |
| 1365 | } else { |
| 1366 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 1367 | } |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1368 | } |
| 1369 | } |
| 1370 | |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1371 | // find a compatible input profile (not necessarily identical in parameters) |
| 1372 | sp<IOProfile> profile; |
| 1373 | // samplingRate and flags may be updated by getInputProfile |
| 1374 | uint32_t profileSamplingRate = samplingRate; |
| 1375 | audio_format_t profileFormat = format; |
| 1376 | audio_channel_mask_t profileChannelMask = channelMask; |
| 1377 | audio_input_flags_t profileFlags = flags; |
| 1378 | for (;;) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1379 | profile = getInputProfile(device, address, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1380 | profileSamplingRate, profileFormat, profileChannelMask, |
| 1381 | profileFlags); |
| 1382 | if (profile != 0) { |
| 1383 | break; // success |
| 1384 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { |
| 1385 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry |
| 1386 | } else { // fail |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1387 | ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u," |
| 1388 | "format %#x, channelMask 0x%X, flags %#x", |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1389 | device, samplingRate, format, channelMask, flags); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1390 | return BAD_VALUE; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1391 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1394 | if (profile->getModuleHandle() == 0) { |
| 1395 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1396 | return NO_INIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1400 | config.sample_rate = profileSamplingRate; |
| 1401 | config.channel_mask = profileChannelMask; |
| 1402 | config.format = profileFormat; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1403 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1404 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1405 | input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1406 | &config, |
| 1407 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1408 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1409 | halInputSource, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1410 | profileFlags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1411 | |
| 1412 | // only accept input with the exact requested set of parameters |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1413 | if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE || |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1414 | (profileSamplingRate != config.sample_rate) || |
| 1415 | (profileFormat != config.format) || |
| 1416 | (profileChannelMask != config.channel_mask)) { |
| 1417 | ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d," |
| 1418 | " channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1419 | samplingRate, format, channelMask); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1420 | if (*input != AUDIO_IO_HANDLE_NONE) { |
| 1421 | mpClientInterface->closeInput(*input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1422 | } |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1423 | return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1424 | } |
| 1425 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1426 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1427 | inputDesc->mInputSource = inputSource; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1428 | inputDesc->mRefCount = 0; |
| 1429 | inputDesc->mOpenRefCount = 1; |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1430 | inputDesc->mSamplingRate = profileSamplingRate; |
| 1431 | inputDesc->mFormat = profileFormat; |
| 1432 | inputDesc->mChannelMask = profileChannelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1433 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1434 | inputDesc->mSessions.add(session); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1435 | inputDesc->mIsSoundTrigger = isSoundTrigger; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1436 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1437 | |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1438 | ALOGV("getInputForAttr() returns input type = %d", *inputType); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1439 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1440 | addInput(*input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1441 | mpClientInterface->onAudioPortListUpdate(); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1442 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1443 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1446 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
| 1447 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1448 | { |
| 1449 | ALOGV("startInput() input %d", input); |
| 1450 | ssize_t index = mInputs.indexOfKey(input); |
| 1451 | if (index < 0) { |
| 1452 | ALOGW("startInput() unknown input %d", input); |
| 1453 | return BAD_VALUE; |
| 1454 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1455 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1456 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1457 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1458 | if (index < 0) { |
| 1459 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1460 | return BAD_VALUE; |
| 1461 | } |
| 1462 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1463 | // virtual input devices are compatible with other input devices |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1464 | if (!is_virtual_input_device(inputDesc->mDevice)) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1465 | |
| 1466 | // for a non-virtual input device, check if there is another (non-virtual) active input |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1467 | audio_io_handle_t activeInput = mInputs.getActiveInput(); |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1468 | if (activeInput != 0 && activeInput != input) { |
| 1469 | |
| 1470 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1471 | // otherwise the active input continues and the new input cannot be started. |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1472 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1473 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1474 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1475 | stopInput(activeInput, activeDesc->mSessions.itemAt(0)); |
| 1476 | releaseInput(activeInput, activeDesc->mSessions.itemAt(0)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1477 | } else { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1478 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1479 | return INVALID_OPERATION; |
| 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1484 | // Routing? |
| 1485 | mInputRoutes.incRouteActivity(session); |
| 1486 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1487 | if (inputDesc->mRefCount == 0 || mInputRoutes.hasRouteChanged(session)) { |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1488 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1489 | if ((inputDesc->mPolicyMix != NULL) |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1490 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1491 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId, |
| 1492 | MIX_STATE_MIXING); |
| 1493 | } |
| 1494 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1495 | if (mInputs.activeInputsCount() == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1496 | SoundTrigger::setCaptureState(true); |
| 1497 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1498 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1499 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1500 | // automatically enable the remote submix output when input is started if not |
| 1501 | // used by a policy mix of type MIX_TYPE_RECORDERS |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1502 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1503 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1504 | String8 address = String8(""); |
| 1505 | if (inputDesc->mPolicyMix == NULL) { |
| 1506 | address = String8("0"); |
| 1507 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1508 | address = inputDesc->mPolicyMix->mRegistrationId; |
| 1509 | } |
| 1510 | if (address != "") { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1511 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1512 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1513 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1514 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1515 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1518 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); |
| 1519 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1520 | inputDesc->mRefCount++; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1521 | return NO_ERROR; |
| 1522 | } |
| 1523 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1524 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1525 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1526 | { |
| 1527 | ALOGV("stopInput() input %d", input); |
| 1528 | ssize_t index = mInputs.indexOfKey(input); |
| 1529 | if (index < 0) { |
| 1530 | ALOGW("stopInput() unknown input %d", input); |
| 1531 | return BAD_VALUE; |
| 1532 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1533 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1534 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1535 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1536 | if (index < 0) { |
| 1537 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1538 | return BAD_VALUE; |
| 1539 | } |
| 1540 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1541 | if (inputDesc->mRefCount == 0) { |
| 1542 | ALOGW("stopInput() input %d already stopped", input); |
| 1543 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | inputDesc->mRefCount--; |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1547 | |
| 1548 | // Routing? |
| 1549 | mInputRoutes.decRouteActivity(session); |
| 1550 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1551 | if (inputDesc->mRefCount == 0) { |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1552 | // if input maps to a dynamic policy with an activity listener, notify of state change |
| 1553 | if ((inputDesc->mPolicyMix != NULL) |
Jean-Michel Trivi | f613d42 | 2015-04-23 18:41:29 -0700 | [diff] [blame] | 1554 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { |
Jean-Michel Trivi | 2b0c1fc | 2015-04-15 17:29:28 -0700 | [diff] [blame] | 1555 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mRegistrationId, |
| 1556 | MIX_STATE_IDLE); |
| 1557 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1558 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1559 | // automatically disable the remote submix output when input is stopped if not |
| 1560 | // used by a policy mix of type MIX_TYPE_RECORDERS |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1561 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1562 | String8 address = String8(""); |
| 1563 | if (inputDesc->mPolicyMix == NULL) { |
| 1564 | address = String8("0"); |
| 1565 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1566 | address = inputDesc->mPolicyMix->mRegistrationId; |
| 1567 | } |
| 1568 | if (address != "") { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1569 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1570 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1571 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1572 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1575 | resetInputDevice(input); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1576 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1577 | if (mInputs.activeInputsCount() == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1578 | SoundTrigger::setCaptureState(false); |
| 1579 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1580 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1581 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1584 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1585 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1586 | { |
| 1587 | ALOGV("releaseInput() %d", input); |
| 1588 | ssize_t index = mInputs.indexOfKey(input); |
| 1589 | if (index < 0) { |
| 1590 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1591 | return; |
| 1592 | } |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1593 | |
| 1594 | // Routing |
| 1595 | mInputRoutes.removeRoute(session); |
| 1596 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1597 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1598 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1599 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1600 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1601 | if (index < 0) { |
| 1602 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1603 | return; |
| 1604 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1605 | inputDesc->mSessions.remove(session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1606 | if (inputDesc->mOpenRefCount == 0) { |
| 1607 | ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount); |
| 1608 | return; |
| 1609 | } |
| 1610 | inputDesc->mOpenRefCount--; |
| 1611 | if (inputDesc->mOpenRefCount > 0) { |
| 1612 | ALOGV("releaseInput() exit > 0"); |
| 1613 | return; |
| 1614 | } |
| 1615 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1616 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1617 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1618 | ALOGV("releaseInput() exit"); |
| 1619 | } |
| 1620 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1621 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1622 | bool patchRemoved = false; |
| 1623 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1624 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1625 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
| 1626 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 1627 | if (patch_index >= 0) { |
| 1628 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1629 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1630 | mAudioPatches.removeItemsAt(patch_index); |
| 1631 | patchRemoved = true; |
| 1632 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1633 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1634 | } |
| 1635 | mInputs.clear(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1636 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1637 | |
| 1638 | if (patchRemoved) { |
| 1639 | mpClientInterface->onAudioPatchListUpdate(); |
| 1640 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1641 | } |
| 1642 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1643 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1644 | int indexMin, |
| 1645 | int indexMax) |
| 1646 | { |
| 1647 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1648 | mEngine->initStreamVolume(stream, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1649 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1650 | if (stream == AUDIO_STREAM_MUSIC) { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1651 | mEngine->initStreamVolume(AUDIO_STREAM_ACCESSIBILITY, indexMin, indexMax); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1652 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1653 | } |
| 1654 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1655 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1656 | int index, |
| 1657 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1658 | { |
| 1659 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1660 | if ((index < mStreams.valueFor(stream).getVolumeIndexMin()) || |
| 1661 | (index > mStreams.valueFor(stream).getVolumeIndexMax())) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1662 | return BAD_VALUE; |
| 1663 | } |
| 1664 | if (!audio_is_output_device(device)) { |
| 1665 | return BAD_VALUE; |
| 1666 | } |
| 1667 | |
| 1668 | // Force max volume if stream cannot be muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1669 | if (!mStreams.canBeMuted(stream)) index = mStreams.valueFor(stream).getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1670 | |
| 1671 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1672 | stream, device, index); |
| 1673 | |
| 1674 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1675 | // clear all device specific values |
| 1676 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1677 | mStreams.clearCurrentVolumeIndex(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1678 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1679 | mStreams.addCurrentVolumeIndex(stream, device, index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1680 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1681 | // update volume on all outputs whose current device is also selected by the same |
| 1682 | // strategy as the device specified by the caller |
| 1683 | audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1684 | |
| 1685 | |
| 1686 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1687 | audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE; |
| 1688 | if (stream == AUDIO_STREAM_MUSIC) { |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1689 | mStreams.addCurrentVolumeIndex(AUDIO_STREAM_ACCESSIBILITY, device, index); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1690 | accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/); |
| 1691 | } |
| 1692 | if ((device != AUDIO_DEVICE_OUT_DEFAULT) && |
| 1693 | (device & (strategyDevice | accessibilityDevice)) == 0) { |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1694 | return NO_ERROR; |
| 1695 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1696 | status_t status = NO_ERROR; |
| 1697 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1698 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 1699 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1700 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1701 | status_t volStatus = checkAndSetVolume(stream, index, desc, curDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1702 | if (volStatus != NO_ERROR) { |
| 1703 | status = volStatus; |
| 1704 | } |
| 1705 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1706 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) { |
| 1707 | status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1708 | index, desc, curDevice); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1709 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1710 | } |
| 1711 | return status; |
| 1712 | } |
| 1713 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1714 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1715 | int *index, |
| 1716 | audio_devices_t device) |
| 1717 | { |
| 1718 | if (index == NULL) { |
| 1719 | return BAD_VALUE; |
| 1720 | } |
| 1721 | if (!audio_is_output_device(device)) { |
| 1722 | return BAD_VALUE; |
| 1723 | } |
| 1724 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1725 | // the strategy the stream belongs to. |
| 1726 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1727 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1728 | } |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1729 | device = Volume::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1730 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1731 | *index = mStreams.valueFor(stream).getVolumeIndex(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1732 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1733 | return NO_ERROR; |
| 1734 | } |
| 1735 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1736 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1737 | const SortedVector<audio_io_handle_t>& outputs) |
| 1738 | { |
| 1739 | // select one output among several suitable for global effects. |
| 1740 | // The priority is as follows: |
| 1741 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1742 | // AudioFlinger will invalidate the track and the offloaded output |
| 1743 | // will be closed causing the effect to be moved to a PCM output. |
| 1744 | // 2: A deep buffer output |
| 1745 | // 3: the first output in the list |
| 1746 | |
| 1747 | if (outputs.size() == 0) { |
| 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | audio_io_handle_t outputOffloaded = 0; |
| 1752 | audio_io_handle_t outputDeepBuffer = 0; |
| 1753 | |
| 1754 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1755 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1756 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1757 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1758 | outputOffloaded = outputs[i]; |
| 1759 | } |
| 1760 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1761 | outputDeepBuffer = outputs[i]; |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1766 | outputOffloaded, outputDeepBuffer); |
| 1767 | if (outputOffloaded != 0) { |
| 1768 | return outputOffloaded; |
| 1769 | } |
| 1770 | if (outputDeepBuffer != 0) { |
| 1771 | return outputDeepBuffer; |
| 1772 | } |
| 1773 | |
| 1774 | return outputs[0]; |
| 1775 | } |
| 1776 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1777 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1778 | { |
| 1779 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1780 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1781 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1782 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1783 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1784 | |
| 1785 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1786 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1787 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1788 | |
| 1789 | return output; |
| 1790 | } |
| 1791 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1792 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1793 | audio_io_handle_t io, |
| 1794 | uint32_t strategy, |
| 1795 | int session, |
| 1796 | int id) |
| 1797 | { |
| 1798 | ssize_t index = mOutputs.indexOfKey(io); |
| 1799 | if (index < 0) { |
| 1800 | index = mInputs.indexOfKey(io); |
| 1801 | if (index < 0) { |
| 1802 | ALOGW("registerEffect() unknown io %d", io); |
| 1803 | return INVALID_OPERATION; |
| 1804 | } |
| 1805 | } |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 1806 | return mEffects.registerEffect(desc, io, strategy, session, id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1809 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
| 1810 | { |
| 1811 | return mOutputs.isStreamActive(stream, inPastMs); |
| 1812 | } |
| 1813 | |
| 1814 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const |
| 1815 | { |
| 1816 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); |
| 1817 | } |
| 1818 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1819 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1820 | { |
| 1821 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1822 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 1823 | if (inputDescriptor->mRefCount == 0) { |
| 1824 | continue; |
| 1825 | } |
| 1826 | if (inputDescriptor->mInputSource == (int)source) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1827 | return true; |
| 1828 | } |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 1829 | // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it |
| 1830 | // corresponds to an active capture triggered by a hardware hotword recognition |
| 1831 | if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) && |
| 1832 | (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) { |
| 1833 | // FIXME: we should not assume that the first session is the active one and keep |
| 1834 | // activity count per session. Same in startInput(). |
| 1835 | ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0)); |
| 1836 | if (index >= 0) { |
| 1837 | return true; |
| 1838 | } |
| 1839 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1840 | } |
| 1841 | return false; |
| 1842 | } |
| 1843 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1844 | // Register a list of custom mixes with their attributes and format. |
| 1845 | // When a mix is registered, corresponding input and output profiles are |
| 1846 | // added to the remote submix hw module. The profile contains only the |
| 1847 | // parameters (sampling rate, format...) specified by the mix. |
| 1848 | // The corresponding input remote submix device is also connected. |
| 1849 | // |
| 1850 | // When a remote submix device is connected, the address is checked to select the |
| 1851 | // appropriate profile and the corresponding input or output stream is opened. |
| 1852 | // |
| 1853 | // When capture starts, getInputForAttr() will: |
| 1854 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 1855 | // - 2 if none found, getDeviceForInputSource() will: |
| 1856 | // - 2.1 look for a mix matching the attributes source |
| 1857 | // - 2.2 if none found, default to device selection by policy rules |
| 1858 | // At this time, the corresponding output remote submix device is also connected |
| 1859 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 1860 | // after AudioTracks are invalidated |
| 1861 | // |
| 1862 | // When playback starts, getOutputForAttr() will: |
| 1863 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 1864 | // - 2 if none found, look for a mix matching the attributes usage |
| 1865 | // - 3 if none found, default to device and output selection by policy rules. |
| 1866 | |
| 1867 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 1868 | { |
| 1869 | sp<HwModule> module; |
| 1870 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 1871 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && |
| 1872 | mHwModules[i]->mHandle != 0) { |
| 1873 | module = mHwModules[i]; |
| 1874 | break; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | if (module == 0) { |
| 1879 | return INVALID_OPERATION; |
| 1880 | } |
| 1881 | |
| 1882 | ALOGV("registerPolicyMixes() num mixes %d", mixes.size()); |
| 1883 | |
| 1884 | for (size_t i = 0; i < mixes.size(); i++) { |
| 1885 | String8 address = mixes[i].mRegistrationId; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1886 | |
| 1887 | if (mPolicyMixes.registerMix(address, mixes[i]) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1888 | continue; |
| 1889 | } |
| 1890 | audio_config_t outputConfig = mixes[i].mFormat; |
| 1891 | audio_config_t inputConfig = mixes[i].mFormat; |
| 1892 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 1893 | // stereo and let audio flinger do the channel conversion if needed. |
| 1894 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1895 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 1896 | module->addOutputProfile(address, &outputConfig, |
| 1897 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 1898 | module->addInputProfile(address, &inputConfig, |
| 1899 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1900 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1901 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1902 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1903 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1904 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1905 | } else { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1906 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1907 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1908 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1909 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1910 | } |
| 1911 | return NO_ERROR; |
| 1912 | } |
| 1913 | |
| 1914 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 1915 | { |
| 1916 | sp<HwModule> module; |
| 1917 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 1918 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && |
| 1919 | mHwModules[i]->mHandle != 0) { |
| 1920 | module = mHwModules[i]; |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | if (module == 0) { |
| 1926 | return INVALID_OPERATION; |
| 1927 | } |
| 1928 | |
| 1929 | ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size()); |
| 1930 | |
| 1931 | for (size_t i = 0; i < mixes.size(); i++) { |
| 1932 | String8 address = mixes[i].mRegistrationId; |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1933 | |
| 1934 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1935 | continue; |
| 1936 | } |
| 1937 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1938 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 1939 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) |
| 1940 | { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1941 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1942 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1943 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1944 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1945 | |
| 1946 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 1947 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) |
| 1948 | { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1949 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1950 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1951 | address.string(), "remote-submix"); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1952 | } |
| 1953 | module->removeOutputProfile(address); |
| 1954 | module->removeInputProfile(address); |
| 1955 | } |
| 1956 | return NO_ERROR; |
| 1957 | } |
| 1958 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1959 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1960 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1961 | { |
| 1962 | const size_t SIZE = 256; |
| 1963 | char buffer[SIZE]; |
| 1964 | String8 result; |
| 1965 | |
| 1966 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 1967 | result.append(buffer); |
| 1968 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1969 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1970 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1971 | snprintf(buffer, SIZE, " Phone state: %d\n", mEngine->getPhoneState()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1972 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1973 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1974 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1975 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1976 | 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] | 1977 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1978 | 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] | 1979 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1980 | 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] | 1981 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1982 | 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] | 1983 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1984 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1985 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 1986 | result.append(buffer); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 1987 | write(fd, result.string(), result.size()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1988 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1989 | mAvailableOutputDevices.dump(fd, String8("output")); |
| 1990 | mAvailableInputDevices.dump(fd, String8("input")); |
| 1991 | mHwModules.dump(fd); |
| 1992 | mOutputs.dump(fd); |
| 1993 | mInputs.dump(fd); |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 1994 | mStreams.dump(fd); |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 1995 | mEffects.dump(fd); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1996 | mAudioPatches.dump(fd); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1997 | |
| 1998 | return NO_ERROR; |
| 1999 | } |
| 2000 | |
| 2001 | // This function checks for the parameters which can be offloaded. |
| 2002 | // This can be enhanced depending on the capability of the DSP and policy |
| 2003 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2004 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2005 | { |
| 2006 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2007 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2008 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2009 | offloadInfo.format, |
| 2010 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2011 | offloadInfo.has_video); |
| 2012 | |
| 2013 | // Check if offload has been disabled |
| 2014 | char propValue[PROPERTY_VALUE_MAX]; |
| 2015 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2016 | if (atoi(propValue) != 0) { |
| 2017 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2018 | return false; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | // Check if stream type is music, then only allow offload as of now. |
| 2023 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2024 | { |
| 2025 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2026 | return false; |
| 2027 | } |
| 2028 | |
| 2029 | //TODO: enable audio offloading with video when ready |
| 2030 | if (offloadInfo.has_video) |
| 2031 | { |
| 2032 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2033 | return false; |
| 2034 | } |
| 2035 | |
| 2036 | //If duration is less than minimum value defined in property, return false |
| 2037 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2038 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2039 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2040 | return false; |
| 2041 | } |
| 2042 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2043 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2044 | return false; |
| 2045 | } |
| 2046 | |
| 2047 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2048 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2049 | // detects there is an active non offloadable effect. |
| 2050 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2051 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2052 | // in the background. |
François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2053 | if (mEffects.isNonOffloadableEffectEnabled()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2054 | return false; |
| 2055 | } |
| 2056 | |
| 2057 | // See if there is a profile to support this. |
| 2058 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2059 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2060 | offloadInfo.sample_rate, |
| 2061 | offloadInfo.format, |
| 2062 | offloadInfo.channel_mask, |
| 2063 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2064 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2065 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2068 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2069 | audio_port_type_t type, |
| 2070 | unsigned int *num_ports, |
| 2071 | struct audio_port *ports, |
| 2072 | unsigned int *generation) |
| 2073 | { |
| 2074 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2075 | generation == NULL) { |
| 2076 | return BAD_VALUE; |
| 2077 | } |
| 2078 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2079 | if (ports == NULL) { |
| 2080 | *num_ports = 0; |
| 2081 | } |
| 2082 | |
| 2083 | size_t portsWritten = 0; |
| 2084 | size_t portsMax = *num_ports; |
| 2085 | *num_ports = 0; |
| 2086 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2087 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2088 | for (size_t i = 0; |
| 2089 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2090 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2091 | } |
| 2092 | *num_ports += mAvailableOutputDevices.size(); |
| 2093 | } |
| 2094 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2095 | for (size_t i = 0; |
| 2096 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2097 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2098 | } |
| 2099 | *num_ports += mAvailableInputDevices.size(); |
| 2100 | } |
| 2101 | } |
| 2102 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2103 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2104 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2105 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2106 | } |
| 2107 | *num_ports += mInputs.size(); |
| 2108 | } |
| 2109 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2110 | size_t numOutputs = 0; |
| 2111 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2112 | if (!mOutputs[i]->isDuplicated()) { |
| 2113 | numOutputs++; |
| 2114 | if (portsWritten < portsMax) { |
| 2115 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2116 | } |
| 2117 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2118 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2119 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2120 | } |
| 2121 | } |
| 2122 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2123 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2124 | return NO_ERROR; |
| 2125 | } |
| 2126 | |
| 2127 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2128 | { |
| 2129 | return NO_ERROR; |
| 2130 | } |
| 2131 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2132 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2133 | audio_patch_handle_t *handle, |
| 2134 | uid_t uid) |
| 2135 | { |
| 2136 | ALOGV("createAudioPatch()"); |
| 2137 | |
| 2138 | if (handle == NULL || patch == NULL) { |
| 2139 | return BAD_VALUE; |
| 2140 | } |
| 2141 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2142 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2143 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2144 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2145 | return BAD_VALUE; |
| 2146 | } |
| 2147 | // only one source per audio patch supported for now |
| 2148 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2149 | return INVALID_OPERATION; |
| 2150 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2151 | |
| 2152 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2153 | return INVALID_OPERATION; |
| 2154 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2155 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2156 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2157 | return INVALID_OPERATION; |
| 2158 | } |
| 2159 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2160 | |
| 2161 | sp<AudioPatch> patchDesc; |
| 2162 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2163 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2164 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2165 | patch->sources[0].role, |
| 2166 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2167 | #if LOG_NDEBUG == 0 |
| 2168 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2169 | ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id, |
| 2170 | patch->sinks[i].role, |
| 2171 | patch->sinks[i].type); |
| 2172 | } |
| 2173 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2174 | |
| 2175 | if (index >= 0) { |
| 2176 | patchDesc = mAudioPatches.valueAt(index); |
| 2177 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2178 | mUidCached, patchDesc->mUid, uid); |
| 2179 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2180 | return INVALID_OPERATION; |
| 2181 | } |
| 2182 | } else { |
| 2183 | *handle = 0; |
| 2184 | } |
| 2185 | |
| 2186 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2187 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2188 | if (outputDesc == NULL) { |
| 2189 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2190 | return BAD_VALUE; |
| 2191 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2192 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2193 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2194 | if (patchDesc != 0) { |
| 2195 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2196 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2197 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2198 | return BAD_VALUE; |
| 2199 | } |
| 2200 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2201 | DeviceVector devices; |
| 2202 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2203 | // Only support mix to devices connection |
| 2204 | // TODO add support for mix to mix connection |
| 2205 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2206 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2207 | return INVALID_OPERATION; |
| 2208 | } |
| 2209 | sp<DeviceDescriptor> devDesc = |
| 2210 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2211 | if (devDesc == 0) { |
| 2212 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2213 | return BAD_VALUE; |
| 2214 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2215 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2216 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2217 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2218 | patch->sources[0].sample_rate, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2219 | NULL, // updatedSamplingRate |
| 2220 | patch->sources[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2221 | NULL, // updatedFormat |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2222 | patch->sources[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2223 | NULL, // updatedChannelMask |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2224 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2225 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2226 | devDesc->type()); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2227 | return INVALID_OPERATION; |
| 2228 | } |
| 2229 | devices.add(devDesc); |
| 2230 | } |
| 2231 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2232 | return INVALID_OPERATION; |
| 2233 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2234 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2235 | // TODO: reconfigure output format and channels here |
| 2236 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2237 | devices.types(), outputDesc->mIoHandle); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2238 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2239 | index = mAudioPatches.indexOfKey(*handle); |
| 2240 | if (index >= 0) { |
| 2241 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2242 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2243 | } |
| 2244 | patchDesc = mAudioPatches.valueAt(index); |
| 2245 | patchDesc->mUid = uid; |
| 2246 | ALOGV("createAudioPatch() success"); |
| 2247 | } else { |
| 2248 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2249 | return INVALID_OPERATION; |
| 2250 | } |
| 2251 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2252 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2253 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2254 | // only one sink supported when connecting an input device to a mix |
| 2255 | if (patch->num_sinks > 1) { |
| 2256 | return INVALID_OPERATION; |
| 2257 | } |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2258 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2259 | if (inputDesc == NULL) { |
| 2260 | return BAD_VALUE; |
| 2261 | } |
| 2262 | if (patchDesc != 0) { |
| 2263 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2264 | return BAD_VALUE; |
| 2265 | } |
| 2266 | } |
| 2267 | sp<DeviceDescriptor> devDesc = |
| 2268 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2269 | if (devDesc == 0) { |
| 2270 | return BAD_VALUE; |
| 2271 | } |
| 2272 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2273 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2274 | devDesc->mAddress, |
| 2275 | patch->sinks[0].sample_rate, |
| 2276 | NULL, /*updatedSampleRate*/ |
| 2277 | patch->sinks[0].format, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2278 | NULL, /*updatedFormat*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2279 | patch->sinks[0].channel_mask, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2280 | NULL, /*updatedChannelMask*/ |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2281 | // FIXME for the parameter type, |
| 2282 | // and the NONE |
| 2283 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2284 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2285 | return INVALID_OPERATION; |
| 2286 | } |
| 2287 | // TODO: reconfigure output format and channels here |
| 2288 | ALOGV("createAudioPatch() setting device %08x on output %d", |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2289 | devDesc->type(), inputDesc->mIoHandle); |
| 2290 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2291 | index = mAudioPatches.indexOfKey(*handle); |
| 2292 | if (index >= 0) { |
| 2293 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2294 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2295 | } |
| 2296 | patchDesc = mAudioPatches.valueAt(index); |
| 2297 | patchDesc->mUid = uid; |
| 2298 | ALOGV("createAudioPatch() success"); |
| 2299 | } else { |
| 2300 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2301 | return INVALID_OPERATION; |
| 2302 | } |
| 2303 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2304 | // device to device connection |
| 2305 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2306 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2307 | return BAD_VALUE; |
| 2308 | } |
| 2309 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2310 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2311 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2312 | if (srcDeviceDesc == 0) { |
| 2313 | return BAD_VALUE; |
| 2314 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2315 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2316 | //update source and sink with our own data as the data passed in the patch may |
| 2317 | // be incomplete. |
| 2318 | struct audio_patch newPatch = *patch; |
| 2319 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2320 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2321 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2322 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2323 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2324 | return INVALID_OPERATION; |
| 2325 | } |
| 2326 | |
| 2327 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2328 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2329 | if (sinkDeviceDesc == 0) { |
| 2330 | return BAD_VALUE; |
| 2331 | } |
| 2332 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2333 | |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2334 | // create a software bridge in PatchPanel if: |
| 2335 | // - source and sink devices are on differnt HW modules OR |
| 2336 | // - audio HAL version is < 3.0 |
| 2337 | if ((srcDeviceDesc->getModuleHandle() != sinkDeviceDesc->getModuleHandle()) || |
| 2338 | (srcDeviceDesc->mModule->mHalVersion < AUDIO_DEVICE_API_VERSION_3_0)) { |
| 2339 | // support only one sink device for now to simplify output selection logic |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2340 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2341 | return INVALID_OPERATION; |
| 2342 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2343 | SortedVector<audio_io_handle_t> outputs = |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2344 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2345 | // if the sink device is reachable via an opened output stream, request to go via |
| 2346 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2347 | audio_io_handle_t output = selectOutput(outputs, |
| 2348 | AUDIO_OUTPUT_FLAG_NONE, |
| 2349 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2350 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2351 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2352 | if (outputDesc->isDuplicated()) { |
| 2353 | return INVALID_OPERATION; |
| 2354 | } |
| 2355 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2356 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2357 | newPatch.num_sources = 2; |
| 2358 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2359 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2360 | } |
| 2361 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2362 | |
| 2363 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2364 | if (index >= 0) { |
| 2365 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2366 | } |
| 2367 | |
| 2368 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2369 | &afPatchHandle, |
| 2370 | 0); |
| 2371 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2372 | status, afPatchHandle); |
| 2373 | if (status == NO_ERROR) { |
| 2374 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2375 | patchDesc = new AudioPatch(&newPatch, uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2376 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2377 | } else { |
| 2378 | patchDesc->mPatch = newPatch; |
| 2379 | } |
| 2380 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2381 | *handle = patchDesc->mHandle; |
| 2382 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2383 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2384 | } else { |
| 2385 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2386 | status); |
| 2387 | return INVALID_OPERATION; |
| 2388 | } |
| 2389 | } else { |
| 2390 | return BAD_VALUE; |
| 2391 | } |
| 2392 | } else { |
| 2393 | return BAD_VALUE; |
| 2394 | } |
| 2395 | return NO_ERROR; |
| 2396 | } |
| 2397 | |
| 2398 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2399 | uid_t uid) |
| 2400 | { |
| 2401 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2402 | |
| 2403 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2404 | |
| 2405 | if (index < 0) { |
| 2406 | return BAD_VALUE; |
| 2407 | } |
| 2408 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2409 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2410 | mUidCached, patchDesc->mUid, uid); |
| 2411 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2412 | return INVALID_OPERATION; |
| 2413 | } |
| 2414 | |
| 2415 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2416 | patchDesc->mUid = mUidCached; |
| 2417 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2418 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2419 | if (outputDesc == NULL) { |
| 2420 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2421 | return BAD_VALUE; |
| 2422 | } |
| 2423 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2424 | setOutputDevice(outputDesc, |
| 2425 | getNewOutputDevice(outputDesc, true /*fromCache*/), |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2426 | true, |
| 2427 | 0, |
| 2428 | NULL); |
| 2429 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2430 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2431 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2432 | if (inputDesc == NULL) { |
| 2433 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2434 | return BAD_VALUE; |
| 2435 | } |
| 2436 | setInputDevice(inputDesc->mIoHandle, |
| 2437 | getNewInputDevice(inputDesc->mIoHandle), |
| 2438 | true, |
| 2439 | NULL); |
| 2440 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2441 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2442 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2443 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2444 | status, patchDesc->mAfPatchHandle); |
| 2445 | removeAudioPatch(patchDesc->mHandle); |
| 2446 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2447 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2448 | } else { |
| 2449 | return BAD_VALUE; |
| 2450 | } |
| 2451 | } else { |
| 2452 | return BAD_VALUE; |
| 2453 | } |
| 2454 | return NO_ERROR; |
| 2455 | } |
| 2456 | |
| 2457 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2458 | struct audio_patch *patches, |
| 2459 | unsigned int *generation) |
| 2460 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2461 | if (generation == NULL) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2462 | return BAD_VALUE; |
| 2463 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2464 | *generation = curAudioPortGeneration(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2465 | return mAudioPatches.listAudioPatches(num_patches, patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2468 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2469 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2470 | ALOGV("setAudioPortConfig()"); |
| 2471 | |
| 2472 | if (config == NULL) { |
| 2473 | return BAD_VALUE; |
| 2474 | } |
| 2475 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2476 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2477 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2478 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2479 | } |
| 2480 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2481 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2482 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2483 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2484 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2485 | if (outputDesc == NULL) { |
| 2486 | return BAD_VALUE; |
| 2487 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2488 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2489 | "setAudioPortConfig() called on duplicated output %d", |
| 2490 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2491 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2492 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2493 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2494 | if (inputDesc == NULL) { |
| 2495 | return BAD_VALUE; |
| 2496 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2497 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2498 | } else { |
| 2499 | return BAD_VALUE; |
| 2500 | } |
| 2501 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2502 | sp<DeviceDescriptor> deviceDesc; |
| 2503 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2504 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2505 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2506 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2507 | } else { |
| 2508 | return BAD_VALUE; |
| 2509 | } |
| 2510 | if (deviceDesc == NULL) { |
| 2511 | return BAD_VALUE; |
| 2512 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2513 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2514 | } else { |
| 2515 | return BAD_VALUE; |
| 2516 | } |
| 2517 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2518 | struct audio_port_config backupConfig; |
| 2519 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2520 | if (status == NO_ERROR) { |
| 2521 | struct audio_port_config newConfig; |
| 2522 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2523 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2524 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2525 | if (status != NO_ERROR) { |
| 2526 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2527 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2528 | |
| 2529 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2530 | } |
| 2531 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2532 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) |
| 2533 | { |
| 2534 | clearAudioPatches(uid); |
| 2535 | clearSessionRoutes(uid); |
| 2536 | } |
| 2537 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2538 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2539 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2540 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2541 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2542 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2543 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2544 | } |
| 2545 | } |
| 2546 | } |
| 2547 | |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 2548 | |
| 2549 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, |
| 2550 | audio_io_handle_t ouptutToSkip) |
| 2551 | { |
| 2552 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 2553 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 2554 | for (size_t j = 0; j < mOutputs.size(); j++) { |
| 2555 | if (mOutputs.keyAt(j) == ouptutToSkip) { |
| 2556 | continue; |
| 2557 | } |
| 2558 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); |
| 2559 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { |
| 2560 | continue; |
| 2561 | } |
| 2562 | // If the default device for this strategy is on another output mix, |
| 2563 | // invalidate all tracks in this strategy to force re connection. |
| 2564 | // Otherwise select new device on the output mix. |
| 2565 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { |
| 2566 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
| 2567 | if (stream == AUDIO_STREAM_PATCH) { |
| 2568 | continue; |
| 2569 | } |
| 2570 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 2571 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); |
| 2572 | } |
| 2573 | } |
| 2574 | } else { |
| 2575 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); |
| 2576 | setOutputDevice(outputDesc, newDevice, false); |
| 2577 | } |
| 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) |
| 2582 | { |
| 2583 | // remove output routes associated with this uid |
| 2584 | SortedVector<routing_strategy> affectedStrategies; |
| 2585 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--) { |
| 2586 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); |
| 2587 | if (route->mUid == uid) { |
| 2588 | mOutputRoutes.removeItemsAt(i); |
| 2589 | if (route->mDeviceDescriptor != 0) { |
| 2590 | affectedStrategies.add(getStrategy(route->mStreamType)); |
| 2591 | } |
| 2592 | } |
| 2593 | } |
| 2594 | // reroute outputs if necessary |
| 2595 | for (size_t i = 0; i < affectedStrategies.size(); i++) { |
| 2596 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); |
| 2597 | } |
| 2598 | |
| 2599 | // remove input routes associated with this uid |
| 2600 | SortedVector<audio_source_t> affectedSources; |
| 2601 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--) { |
| 2602 | sp<SessionRoute> route = mInputRoutes.valueAt(i); |
| 2603 | if (route->mUid == uid) { |
| 2604 | mInputRoutes.removeItemsAt(i); |
| 2605 | if (route->mDeviceDescriptor != 0) { |
| 2606 | affectedSources.add(route->mSource); |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | // reroute inputs if necessary |
| 2611 | SortedVector<audio_io_handle_t> inputsToClose; |
| 2612 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2613 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); |
| 2614 | if (affectedSources.indexOf(inputDesc->mInputSource) >= 0) { |
| 2615 | inputsToClose.add(inputDesc->mIoHandle); |
| 2616 | } |
| 2617 | } |
| 2618 | for (size_t i = 0; i < inputsToClose.size(); i++) { |
| 2619 | closeInput(inputsToClose[i]); |
| 2620 | } |
| 2621 | } |
| 2622 | |
| 2623 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2624 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2625 | audio_io_handle_t *ioHandle, |
| 2626 | audio_devices_t *device) |
| 2627 | { |
| 2628 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); |
| 2629 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2630 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2631 | |
François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 2632 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 2635 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source __unused, |
| 2636 | const audio_attributes_t *attributes __unused, |
| 2637 | audio_io_handle_t *handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2638 | { |
| 2639 | return INVALID_OPERATION; |
| 2640 | } |
| 2641 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 2642 | status_t AudioPolicyManager::stopAudioSource(audio_io_handle_t handle __unused) |
Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 2643 | { |
| 2644 | return INVALID_OPERATION; |
| 2645 | } |
| 2646 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2647 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2648 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2649 | // ---------------------------------------------------------------------------- |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2650 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 2651 | { |
| 2652 | return android_atomic_inc(&mAudioPortGeneration); |
| 2653 | } |
| 2654 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2655 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2656 | : |
| 2657 | #ifdef AUDIO_POLICY_TEST |
| 2658 | Thread(false), |
| 2659 | #endif //AUDIO_POLICY_TEST |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2660 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2661 | mA2dpSuspended(false), |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2662 | mSpeakerDrcEnabled(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2663 | mAudioPortGeneration(1), |
| 2664 | mBeaconMuteRefCount(0), |
| 2665 | mBeaconPlayingRefCount(0), |
| 2666 | mBeaconMuted(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2667 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2668 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); |
| 2669 | if (!engineInstance) { |
| 2670 | ALOGE("%s: Could not get an instance of policy engine", __FUNCTION__); |
| 2671 | return; |
| 2672 | } |
| 2673 | // Retrieve the Policy Manager Interface |
| 2674 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); |
| 2675 | if (mEngine == NULL) { |
| 2676 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); |
| 2677 | return; |
| 2678 | } |
| 2679 | mEngine->setObserver(this); |
| 2680 | status_t status = mEngine->initCheck(); |
| 2681 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); |
| 2682 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2683 | mUidCached = getuid(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2684 | mpClientInterface = clientInterface; |
| 2685 | |
Eric Laurent | 7288ab8 | 2015-05-06 18:44:02 -0700 | [diff] [blame] | 2686 | mDefaultOutputDevice = new DeviceDescriptor(AUDIO_DEVICE_OUT_SPEAKER); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2687 | if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, |
| 2688 | mHwModules, mAvailableInputDevices, mAvailableOutputDevices, |
| 2689 | mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) { |
| 2690 | if (ConfigParsingUtils::loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE, |
| 2691 | mHwModules, mAvailableInputDevices, mAvailableOutputDevices, |
| 2692 | mDefaultOutputDevice, mSpeakerDrcEnabled) != NO_ERROR) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2693 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 2694 | defaultAudioPolicyConfig(); |
| 2695 | } |
| 2696 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2697 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2698 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2699 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) |
| 2700 | mEngine->initializeVolumeCurves(mSpeakerDrcEnabled); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2701 | |
| 2702 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2703 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 2704 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2705 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2706 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); |
| 2707 | if (mHwModules[i]->mHandle == 0) { |
| 2708 | ALOGW("could not open HW module %s", mHwModules[i]->mName); |
| 2709 | continue; |
| 2710 | } |
| 2711 | // open all output streams needed to access attached devices |
| 2712 | // except for direct output streams that are only opened when they are actually |
| 2713 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2714 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2715 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2716 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2717 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2718 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2719 | if (outProfile->mSupportedDevices.isEmpty()) { |
| 2720 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); |
| 2721 | continue; |
| 2722 | } |
| 2723 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2724 | if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
| 2725 | continue; |
| 2726 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2727 | audio_devices_t profileType = outProfile->mSupportedDevices.types(); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2728 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { |
| 2729 | profileType = mDefaultOutputDevice->type(); |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2730 | } else { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2731 | // chose first device present in mSupportedDevices also part of |
| 2732 | // outputDeviceTypes |
| 2733 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2734 | profileType = outProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2735 | if ((profileType & outputDeviceTypes) != 0) { |
| 2736 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2737 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2738 | } |
| 2739 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2740 | if ((profileType & outputDeviceTypes) == 0) { |
| 2741 | continue; |
| 2742 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2743 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, |
| 2744 | mpClientInterface); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2745 | |
| 2746 | outputDesc->mDevice = profileType; |
| 2747 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2748 | config.sample_rate = outputDesc->mSamplingRate; |
| 2749 | config.channel_mask = outputDesc->mChannelMask; |
| 2750 | config.format = outputDesc->mFormat; |
| 2751 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2752 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2753 | &output, |
| 2754 | &config, |
| 2755 | &outputDesc->mDevice, |
| 2756 | String8(""), |
| 2757 | &outputDesc->mLatency, |
| 2758 | outputDesc->mFlags); |
| 2759 | |
| 2760 | if (status != NO_ERROR) { |
| 2761 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 2762 | outputDesc->mDevice, |
| 2763 | mHwModules[i]->mName); |
| 2764 | } else { |
| 2765 | outputDesc->mSamplingRate = config.sample_rate; |
| 2766 | outputDesc->mChannelMask = config.channel_mask; |
| 2767 | outputDesc->mFormat = config.format; |
| 2768 | |
| 2769 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2770 | audio_devices_t type = outProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2771 | ssize_t index = |
| 2772 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); |
| 2773 | // 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] | 2774 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 2775 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | if (mPrimaryOutput == 0 && |
| 2779 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2780 | mPrimaryOutput = outputDesc; |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2781 | } |
| 2782 | addOutput(output, outputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2783 | setOutputDevice(outputDesc, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2784 | outputDesc->mDevice, |
| 2785 | true); |
| 2786 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2787 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2788 | // open input streams needed to access attached devices to validate |
| 2789 | // mAvailableInputDevices list |
| 2790 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 2791 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2792 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2793 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2794 | if (inProfile->mSupportedDevices.isEmpty()) { |
| 2795 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); |
| 2796 | continue; |
| 2797 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2798 | // chose first device present in mSupportedDevices also part of |
| 2799 | // inputDeviceTypes |
| 2800 | audio_devices_t profileType = AUDIO_DEVICE_NONE; |
| 2801 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2802 | profileType = inProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2803 | if (profileType & inputDeviceTypes) { |
| 2804 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2805 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2806 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2807 | if ((profileType & inputDeviceTypes) == 0) { |
| 2808 | continue; |
| 2809 | } |
| 2810 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); |
| 2811 | |
| 2812 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; |
| 2813 | inputDesc->mDevice = profileType; |
| 2814 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2815 | // find the address |
| 2816 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 2817 | // the inputs vector must be of size 1, but we don't want to crash here |
| 2818 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 2819 | : String8(""); |
| 2820 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 2821 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 2822 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2823 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2824 | config.sample_rate = inputDesc->mSamplingRate; |
| 2825 | config.channel_mask = inputDesc->mChannelMask; |
| 2826 | config.format = inputDesc->mFormat; |
| 2827 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 2828 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2829 | &input, |
| 2830 | &config, |
| 2831 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 2832 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2833 | AUDIO_SOURCE_MIC, |
| 2834 | AUDIO_INPUT_FLAG_NONE); |
| 2835 | |
| 2836 | if (status == NO_ERROR) { |
| 2837 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2838 | audio_devices_t type = inProfile->mSupportedDevices[k]->type(); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2839 | ssize_t index = |
| 2840 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); |
| 2841 | // 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] | 2842 | if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) { |
| 2843 | mAvailableInputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2844 | } |
| 2845 | } |
| 2846 | mpClientInterface->closeInput(input); |
| 2847 | } else { |
| 2848 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 2849 | inputDesc->mDevice, |
| 2850 | mHwModules[i]->mName); |
| 2851 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2852 | } |
| 2853 | } |
| 2854 | // make sure all attached devices have been allocated a unique ID |
| 2855 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2856 | if (!mAvailableOutputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2857 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2858 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 2859 | continue; |
| 2860 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2861 | // The device is now validated and can be appended to the available devices of the engine |
| 2862 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], |
| 2863 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2864 | i++; |
| 2865 | } |
| 2866 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2867 | if (!mAvailableInputDevices[i]->isAttached()) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2868 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2869 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 2870 | continue; |
| 2871 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2872 | // The device is now validated and can be appended to the available devices of the engine |
| 2873 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], |
| 2874 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2875 | i++; |
| 2876 | } |
| 2877 | // make sure default device is reachable |
| 2878 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2879 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2880 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2881 | |
| 2882 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 2883 | |
| 2884 | updateDevicesAndOutputs(); |
| 2885 | |
| 2886 | #ifdef AUDIO_POLICY_TEST |
| 2887 | if (mPrimaryOutput != 0) { |
| 2888 | AudioParameter outputCmd = AudioParameter(); |
| 2889 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2890 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2891 | |
| 2892 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 2893 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2894 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 2895 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2896 | mTestLatencyMs = 0; |
| 2897 | mCurOutput = 0; |
| 2898 | mDirectOutput = false; |
| 2899 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 2900 | mTestOutputs[i] = 0; |
| 2901 | } |
| 2902 | |
| 2903 | const size_t SIZE = 256; |
| 2904 | char buffer[SIZE]; |
| 2905 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 2906 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 2907 | } |
| 2908 | #endif //AUDIO_POLICY_TEST |
| 2909 | } |
| 2910 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2911 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2912 | { |
| 2913 | #ifdef AUDIO_POLICY_TEST |
| 2914 | exit(); |
| 2915 | #endif //AUDIO_POLICY_TEST |
| 2916 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2917 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2918 | } |
| 2919 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2920 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2921 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2922 | mAvailableOutputDevices.clear(); |
| 2923 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2924 | mOutputs.clear(); |
| 2925 | mInputs.clear(); |
| 2926 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2929 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2930 | { |
| 2931 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; |
| 2932 | } |
| 2933 | |
| 2934 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2935 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2936 | { |
| 2937 | ALOGV("entering threadLoop()"); |
| 2938 | while (!exitPending()) |
| 2939 | { |
| 2940 | String8 command; |
| 2941 | int valueInt; |
| 2942 | String8 value; |
| 2943 | |
| 2944 | Mutex::Autolock _l(mLock); |
| 2945 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 2946 | |
| 2947 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 2948 | AudioParameter param = AudioParameter(command); |
| 2949 | |
| 2950 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 2951 | valueInt != 0) { |
| 2952 | ALOGV("Test command %s received", command.string()); |
| 2953 | String8 target; |
| 2954 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 2955 | target = "Manager"; |
| 2956 | } |
| 2957 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 2958 | param.remove(String8("test_cmd_policy_output")); |
| 2959 | mCurOutput = valueInt; |
| 2960 | } |
| 2961 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 2962 | param.remove(String8("test_cmd_policy_direct")); |
| 2963 | if (value == "false") { |
| 2964 | mDirectOutput = false; |
| 2965 | } else if (value == "true") { |
| 2966 | mDirectOutput = true; |
| 2967 | } |
| 2968 | } |
| 2969 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 2970 | param.remove(String8("test_cmd_policy_input")); |
| 2971 | mTestInput = valueInt; |
| 2972 | } |
| 2973 | |
| 2974 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 2975 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2976 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2977 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2978 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2979 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2980 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2981 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2982 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2983 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2984 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2985 | if (target == "Manager") { |
| 2986 | mTestFormat = format; |
| 2987 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 2988 | AudioParameter outputParam = AudioParameter(); |
| 2989 | outputParam.addInt(String8("format"), format); |
| 2990 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 2991 | } |
| 2992 | } |
| 2993 | } |
| 2994 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 2995 | param.remove(String8("test_cmd_policy_channels")); |
| 2996 | int channels = 0; |
| 2997 | |
| 2998 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2999 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3000 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3001 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3002 | } |
| 3003 | if (channels != 0) { |
| 3004 | if (target == "Manager") { |
| 3005 | mTestChannels = channels; |
| 3006 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3007 | AudioParameter outputParam = AudioParameter(); |
| 3008 | outputParam.addInt(String8("channels"), channels); |
| 3009 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3014 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3015 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3016 | int samplingRate = valueInt; |
| 3017 | if (target == "Manager") { |
| 3018 | mTestSamplingRate = samplingRate; |
| 3019 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3020 | AudioParameter outputParam = AudioParameter(); |
| 3021 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3022 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3023 | } |
| 3024 | } |
| 3025 | } |
| 3026 | |
| 3027 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3028 | param.remove(String8("test_cmd_policy_reopen")); |
| 3029 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3030 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3031 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3032 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3033 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3034 | removeOutput(mPrimaryOutput->mIoHandle); |
| 3035 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, |
| 3036 | mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3037 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3038 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3039 | config.sample_rate = outputDesc->mSamplingRate; |
| 3040 | config.channel_mask = outputDesc->mChannelMask; |
| 3041 | config.format = outputDesc->mFormat; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3042 | audio_io_handle_t handle; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3043 | status_t status = mpClientInterface->openOutput(moduleHandle, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3044 | &handle, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3045 | &config, |
| 3046 | &outputDesc->mDevice, |
| 3047 | String8(""), |
| 3048 | &outputDesc->mLatency, |
| 3049 | outputDesc->mFlags); |
| 3050 | if (status != NO_ERROR) { |
| 3051 | ALOGE("Failed to reopen hardware output stream, " |
| 3052 | "samplingRate: %d, format %d, channels %d", |
| 3053 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3054 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3055 | outputDesc->mSamplingRate = config.sample_rate; |
| 3056 | outputDesc->mChannelMask = config.channel_mask; |
| 3057 | outputDesc->mFormat = config.format; |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3058 | mPrimaryOutput = outputDesc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3059 | AudioParameter outputCmd = AudioParameter(); |
| 3060 | outputCmd.addInt(String8("set_id"), 0); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3061 | mpClientInterface->setParameters(handle, outputCmd.toString()); |
| 3062 | addOutput(handle, outputDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3063 | } |
| 3064 | } |
| 3065 | |
| 3066 | |
| 3067 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3068 | } |
| 3069 | } |
| 3070 | return false; |
| 3071 | } |
| 3072 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3073 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3074 | { |
| 3075 | { |
| 3076 | AutoMutex _l(mLock); |
| 3077 | requestExit(); |
| 3078 | mWaitWorkCV.signal(); |
| 3079 | } |
| 3080 | requestExitAndWait(); |
| 3081 | } |
| 3082 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3083 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3084 | { |
| 3085 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3086 | if (output == mTestOutputs[i]) return i; |
| 3087 | } |
| 3088 | return 0; |
| 3089 | } |
| 3090 | #endif //AUDIO_POLICY_TEST |
| 3091 | |
| 3092 | // --- |
| 3093 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3094 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<SwAudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3095 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3096 | outputDesc->setIoHandle(output); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3097 | mOutputs.add(output, outputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3098 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3099 | } |
| 3100 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3101 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) |
| 3102 | { |
| 3103 | mOutputs.removeItem(output); |
| 3104 | } |
| 3105 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3106 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3107 | { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3108 | inputDesc->setIoHandle(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3109 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3110 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3111 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3112 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3113 | void AudioPolicyManager::findIoHandlesByAddress(sp<SwAudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3114 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3115 | const String8 address /*in*/, |
| 3116 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3117 | sp<DeviceDescriptor> devDesc = |
| 3118 | desc->mProfile->mSupportedDevices.getDevice(device, address); |
| 3119 | if (devDesc != 0) { |
| 3120 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3121 | desc->mIoHandle, address.string()); |
| 3122 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3123 | } |
| 3124 | } |
| 3125 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3126 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3127 | audio_policy_dev_state_t state, |
| 3128 | SortedVector<audio_io_handle_t>& outputs, |
| 3129 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3130 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3131 | audio_devices_t device = devDesc->type(); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3132 | sp<SwAudioOutputDescriptor> desc; |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3133 | // erase all current sample rates, formats and channel masks |
| 3134 | devDesc->clearCapabilities(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3135 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3136 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3137 | // first list already open outputs that can be routed to this device |
| 3138 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3139 | desc = mOutputs.valueAt(i); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3140 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3141 | if (!device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3142 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3143 | outputs.add(mOutputs.keyAt(i)); |
| 3144 | } else { |
| 3145 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3146 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3147 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3148 | } |
| 3149 | } |
| 3150 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3151 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3152 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3153 | { |
| 3154 | if (mHwModules[i]->mHandle == 0) { |
| 3155 | continue; |
| 3156 | } |
| 3157 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3158 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3159 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
| 3160 | if (profile->mSupportedDevices.types() & device) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3161 | if (!device_distinguishes_on_address(device) || |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3162 | address == profile->mSupportedDevices[0]->mAddress) { |
| 3163 | profiles.add(profile); |
| 3164 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3165 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3170 | ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size()); |
| 3171 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3172 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3173 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3174 | return BAD_VALUE; |
| 3175 | } |
| 3176 | |
| 3177 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3178 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3179 | 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] | 3180 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3181 | |
| 3182 | // nothing to do if one output is already opened for this profile |
| 3183 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3184 | for (j = 0; j < outputs.size(); j++) { |
| 3185 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3186 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3187 | // matching profile: save the sample rates, format and channel masks supported |
| 3188 | // by the profile in our device descriptor |
| 3189 | devDesc->importAudioPort(profile); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3190 | break; |
| 3191 | } |
| 3192 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3193 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3194 | continue; |
| 3195 | } |
| 3196 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3197 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3198 | device, address.string(), profile.get()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3199 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3200 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3201 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3202 | config.sample_rate = desc->mSamplingRate; |
| 3203 | config.channel_mask = desc->mChannelMask; |
| 3204 | config.format = desc->mFormat; |
| 3205 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3206 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3207 | config.offload_info.format = desc->mFormat; |
| 3208 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3209 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3210 | &output, |
| 3211 | &config, |
| 3212 | &desc->mDevice, |
| 3213 | address, |
| 3214 | &desc->mLatency, |
| 3215 | desc->mFlags); |
| 3216 | if (status == NO_ERROR) { |
| 3217 | desc->mSamplingRate = config.sample_rate; |
| 3218 | desc->mChannelMask = config.channel_mask; |
| 3219 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3220 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3221 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3222 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3223 | char *param = audio_device_address_to_parameter(device, address); |
| 3224 | mpClientInterface->setParameters(output, String8(param)); |
| 3225 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3226 | } |
| 3227 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3228 | // Here is where we step through and resolve any "dynamic" fields |
| 3229 | String8 reply; |
| 3230 | char *value; |
| 3231 | if (profile->mSamplingRates[0] == 0) { |
| 3232 | reply = mpClientInterface->getParameters(output, |
| 3233 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3234 | ALOGV("checkOutputsForDevice() supported sampling rates %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3235 | reply.string()); |
| 3236 | value = strpbrk((char *)reply.string(), "="); |
| 3237 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3238 | profile->loadSamplingRates(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3239 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3240 | } |
| 3241 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3242 | reply = mpClientInterface->getParameters(output, |
| 3243 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3244 | ALOGV("checkOutputsForDevice() supported formats %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3245 | reply.string()); |
| 3246 | value = strpbrk((char *)reply.string(), "="); |
| 3247 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3248 | profile->loadFormats(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3249 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3250 | } |
| 3251 | if (profile->mChannelMasks[0] == 0) { |
| 3252 | reply = mpClientInterface->getParameters(output, |
| 3253 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3254 | ALOGV("checkOutputsForDevice() supported channel masks %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3255 | reply.string()); |
| 3256 | value = strpbrk((char *)reply.string(), "="); |
| 3257 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3258 | profile->loadOutChannels(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3259 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3260 | } |
| 3261 | if (((profile->mSamplingRates[0] == 0) && |
| 3262 | (profile->mSamplingRates.size() < 2)) || |
| 3263 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && |
| 3264 | (profile->mFormats.size() < 2)) || |
| 3265 | ((profile->mChannelMasks[0] == 0) && |
| 3266 | (profile->mChannelMasks.size() < 2))) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3267 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3268 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3269 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 3270 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || |
| 3271 | profile->mChannelMasks[0] == 0) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3272 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3273 | config.sample_rate = profile->pickSamplingRate(); |
| 3274 | config.channel_mask = profile->pickChannelMask(); |
| 3275 | config.format = profile->pickFormat(); |
| 3276 | config.offload_info.sample_rate = config.sample_rate; |
| 3277 | config.offload_info.channel_mask = config.channel_mask; |
| 3278 | config.offload_info.format = config.format; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3279 | status = mpClientInterface->openOutput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3280 | &output, |
| 3281 | &config, |
| 3282 | &desc->mDevice, |
| 3283 | address, |
| 3284 | &desc->mLatency, |
| 3285 | desc->mFlags); |
| 3286 | if (status == NO_ERROR) { |
| 3287 | desc->mSamplingRate = config.sample_rate; |
| 3288 | desc->mChannelMask = config.channel_mask; |
| 3289 | desc->mFormat = config.format; |
| 3290 | } else { |
| 3291 | output = AUDIO_IO_HANDLE_NONE; |
| 3292 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3295 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3296 | addOutput(output, desc); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3297 | if (device_distinguishes_on_address(device) && address != "0") { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3298 | sp<AudioPolicyMix> policyMix; |
| 3299 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3300 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3301 | address.string()); |
| 3302 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3303 | policyMix->setOutput(desc); |
Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 3304 | desc->mPolicyMix = policyMix->getMix(); |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3305 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3306 | } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { |
| 3307 | // no duplicated output for direct outputs and |
| 3308 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3309 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3310 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3311 | // set initial stream volume for device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3312 | applyStreamVolumes(desc, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3313 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3314 | //TODO: configure audio effect output stage here |
| 3315 | |
| 3316 | // 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] | 3317 | duplicatedOutput = |
| 3318 | mpClientInterface->openDuplicateOutput(output, |
| 3319 | mPrimaryOutput->mIoHandle); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3320 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3321 | // add duplicated output descriptor |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3322 | sp<SwAudioOutputDescriptor> dupOutputDesc = |
| 3323 | new SwAudioOutputDescriptor(NULL, mpClientInterface); |
| 3324 | dupOutputDesc->mOutput1 = mPrimaryOutput; |
| 3325 | dupOutputDesc->mOutput2 = desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3326 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3327 | dupOutputDesc->mFormat = desc->mFormat; |
| 3328 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3329 | dupOutputDesc->mLatency = desc->mLatency; |
| 3330 | addOutput(duplicatedOutput, dupOutputDesc); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3331 | applyStreamVolumes(dupOutputDesc, device, 0, true); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3332 | } else { |
| 3333 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3334 | mPrimaryOutput->mIoHandle, output); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3335 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3336 | removeOutput(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3337 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3338 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3339 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3340 | } |
| 3341 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3342 | } else { |
| 3343 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3344 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3345 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3346 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3347 | profiles.removeAt(profile_index); |
| 3348 | profile_index--; |
| 3349 | } else { |
| 3350 | outputs.add(output); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3351 | devDesc->importAudioPort(profile); |
| 3352 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3353 | if (device_distinguishes_on_address(device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3354 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3355 | device, address.string()); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3356 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3357 | NULL/*patch handle*/, address.string()); |
| 3358 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3359 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3360 | } |
| 3361 | } |
| 3362 | |
| 3363 | if (profiles.isEmpty()) { |
| 3364 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3365 | return BAD_VALUE; |
| 3366 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3367 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3368 | // check if one opened output is not needed any more after disconnecting one device |
| 3369 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3370 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3371 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3372 | // exact match on device |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3373 | if (device_distinguishes_on_address(device) && |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3374 | (desc->supportedDevices() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3375 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3376 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3377 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3378 | mOutputs.keyAt(i)); |
| 3379 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3380 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3381 | } |
| 3382 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3383 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3384 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3385 | { |
| 3386 | if (mHwModules[i]->mHandle == 0) { |
| 3387 | continue; |
| 3388 | } |
| 3389 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3390 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3391 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3392 | if (profile->mSupportedDevices.types() & device) { |
| 3393 | ALOGV("checkOutputsForDevice(): " |
| 3394 | "clearing direct output profile %zu on module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3395 | if (profile->mSamplingRates[0] == 0) { |
| 3396 | profile->mSamplingRates.clear(); |
| 3397 | profile->mSamplingRates.add(0); |
| 3398 | } |
| 3399 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3400 | profile->mFormats.clear(); |
| 3401 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3402 | } |
| 3403 | if (profile->mChannelMasks[0] == 0) { |
| 3404 | profile->mChannelMasks.clear(); |
| 3405 | profile->mChannelMasks.add(0); |
| 3406 | } |
| 3407 | } |
| 3408 | } |
| 3409 | } |
| 3410 | } |
| 3411 | return NO_ERROR; |
| 3412 | } |
| 3413 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3414 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3415 | audio_policy_dev_state_t state, |
| 3416 | SortedVector<audio_io_handle_t>& inputs, |
| 3417 | const String8 address) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3418 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3419 | sp<AudioInputDescriptor> desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3420 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3421 | // first list already open inputs that can be routed to this device |
| 3422 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3423 | desc = mInputs.valueAt(input_index); |
| 3424 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
| 3425 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3426 | inputs.add(mInputs.keyAt(input_index)); |
| 3427 | } |
| 3428 | } |
| 3429 | |
| 3430 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3431 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3432 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3433 | { |
| 3434 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3435 | continue; |
| 3436 | } |
| 3437 | for (size_t profile_index = 0; |
| 3438 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3439 | profile_index++) |
| 3440 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3441 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3442 | |
| 3443 | if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3444 | if (!device_distinguishes_on_address(device) || |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3445 | address == profile->mSupportedDevices[0]->mAddress) { |
| 3446 | profiles.add(profile); |
| 3447 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3448 | profile_index, module_idx); |
| 3449 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3450 | } |
| 3451 | } |
| 3452 | } |
| 3453 | |
| 3454 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3455 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3456 | return BAD_VALUE; |
| 3457 | } |
| 3458 | |
| 3459 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3460 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3461 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3462 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3463 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3464 | // nothing to do if one input is already opened for this profile |
| 3465 | size_t input_index; |
| 3466 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3467 | desc = mInputs.valueAt(input_index); |
| 3468 | if (desc->mProfile == profile) { |
| 3469 | break; |
| 3470 | } |
| 3471 | } |
| 3472 | if (input_index != mInputs.size()) { |
| 3473 | continue; |
| 3474 | } |
| 3475 | |
| 3476 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3477 | desc = new AudioInputDescriptor(profile); |
| 3478 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3479 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3480 | config.sample_rate = desc->mSamplingRate; |
| 3481 | config.channel_mask = desc->mChannelMask; |
| 3482 | config.format = desc->mFormat; |
| 3483 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3484 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3485 | &input, |
| 3486 | &config, |
| 3487 | &desc->mDevice, |
| 3488 | address, |
| 3489 | AUDIO_SOURCE_MIC, |
| 3490 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3491 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3492 | if (status == NO_ERROR) { |
| 3493 | desc->mSamplingRate = config.sample_rate; |
| 3494 | desc->mChannelMask = config.channel_mask; |
| 3495 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3496 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3497 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3498 | char *param = audio_device_address_to_parameter(device, address); |
| 3499 | mpClientInterface->setParameters(input, String8(param)); |
| 3500 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3501 | } |
| 3502 | |
| 3503 | // Here is where we step through and resolve any "dynamic" fields |
| 3504 | String8 reply; |
| 3505 | char *value; |
| 3506 | if (profile->mSamplingRates[0] == 0) { |
| 3507 | reply = mpClientInterface->getParameters(input, |
| 3508 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
| 3509 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", |
| 3510 | reply.string()); |
| 3511 | value = strpbrk((char *)reply.string(), "="); |
| 3512 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3513 | profile->loadSamplingRates(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3514 | } |
| 3515 | } |
| 3516 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3517 | reply = mpClientInterface->getParameters(input, |
| 3518 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
| 3519 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); |
| 3520 | value = strpbrk((char *)reply.string(), "="); |
| 3521 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3522 | profile->loadFormats(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3523 | } |
| 3524 | } |
| 3525 | if (profile->mChannelMasks[0] == 0) { |
| 3526 | reply = mpClientInterface->getParameters(input, |
| 3527 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
| 3528 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", |
| 3529 | reply.string()); |
| 3530 | value = strpbrk((char *)reply.string(), "="); |
| 3531 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3532 | profile->loadInChannels(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3533 | } |
| 3534 | } |
| 3535 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || |
| 3536 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || |
| 3537 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { |
| 3538 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3539 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3540 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | if (input != 0) { |
| 3544 | addInput(input, desc); |
| 3545 | } |
| 3546 | } // endif input != 0 |
| 3547 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3548 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3549 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3550 | profiles.removeAt(profile_index); |
| 3551 | profile_index--; |
| 3552 | } else { |
| 3553 | inputs.add(input); |
| 3554 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3555 | } |
| 3556 | } // end scan profiles |
| 3557 | |
| 3558 | if (profiles.isEmpty()) { |
| 3559 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3560 | return BAD_VALUE; |
| 3561 | } |
| 3562 | } else { |
| 3563 | // Disconnect |
| 3564 | // check if one opened input is not needed any more after disconnecting one device |
| 3565 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3566 | desc = mInputs.valueAt(input_index); |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3567 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() & |
| 3568 | ~AUDIO_DEVICE_BIT_IN)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3569 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 3570 | mInputs.keyAt(input_index)); |
| 3571 | inputs.add(mInputs.keyAt(input_index)); |
| 3572 | } |
| 3573 | } |
| 3574 | // Clear any profiles associated with the disconnected device. |
| 3575 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 3576 | if (mHwModules[module_index]->mHandle == 0) { |
| 3577 | continue; |
| 3578 | } |
| 3579 | for (size_t profile_index = 0; |
| 3580 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 3581 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3582 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3583 | if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3584 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3585 | profile_index, module_index); |
| 3586 | if (profile->mSamplingRates[0] == 0) { |
| 3587 | profile->mSamplingRates.clear(); |
| 3588 | profile->mSamplingRates.add(0); |
| 3589 | } |
| 3590 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3591 | profile->mFormats.clear(); |
| 3592 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3593 | } |
| 3594 | if (profile->mChannelMasks[0] == 0) { |
| 3595 | profile->mChannelMasks.clear(); |
| 3596 | profile->mChannelMasks.add(0); |
| 3597 | } |
| 3598 | } |
| 3599 | } |
| 3600 | } |
| 3601 | } // end disconnect |
| 3602 | |
| 3603 | return NO_ERROR; |
| 3604 | } |
| 3605 | |
| 3606 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3607 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3608 | { |
| 3609 | ALOGV("closeOutput(%d)", output); |
| 3610 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3611 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3612 | if (outputDesc == NULL) { |
| 3613 | ALOGW("closeOutput() unknown output %d", output); |
| 3614 | return; |
| 3615 | } |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 3616 | mPolicyMixes.closeOutput(outputDesc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3617 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3618 | // look for duplicated outputs connected to the output being removed. |
| 3619 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3620 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3621 | if (dupOutputDesc->isDuplicated() && |
| 3622 | (dupOutputDesc->mOutput1 == outputDesc || |
| 3623 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3624 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3625 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 3626 | outputDesc2 = dupOutputDesc->mOutput2; |
| 3627 | } else { |
| 3628 | outputDesc2 = dupOutputDesc->mOutput1; |
| 3629 | } |
| 3630 | // As all active tracks on duplicated output will be deleted, |
| 3631 | // and as they were also referenced on the other output, the reference |
| 3632 | // count for their stream type must be adjusted accordingly on |
| 3633 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3634 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3635 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3636 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3637 | } |
| 3638 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 3639 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 3640 | |
| 3641 | mpClientInterface->closeOutput(duplicatedOutput); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3642 | removeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3646 | nextAudioPortGeneration(); |
| 3647 | |
| 3648 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3649 | if (index >= 0) { |
| 3650 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3651 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3652 | mAudioPatches.removeItemsAt(index); |
| 3653 | mpClientInterface->onAudioPatchListUpdate(); |
| 3654 | } |
| 3655 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3656 | AudioParameter param; |
| 3657 | param.add(String8("closing"), String8("true")); |
| 3658 | mpClientInterface->setParameters(output, param.toString()); |
| 3659 | |
| 3660 | mpClientInterface->closeOutput(output); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3661 | removeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3662 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3663 | } |
| 3664 | |
| 3665 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 3666 | { |
| 3667 | ALOGV("closeInput(%d)", input); |
| 3668 | |
| 3669 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 3670 | if (inputDesc == NULL) { |
| 3671 | ALOGW("closeInput() unknown input %d", input); |
| 3672 | return; |
| 3673 | } |
| 3674 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3675 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3676 | |
| 3677 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3678 | if (index >= 0) { |
| 3679 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3680 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3681 | mAudioPatches.removeItemsAt(index); |
| 3682 | mpClientInterface->onAudioPatchListUpdate(); |
| 3683 | } |
| 3684 | |
| 3685 | mpClientInterface->closeInput(input); |
| 3686 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3687 | } |
| 3688 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3689 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( |
| 3690 | audio_devices_t device, |
| 3691 | SwAudioOutputCollection openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3692 | { |
| 3693 | SortedVector<audio_io_handle_t> outputs; |
| 3694 | |
| 3695 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 3696 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 3697 | ALOGVV("output %d isDuplicated=%d device=%04x", |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3698 | i, openOutputs.valueAt(i)->isDuplicated(), |
| 3699 | openOutputs.valueAt(i)->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3700 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 3701 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 3702 | outputs.add(openOutputs.keyAt(i)); |
| 3703 | } |
| 3704 | } |
| 3705 | return outputs; |
| 3706 | } |
| 3707 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3708 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3709 | SortedVector<audio_io_handle_t>& outputs2) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3710 | { |
| 3711 | if (outputs1.size() != outputs2.size()) { |
| 3712 | return false; |
| 3713 | } |
| 3714 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 3715 | if (outputs1[i] != outputs2[i]) { |
| 3716 | return false; |
| 3717 | } |
| 3718 | } |
| 3719 | return true; |
| 3720 | } |
| 3721 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3722 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3723 | { |
| 3724 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3725 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3726 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 3727 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 3728 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3729 | // also take into account external policy-related changes: add all outputs which are |
| 3730 | // associated with policies in the "before" and "after" output vectors |
| 3731 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 3732 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3733 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3734 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 3735 | srcOutputs.add(desc->mIoHandle); |
| 3736 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 3737 | } |
| 3738 | } |
| 3739 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3740 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3741 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 3742 | dstOutputs.add(desc->mIoHandle); |
| 3743 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 3744 | } |
| 3745 | } |
| 3746 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3747 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 3748 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 3749 | strategy, srcOutputs[0], dstOutputs[0]); |
| 3750 | // mute strategy while moving tracks from one output to another |
| 3751 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3752 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3753 | if (isStrategyActive(desc, strategy)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3754 | setStrategyMute(strategy, true, desc); |
| 3755 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3756 | } |
| 3757 | } |
| 3758 | |
| 3759 | // Move effects associated to this strategy from previous output to new output |
| 3760 | if (strategy == STRATEGY_MEDIA) { |
| 3761 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 3762 | SortedVector<audio_io_handle_t> moved; |
| 3763 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3764 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 3765 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 3766 | effectDesc->mIo != fxOutput) { |
| 3767 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3768 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 3769 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3770 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3771 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3772 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3773 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3774 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3775 | } |
| 3776 | } |
| 3777 | } |
| 3778 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3779 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3780 | if (i == AUDIO_STREAM_PATCH) { |
| 3781 | continue; |
| 3782 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3783 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 3784 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3785 | } |
| 3786 | } |
| 3787 | } |
| 3788 | } |
| 3789 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3790 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3791 | { |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3792 | 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] | 3793 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3794 | checkOutputForStrategy(STRATEGY_PHONE); |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3795 | 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] | 3796 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3797 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 3798 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3799 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3800 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 3801 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3802 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3803 | } |
| 3804 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3805 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3806 | { |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3807 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3809 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3810 | return; |
| 3811 | } |
| 3812 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3813 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3814 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 3815 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 3816 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3817 | // suspend A2DP output if: |
| 3818 | // (NOT already suspended) && |
| 3819 | // ((SCO device is connected && |
| 3820 | // (forced usage for communication || for record is SCO))) || |
| 3821 | // (phone state is ringing || in call) |
| 3822 | // |
| 3823 | // restore A2DP output if: |
| 3824 | // (Already suspended) && |
| 3825 | // ((SCO device is NOT connected || |
| 3826 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 3827 | // (phone state is NOT ringing && NOT in call) |
| 3828 | // |
| 3829 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3830 | if ((!isScoConnected || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3831 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != AUDIO_POLICY_FORCE_BT_SCO) && |
| 3832 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 3833 | ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && |
| 3834 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3835 | |
| 3836 | mpClientInterface->restoreOutput(a2dpOutput); |
| 3837 | mA2dpSuspended = false; |
| 3838 | } |
| 3839 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3840 | if ((isScoConnected && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3841 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) || |
| 3842 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 3843 | ((mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || |
| 3844 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3845 | |
| 3846 | mpClientInterface->suspendOutput(a2dpOutput); |
| 3847 | mA2dpSuspended = true; |
| 3848 | } |
| 3849 | } |
| 3850 | } |
| 3851 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3852 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
| 3853 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3854 | { |
| 3855 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 3856 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3857 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3858 | if (index >= 0) { |
| 3859 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3860 | if (patchDesc->mUid != mUidCached) { |
| 3861 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
| 3862 | outputDesc->device(), outputDesc->mPatchHandle); |
| 3863 | return outputDesc->device(); |
| 3864 | } |
| 3865 | } |
| 3866 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3867 | // 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] | 3868 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3869 | // use device for strategy enforced audible |
| 3870 | // 2: we are in call or the strategy phone is active on the output: |
| 3871 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3872 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 3873 | // use the device for strategy enforced audible |
| 3874 | // 4: the strategy sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3875 | // use device for strategy sonification |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3876 | // 5: the strategy "respectful" sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3877 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3878 | // 6: the strategy accessibility is active on the output: |
| 3879 | // use device for strategy accessibility |
| 3880 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3881 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3882 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3883 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3884 | // 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] | 3885 | // use device for strategy t-t-s |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3886 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3887 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3888 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 3889 | } else if (isInCall() || |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3890 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3891 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3892 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 3893 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3894 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3895 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3896 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3897 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3898 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3899 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3900 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3901 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3902 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3903 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3904 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3905 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3906 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3907 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3908 | } |
| 3909 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3910 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 3911 | return device; |
| 3912 | } |
| 3913 | |
| 3914 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
| 3915 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3916 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3917 | |
| 3918 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3919 | if (index >= 0) { |
| 3920 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3921 | if (patchDesc->mUid != mUidCached) { |
| 3922 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
| 3923 | inputDesc->mDevice, inputDesc->mPatchHandle); |
| 3924 | return inputDesc->mDevice; |
| 3925 | } |
| 3926 | } |
| 3927 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3928 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3929 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3930 | return device; |
| 3931 | } |
| 3932 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3933 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3934 | return (uint32_t)getStrategy(stream); |
| 3935 | } |
| 3936 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3937 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3938 | // By checking the range of stream before calling getStrategy, we avoid |
| 3939 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 3940 | // 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] | 3941 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3942 | return AUDIO_DEVICE_NONE; |
| 3943 | } |
| 3944 | audio_devices_t devices; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 3945 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3946 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3947 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); |
| 3948 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3949 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 3950 | if (isStrategyActive(outputDesc, strategy)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3951 | devices = outputDesc->device(); |
| 3952 | break; |
| 3953 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3954 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 3955 | |
| 3956 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 3957 | and doesn't really need to.*/ |
| 3958 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 3959 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 3960 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 3961 | } |
| 3962 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3963 | return devices; |
| 3964 | } |
| 3965 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3966 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const |
| 3967 | { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 3968 | 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] | 3969 | return mEngine->getStrategyForStream(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3970 | } |
| 3971 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3972 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 3973 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3974 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 3975 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 3976 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3977 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 3978 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 3979 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3980 | // usage to strategy mapping |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3981 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3984 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3985 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3986 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3987 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 3988 | updateDevicesAndOutputs(); |
| 3989 | break; |
| 3990 | default: |
| 3991 | break; |
| 3992 | } |
| 3993 | } |
| 3994 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3995 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
| 3996 | switch(event) { |
| 3997 | case STARTING_OUTPUT: |
| 3998 | mBeaconMuteRefCount++; |
| 3999 | break; |
| 4000 | case STOPPING_OUTPUT: |
| 4001 | if (mBeaconMuteRefCount > 0) { |
| 4002 | mBeaconMuteRefCount--; |
| 4003 | } |
| 4004 | break; |
| 4005 | case STARTING_BEACON: |
| 4006 | mBeaconPlayingRefCount++; |
| 4007 | break; |
| 4008 | case STOPPING_BEACON: |
| 4009 | if (mBeaconPlayingRefCount > 0) { |
| 4010 | mBeaconPlayingRefCount--; |
| 4011 | } |
| 4012 | break; |
| 4013 | } |
| 4014 | |
| 4015 | if (mBeaconMuteRefCount > 0) { |
| 4016 | // any playback causes beacon to be muted |
| 4017 | return setBeaconMute(true); |
| 4018 | } else { |
| 4019 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4020 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4025 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4026 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4027 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4028 | if (mBeaconMuted != mute) { |
| 4029 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4030 | ALOGV("\t muting %d", mute); |
| 4031 | uint32_t maxLatency = 0; |
| 4032 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4033 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4034 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4035 | desc, |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4036 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4037 | const uint32_t latency = desc->latency() * 2; |
| 4038 | if (latency > maxLatency) { |
| 4039 | maxLatency = latency; |
| 4040 | } |
| 4041 | } |
| 4042 | mBeaconMuted = mute; |
| 4043 | return maxLatency; |
| 4044 | } |
| 4045 | return 0; |
| 4046 | } |
| 4047 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4048 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4049 | bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4050 | { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4051 | // Routing |
| 4052 | // see if we have an explicit route |
| 4053 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy |
| 4054 | // (getStrategy(stream)). |
| 4055 | // if the strategy from the stream type in the RouteMap is the same as the argument above, |
| 4056 | // and activity count is non-zero |
| 4057 | // the device = the device from the descriptor in the RouteMap, and exit. |
| 4058 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { |
| 4059 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); |
| 4060 | routing_strategy strat = getStrategy(route->mStreamType); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4061 | if (strat == strategy && route->isActive()) { |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4062 | return route->mDeviceDescriptor->type(); |
| 4063 | } |
| 4064 | } |
| 4065 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4066 | if (fromCache) { |
| 4067 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4068 | strategy, mDeviceForStrategy[strategy]); |
| 4069 | return mDeviceForStrategy[strategy]; |
| 4070 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4071 | return mEngine->getDeviceForStrategy(strategy); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4072 | } |
| 4073 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4074 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4075 | { |
| 4076 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4077 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4078 | } |
| 4079 | mPreviousOutputs = mOutputs; |
| 4080 | } |
| 4081 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4082 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4083 | audio_devices_t prevDevice, |
| 4084 | uint32_t delayMs) |
| 4085 | { |
| 4086 | // mute/unmute strategies using an incompatible device combination |
| 4087 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4088 | // if unmuting, unmute only after the specified delay |
| 4089 | if (outputDesc->isDuplicated()) { |
| 4090 | return 0; |
| 4091 | } |
| 4092 | |
| 4093 | uint32_t muteWaitMs = 0; |
| 4094 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4095 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4096 | |
| 4097 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4098 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4099 | curDevice = curDevice & outputDesc->supportedDevices(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4100 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4101 | bool doMute = false; |
| 4102 | |
| 4103 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4104 | doMute = true; |
| 4105 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4106 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4107 | doMute = true; |
| 4108 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4109 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4110 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4111 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4112 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4113 | // skip output if it does not share any device with current output |
| 4114 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4115 | == AUDIO_DEVICE_NONE) { |
| 4116 | continue; |
| 4117 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4118 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x)", |
| 4119 | mute ? "muting" : "unmuting", i, curDevice); |
| 4120 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4121 | if (isStrategyActive(desc, (routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4122 | if (mute) { |
| 4123 | // FIXME: should not need to double latency if volume could be applied |
| 4124 | // immediately by the audioflinger mixer. We must account for the delay |
| 4125 | // between now and the next time the audioflinger thread for this output |
| 4126 | // will process a buffer (which corresponds to one buffer size, |
| 4127 | // usually 1/2 or 1/4 of the latency). |
| 4128 | if (muteWaitMs < desc->latency() * 2) { |
| 4129 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4130 | } |
| 4131 | } |
| 4132 | } |
| 4133 | } |
| 4134 | } |
| 4135 | } |
| 4136 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4137 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4138 | // different per device volumes |
| 4139 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4140 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4141 | muteWaitMs = outputDesc->latency() * 2; |
| 4142 | } |
| 4143 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4144 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4145 | setStrategyMute((routing_strategy)i, true, outputDesc); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4146 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4147 | setStrategyMute((routing_strategy)i, false, outputDesc, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4148 | muteWaitMs *2, device); |
| 4149 | } |
| 4150 | } |
| 4151 | } |
| 4152 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4153 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4154 | if (muteWaitMs > delayMs) { |
| 4155 | muteWaitMs -= delayMs; |
| 4156 | usleep(muteWaitMs * 1000); |
| 4157 | return muteWaitMs; |
| 4158 | } |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4162 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4163 | audio_devices_t device, |
| 4164 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4165 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4166 | audio_patch_handle_t *patchHandle, |
| 4167 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4168 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4169 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4170 | AudioParameter param; |
| 4171 | uint32_t muteWaitMs; |
| 4172 | |
| 4173 | if (outputDesc->isDuplicated()) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4174 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); |
| 4175 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | return muteWaitMs; |
| 4177 | } |
| 4178 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4179 | // output profile |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4180 | if ((device != AUDIO_DEVICE_NONE) && |
| 4181 | ((device & outputDesc->supportedDevices()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4182 | return 0; |
| 4183 | } |
| 4184 | |
| 4185 | // filter devices according to output selected |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4186 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4187 | |
| 4188 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4189 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4190 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4191 | |
| 4192 | if (device != AUDIO_DEVICE_NONE) { |
| 4193 | outputDesc->mDevice = device; |
| 4194 | } |
| 4195 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4196 | |
| 4197 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4198 | // the requested device is AUDIO_DEVICE_NONE |
| 4199 | // OR the requested device is the same as current device |
| 4200 | // AND force is not specified |
| 4201 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4202 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4203 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && |
| 4204 | !force && |
| 4205 | outputDesc->mPatchHandle != 0) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4206 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4207 | return muteWaitMs; |
| 4208 | } |
| 4209 | |
| 4210 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4211 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4212 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4213 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4214 | resetOutputDevice(outputDesc, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4215 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4216 | DeviceVector deviceList = (address == NULL) ? |
| 4217 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4218 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4219 | if (!deviceList.isEmpty()) { |
| 4220 | struct audio_patch patch; |
| 4221 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4222 | patch.num_sources = 1; |
| 4223 | patch.num_sinks = 0; |
| 4224 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4225 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4226 | patch.num_sinks++; |
| 4227 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4228 | ssize_t index; |
| 4229 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4230 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4231 | } else { |
| 4232 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4233 | } |
| 4234 | sp< AudioPatch> patchDesc; |
| 4235 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4236 | if (index >= 0) { |
| 4237 | patchDesc = mAudioPatches.valueAt(index); |
| 4238 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4239 | } |
| 4240 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4241 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4242 | &afPatchHandle, |
| 4243 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4244 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4245 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4246 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4247 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4248 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4249 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4250 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4251 | } else { |
| 4252 | patchDesc->mPatch = patch; |
| 4253 | } |
| 4254 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4255 | patchDesc->mUid = mUidCached; |
| 4256 | if (patchHandle) { |
| 4257 | *patchHandle = patchDesc->mHandle; |
| 4258 | } |
| 4259 | outputDesc->mPatchHandle = patchDesc->mHandle; |
| 4260 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4261 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4262 | } |
| 4263 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4264 | |
| 4265 | // inform all input as well |
| 4266 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4267 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4268 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4269 | AudioParameter inputCmd = AudioParameter(); |
| 4270 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4271 | inputDescriptor->mIoHandle, device); |
| 4272 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4273 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4274 | inputCmd.toString(), |
| 4275 | delayMs); |
| 4276 | } |
| 4277 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4278 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4279 | |
| 4280 | // update stream volumes according to new device |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4281 | applyStreamVolumes(outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4282 | |
| 4283 | return muteWaitMs; |
| 4284 | } |
| 4285 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4286 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4287 | int delayMs, |
| 4288 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4289 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4290 | ssize_t index; |
| 4291 | if (patchHandle) { |
| 4292 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4293 | } else { |
| 4294 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4295 | } |
| 4296 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4297 | return INVALID_OPERATION; |
| 4298 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4299 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4300 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4301 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
| 4302 | outputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4303 | removeAudioPatch(patchDesc->mHandle); |
| 4304 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4305 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4306 | return status; |
| 4307 | } |
| 4308 | |
| 4309 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4310 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4311 | bool force, |
| 4312 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4313 | { |
| 4314 | status_t status = NO_ERROR; |
| 4315 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4316 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4317 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4318 | inputDesc->mDevice = device; |
| 4319 | |
| 4320 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4321 | if (!deviceList.isEmpty()) { |
| 4322 | struct audio_patch patch; |
| 4323 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4324 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4325 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4326 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
| 4327 | !inputDesc->mIsSoundTrigger) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4328 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4329 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4330 | patch.num_sinks = 1; |
| 4331 | //only one input device for now |
| 4332 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4333 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4334 | ssize_t index; |
| 4335 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4336 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4337 | } else { |
| 4338 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4339 | } |
| 4340 | sp< AudioPatch> patchDesc; |
| 4341 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4342 | if (index >= 0) { |
| 4343 | patchDesc = mAudioPatches.valueAt(index); |
| 4344 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4345 | } |
| 4346 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4347 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4348 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4349 | 0); |
| 4350 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4351 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4352 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4353 | if (index < 0) { |
François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 4354 | patchDesc = new AudioPatch(&patch, mUidCached); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4355 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4356 | } else { |
| 4357 | patchDesc->mPatch = patch; |
| 4358 | } |
| 4359 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4360 | patchDesc->mUid = mUidCached; |
| 4361 | if (patchHandle) { |
| 4362 | *patchHandle = patchDesc->mHandle; |
| 4363 | } |
| 4364 | inputDesc->mPatchHandle = patchDesc->mHandle; |
| 4365 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4366 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4367 | } |
| 4368 | } |
| 4369 | } |
| 4370 | return status; |
| 4371 | } |
| 4372 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4373 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4374 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4375 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4376 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4377 | ssize_t index; |
| 4378 | if (patchHandle) { |
| 4379 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4380 | } else { |
| 4381 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4382 | } |
| 4383 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4384 | return INVALID_OPERATION; |
| 4385 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4386 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4387 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4388 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
| 4389 | inputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4390 | removeAudioPatch(patchDesc->mHandle); |
| 4391 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4392 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4393 | return status; |
| 4394 | } |
| 4395 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 4396 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4397 | String8 address, |
| 4398 | uint32_t& samplingRate, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4399 | audio_format_t& format, |
| 4400 | audio_channel_mask_t& channelMask, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4401 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4402 | { |
| 4403 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4404 | // profile supporting all requested parameters. |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4405 | // |
| 4406 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return |
| 4407 | // the best matching profile, not the first one. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4408 | |
| 4409 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4410 | { |
| 4411 | if (mHwModules[i]->mHandle == 0) { |
| 4412 | continue; |
| 4413 | } |
| 4414 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4415 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4416 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4417 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4418 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4419 | &samplingRate /*updatedSamplingRate*/, |
Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 4420 | format, |
| 4421 | &format /*updatedFormat*/, |
| 4422 | channelMask, |
| 4423 | &channelMask /*updatedChannelMask*/, |
| 4424 | (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4425 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4426 | return profile; |
| 4427 | } |
| 4428 | } |
| 4429 | } |
| 4430 | return NULL; |
| 4431 | } |
| 4432 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4433 | |
| 4434 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4435 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4436 | { |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4437 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
| 4438 | audio_devices_t selectedDeviceFromMix = |
| 4439 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4440 | |
François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4441 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { |
| 4442 | return selectedDeviceFromMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4443 | } |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4444 | return getDeviceForInputSource(inputSource); |
| 4445 | } |
| 4446 | |
| 4447 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 4448 | { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4449 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { |
| 4450 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4451 | if (inputSource == route->mSource && route->isActive()) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4452 | return route->mDeviceDescriptor->type(); |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | return mEngine->getDeviceForInputSource(inputSource); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4457 | } |
| 4458 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4459 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4460 | int index, |
| 4461 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4462 | { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4463 | float volumeDb = mEngine->volIndexToDb(Volume::getDeviceCategory(device), stream, index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4464 | |
| 4465 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 4466 | // to avoid sound level bursts in user's ears: |
| 4467 | // - always attenuate ring tones and notifications volume by 6dB |
| 4468 | // - if music is playing, always limit the volume to current music volume, |
| 4469 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4470 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4471 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 4472 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 4473 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 4474 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 4475 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 4476 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4477 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4478 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4479 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && |
| 4480 | mStreams.canBeMuted(stream)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4481 | volumeDb += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4482 | // when the phone is ringing we must consider that music could have been paused just before |
| 4483 | // by the music application and behave as if music was active if the last music track was |
| 4484 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4485 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4486 | mLimitRingtoneVolume) { |
| 4487 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4488 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, |
| 4489 | mStreams.valueFor(AUDIO_STREAM_MUSIC).getVolumeIndex(musicDevice), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4490 | musicDevice); |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4491 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? |
| 4492 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; |
| 4493 | if (volumeDb > minVolDB) { |
| 4494 | volumeDb = minVolDB; |
| 4495 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4496 | } |
| 4497 | } |
| 4498 | } |
| 4499 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4500 | return volumeDb; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4501 | } |
| 4502 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4503 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4504 | int index, |
| 4505 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4506 | audio_devices_t device, |
| 4507 | int delayMs, |
| 4508 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4509 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4510 | // do not change actual stream volume if the stream is muted |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4511 | if (outputDesc->mMuteCount[stream] != 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4512 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4513 | stream, outputDesc->mMuteCount[stream]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4514 | return NO_ERROR; |
| 4515 | } |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4516 | audio_policy_forced_cfg_t forceUseForComm = |
| 4517 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4518 | // 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] | 4519 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4520 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4521 | 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] | 4522 | stream, forceUseForComm); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4523 | return INVALID_OPERATION; |
| 4524 | } |
| 4525 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4526 | if (device == AUDIO_DEVICE_NONE) { |
| 4527 | device = outputDesc->device(); |
| 4528 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4529 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4530 | float volumeDb = computeVolume(stream, index, device); |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4531 | if (outputDesc->isFixedVolume(device)) { |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4532 | volumeDb = 0.0f; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4533 | } |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4534 | |
Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 4535 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4536 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4537 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 4538 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4539 | float voiceVolume; |
| 4540 | // 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] | 4541 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4542 | voiceVolume = (float)index/(float)mStreams.valueFor(stream).getVolumeIndexMax(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4543 | } else { |
| 4544 | voiceVolume = 1.0; |
| 4545 | } |
| 4546 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4547 | if (voiceVolume != mLastVoiceVolume && outputDesc == mPrimaryOutput) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4548 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 4549 | mLastVoiceVolume = voiceVolume; |
| 4550 | } |
| 4551 | } |
| 4552 | |
| 4553 | return NO_ERROR; |
| 4554 | } |
| 4555 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4556 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, |
| 4557 | audio_devices_t device, |
| 4558 | int delayMs, |
| 4559 | bool force) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4560 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4561 | ALOGVV("applyStreamVolumes() for device %08x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4562 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4563 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4564 | if (stream == AUDIO_STREAM_PATCH) { |
| 4565 | continue; |
| 4566 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4567 | checkAndSetVolume((audio_stream_type_t)stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4568 | mStreams.valueFor((audio_stream_type_t)stream).getVolumeIndex(device), |
| 4569 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4570 | device, |
| 4571 | delayMs, |
| 4572 | force); |
| 4573 | } |
| 4574 | } |
| 4575 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4576 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4577 | bool on, |
| 4578 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4579 | int delayMs, |
| 4580 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4581 | { |
| 4582 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4583 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4584 | if (stream == AUDIO_STREAM_PATCH) { |
| 4585 | continue; |
| 4586 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4587 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4588 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | } |
| 4592 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4593 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4594 | bool on, |
| 4595 | const sp<AudioOutputDescriptor>& outputDesc, |
| 4596 | int delayMs, |
| 4597 | audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4598 | { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4599 | const StreamDescriptor& streamDesc = mStreams.valueFor(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4600 | if (device == AUDIO_DEVICE_NONE) { |
| 4601 | device = outputDesc->device(); |
| 4602 | } |
| 4603 | |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4604 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", |
| 4605 | stream, on, outputDesc->mMuteCount[stream], device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4606 | |
| 4607 | if (on) { |
| 4608 | if (outputDesc->mMuteCount[stream] == 0) { |
François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 4609 | if (streamDesc.canBeMuted() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4610 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4611 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4612 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4613 | } |
| 4614 | } |
| 4615 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 4616 | outputDesc->mMuteCount[stream]++; |
| 4617 | } else { |
| 4618 | if (outputDesc->mMuteCount[stream] == 0) { |
| 4619 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 4620 | return; |
| 4621 | } |
| 4622 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 4623 | checkAndSetVolume(stream, |
| 4624 | streamDesc.getVolumeIndex(device), |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4625 | outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4626 | device, |
| 4627 | delayMs); |
| 4628 | } |
| 4629 | } |
| 4630 | } |
| 4631 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4632 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4633 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4634 | { |
| 4635 | // if the stream pertains to sonification strategy and we are in call we must |
| 4636 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 4637 | // in the device used for phone strategy and play the tone if the selected device does not |
| 4638 | // interfere with the device used for phone strategy |
| 4639 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 4640 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4641 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4642 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 4643 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4644 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4645 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 4646 | stream, starting, outputDesc->mDevice, stateChange); |
| 4647 | if (outputDesc->mRefCount[stream]) { |
| 4648 | int muteCount = 1; |
| 4649 | if (stateChange) { |
| 4650 | muteCount = outputDesc->mRefCount[stream]; |
| 4651 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4652 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4653 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 4654 | for (int i = 0; i < muteCount; i++) { |
| 4655 | setStreamMute(stream, starting, mPrimaryOutput); |
| 4656 | } |
| 4657 | } else { |
| 4658 | ALOGV("handleIncallSonification() high visibility"); |
| 4659 | if (outputDesc->device() & |
| 4660 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 4661 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 4662 | for (int i = 0; i < muteCount; i++) { |
| 4663 | setStreamMute(stream, starting, mPrimaryOutput); |
| 4664 | } |
| 4665 | } |
| 4666 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4667 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 4668 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4669 | } else { |
| 4670 | mpClientInterface->stopTone(); |
| 4671 | } |
| 4672 | } |
| 4673 | } |
| 4674 | } |
| 4675 | } |
| 4676 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4677 | // --- SessionRoute class implementation |
| 4678 | void AudioPolicyManager::SessionRoute::log(const char* prefix) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4679 | ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d", |
| 4680 | prefix, mStreamType, mSource, mSession, |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4681 | mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE, |
| 4682 | mRefCount, mActivityCount); |
| 4683 | } |
| 4684 | |
| 4685 | // --- SessionRouteMap class implementation |
| 4686 | bool AudioPolicyManager::SessionRouteMap::hasRoute(audio_session_t session) |
| 4687 | { |
| 4688 | return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0; |
| 4689 | } |
| 4690 | |
Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 4691 | bool AudioPolicyManager::SessionRouteMap::hasRouteChanged(audio_session_t session) |
| 4692 | { |
| 4693 | if (indexOfKey(session) >= 0) { |
| 4694 | if (valueFor(session)->mChanged) { |
| 4695 | valueFor(session)->mChanged = false; |
| 4696 | return true; |
| 4697 | } |
| 4698 | } |
| 4699 | return false; |
| 4700 | } |
| 4701 | |
Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4702 | void AudioPolicyManager::SessionRouteMap::removeRoute(audio_session_t session) |
| 4703 | { |
| 4704 | sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0; |
| 4705 | if (route != 0) { |
| 4706 | ALOG_ASSERT(route->mRefCount > 0); |
| 4707 | --route->mRefCount; |
| 4708 | if (route->mRefCount <= 0) { |
| 4709 | removeItem(session); |
| 4710 | } |
| 4711 | } |
| 4712 | } |
| 4713 | |
| 4714 | int AudioPolicyManager::SessionRouteMap::incRouteActivity(audio_session_t session) |
| 4715 | { |
| 4716 | sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0; |
| 4717 | return route != 0 ? ++(route->mActivityCount) : -1; |
| 4718 | } |
| 4719 | |
| 4720 | int AudioPolicyManager::SessionRouteMap::decRouteActivity(audio_session_t session) |
| 4721 | { |
| 4722 | sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0; |
| 4723 | if (route != 0 && route->mActivityCount > 0) { |
| 4724 | return --(route->mActivityCount); |
| 4725 | } else { |
| 4726 | return -1; |
| 4727 | } |
| 4728 | } |
| 4729 | |
| 4730 | void AudioPolicyManager::SessionRouteMap::log(const char* caption) { |
| 4731 | ALOGI("%s ----", caption); |
| 4732 | for(size_t index = 0; index < size(); index++) { |
| 4733 | valueAt(index)->log(" "); |
| 4734 | } |
| 4735 | } |
| 4736 | |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4737 | void AudioPolicyManager::SessionRouteMap::addRoute(audio_session_t session, |
| 4738 | audio_stream_type_t streamType, |
| 4739 | audio_source_t source, |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4740 | sp<DeviceDescriptor> descriptor, |
| 4741 | uid_t uid) |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4742 | { |
| 4743 | if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) { |
| 4744 | ALOGE("Adding Output Route to InputRouteMap"); |
| 4745 | return; |
| 4746 | } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) { |
| 4747 | ALOGE("Adding Input Route to OutputRouteMap"); |
| 4748 | return; |
| 4749 | } |
| 4750 | |
| 4751 | sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0; |
| 4752 | |
| 4753 | if (route != 0) { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4754 | if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) || |
| 4755 | ((route->mDeviceDescriptor != 0) && |
| 4756 | ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) { |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4757 | route->mChanged = true; |
| 4758 | } |
| 4759 | route->mRefCount++; |
| 4760 | route->mDeviceDescriptor = descriptor; |
| 4761 | } else { |
Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4762 | route = new AudioPolicyManager::SessionRoute(session, streamType, source, descriptor, uid); |
Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 4763 | route->mRefCount++; |
| 4764 | add(session, route); |
| 4765 | if (descriptor != 0) { |
| 4766 | route->mChanged = true; |
| 4767 | } |
| 4768 | } |
| 4769 | } |
| 4770 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4771 | void AudioPolicyManager::defaultAudioPolicyConfig(void) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4772 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4773 | sp<HwModule> module; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4774 | sp<IOProfile> profile; |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 4775 | sp<DeviceDescriptor> defaultInputDevice = |
Eric Laurent | 7288ab8 | 2015-05-06 18:44:02 -0700 | [diff] [blame] | 4776 | new DeviceDescriptor(AUDIO_DEVICE_IN_BUILTIN_MIC); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4777 | mAvailableOutputDevices.add(mDefaultOutputDevice); |
| 4778 | mAvailableInputDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4779 | |
| 4780 | module = new HwModule("primary"); |
| 4781 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4782 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE); |
| 4783 | profile->attach(module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4784 | profile->mSamplingRates.add(44100); |
| 4785 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 4786 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4787 | profile->mSupportedDevices.add(mDefaultOutputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4788 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; |
| 4789 | module->mOutputProfiles.add(profile); |
| 4790 | |
Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4791 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK); |
| 4792 | profile->attach(module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4793 | profile->mSamplingRates.add(8000); |
| 4794 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 4795 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4796 | profile->mSupportedDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4797 | module->mInputProfiles.add(profile); |
| 4798 | |
| 4799 | mHwModules.add(module); |
| 4800 | } |
| 4801 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4802 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 4803 | { |
| 4804 | // flags to stream type mapping |
| 4805 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4806 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 4807 | } |
| 4808 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 4809 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 4810 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 4811 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4812 | return AUDIO_STREAM_TTS; |
| 4813 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4814 | |
| 4815 | // usage to stream type mapping |
| 4816 | switch (attr->usage) { |
| 4817 | case AUDIO_USAGE_MEDIA: |
| 4818 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4819 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 4820 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4821 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 4822 | if (isStreamActive(AUDIO_STREAM_ALARM)) { |
| 4823 | return AUDIO_STREAM_ALARM; |
| 4824 | } |
| 4825 | if (isStreamActive(AUDIO_STREAM_RING)) { |
| 4826 | return AUDIO_STREAM_RING; |
| 4827 | } |
| 4828 | if (isInCall()) { |
| 4829 | return AUDIO_STREAM_VOICE_CALL; |
| 4830 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4831 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4832 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 4833 | return AUDIO_STREAM_SYSTEM; |
| 4834 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 4835 | return AUDIO_STREAM_VOICE_CALL; |
| 4836 | |
| 4837 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 4838 | return AUDIO_STREAM_DTMF; |
| 4839 | |
| 4840 | case AUDIO_USAGE_ALARM: |
| 4841 | return AUDIO_STREAM_ALARM; |
| 4842 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 4843 | return AUDIO_STREAM_RING; |
| 4844 | |
| 4845 | case AUDIO_USAGE_NOTIFICATION: |
| 4846 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 4847 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 4848 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 4849 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 4850 | return AUDIO_STREAM_NOTIFICATION; |
| 4851 | |
| 4852 | case AUDIO_USAGE_UNKNOWN: |
| 4853 | default: |
| 4854 | return AUDIO_STREAM_MUSIC; |
| 4855 | } |
| 4856 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 4857 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4858 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) |
| 4859 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 4860 | // has flags that map to a strategy? |
| 4861 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 4862 | return true; |
| 4863 | } |
| 4864 | |
| 4865 | // has known usage? |
| 4866 | switch (paa->usage) { |
| 4867 | case AUDIO_USAGE_UNKNOWN: |
| 4868 | case AUDIO_USAGE_MEDIA: |
| 4869 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 4870 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 4871 | case AUDIO_USAGE_ALARM: |
| 4872 | case AUDIO_USAGE_NOTIFICATION: |
| 4873 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 4874 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 4875 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 4876 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 4877 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 4878 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 4879 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 4880 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 4881 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4882 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 4883 | break; |
| 4884 | default: |
| 4885 | return false; |
| 4886 | } |
| 4887 | return true; |
| 4888 | } |
| 4889 | |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4890 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor> outputDesc, |
| 4891 | routing_strategy strategy, uint32_t inPastMs, |
| 4892 | nsecs_t sysTime) const |
| 4893 | { |
| 4894 | if ((sysTime == 0) && (inPastMs != 0)) { |
| 4895 | sysTime = systemTime(); |
| 4896 | } |
| 4897 | for (int i = 0; i < (int)AUDIO_STREAM_CNT; i++) { |
| 4898 | if (i == AUDIO_STREAM_PATCH) { |
| 4899 | continue; |
| 4900 | } |
| 4901 | if (((getStrategy((audio_stream_type_t)i) == strategy) || |
| 4902 | (NUM_STRATEGIES == strategy)) && |
| 4903 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { |
| 4904 | return true; |
| 4905 | } |
| 4906 | } |
| 4907 | return false; |
| 4908 | } |
| 4909 | |
François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4910 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) |
| 4911 | { |
| 4912 | return mEngine->getForceUse(usage); |
| 4913 | } |
| 4914 | |
| 4915 | bool AudioPolicyManager::isInCall() |
| 4916 | { |
| 4917 | return isStateInCall(mEngine->getPhoneState()); |
| 4918 | } |
| 4919 | |
| 4920 | bool AudioPolicyManager::isStateInCall(int state) |
| 4921 | { |
| 4922 | return is_state_in_call(state); |
| 4923 | } |
| 4924 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4925 | }; // namespace android |