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