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