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