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