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