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 | |
| 27 | // A device mask for all audio input devices that are considered "virtual" when evaluating |
| 28 | // active inputs in getActiveInput() |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 29 | #define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_FM_TUNER) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 30 | // A device mask for all audio output devices that are considered "remote" when evaluating |
| 31 | // active output devices in isStreamActiveRemotely() |
| 32 | #define APM_AUDIO_OUT_DEVICE_REMOTE_ALL AUDIO_DEVICE_OUT_REMOTE_SUBMIX |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 33 | // A device mask for all audio input and output devices where matching inputs/outputs on device |
| 34 | // type alone is not enough: the address must match too |
| 35 | #define APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX | \ |
| 36 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 37 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 38 | #include <inttypes.h> |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 39 | #include <math.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 40 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 41 | #include <cutils/properties.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 42 | #include <utils/Log.h> |
| 43 | #include <hardware/audio.h> |
| 44 | #include <hardware/audio_effect.h> |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 45 | #include <media/AudioParameter.h> |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 46 | #include <media/AudioPolicyHelper.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 47 | #include <soundtrigger/SoundTrigger.h> |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 48 | #include "AudioPolicyManager.h" |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 49 | #include "audio_policy_conf.h" |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 50 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 51 | namespace android { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 52 | |
| 53 | // ---------------------------------------------------------------------------- |
| 54 | // AudioPolicyInterface implementation |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 57 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 58 | audio_policy_dev_state_t state, |
| 59 | const char *device_address, |
| 60 | const char *device_name) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 61 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 62 | return setDeviceConnectionStateInt(device, state, device_address, device_name); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 66 | audio_policy_dev_state_t state, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 67 | const char *device_address, |
| 68 | const char *device_name) |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 69 | { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 70 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", |
| 71 | - device, state, device_address, device_name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 72 | |
| 73 | // connect/disconnect only 1 device at a time |
| 74 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; |
| 75 | |
Paul McLean | e8610ba | 2015-02-06 13:53:36 -0800 | [diff] [blame] | 76 | sp<DeviceDescriptor> devDesc = getDeviceDescriptor(device, device_address, device_name); |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 77 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 78 | // handle output devices |
| 79 | if (audio_is_output_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 80 | SortedVector <audio_io_handle_t> outputs; |
| 81 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 82 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); |
| 83 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 84 | // save a copy of the opened output descriptors before any output is opened or closed |
| 85 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() |
| 86 | mPreviousOutputs = mOutputs; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 | switch (state) |
| 88 | { |
| 89 | // handle output device connection |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 90 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 91 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 92 | ALOGW("setDeviceConnectionState() device already connected: %x", device); |
| 93 | return INVALID_OPERATION; |
| 94 | } |
| 95 | ALOGV("setDeviceConnectionState() connecting device %x", device); |
| 96 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 97 | // register new device as available |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | index = mAvailableOutputDevices.add(devDesc); |
| 99 | if (index >= 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 100 | sp<HwModule> module = getModuleForDevice(device); |
Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 101 | if (module == 0) { |
| 102 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", |
| 103 | device); |
| 104 | mAvailableOutputDevices.remove(devDesc); |
| 105 | return INVALID_OPERATION; |
| 106 | } |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 107 | mAvailableOutputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 108 | } else { |
| 109 | return NO_MEMORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 112 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 113 | mAvailableOutputDevices.remove(devDesc); |
| 114 | return INVALID_OPERATION; |
| 115 | } |
| 116 | // outputs should never be empty here |
| 117 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" |
| 118 | "checkOutputsForDevice() returned no outputs but status OK"); |
| 119 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", |
| 120 | outputs.size()); |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 121 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 122 | // Send connect to HALs |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 123 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 124 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 125 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 126 | |
| 127 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 128 | // handle output device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 129 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 130 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 131 | ALOGW("setDeviceConnectionState() device not connected: %x", device); |
| 132 | return INVALID_OPERATION; |
| 133 | } |
| 134 | |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 135 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); |
| 136 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 137 | // Send Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 138 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 139 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 140 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 141 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 142 | // remove device from available output devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 143 | mAvailableOutputDevices.remove(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 144 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 145 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 146 | } break; |
| 147 | |
| 148 | default: |
| 149 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 150 | return BAD_VALUE; |
| 151 | } |
| 152 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 153 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP |
| 154 | // output is suspended before any tracks are moved to it |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 155 | checkA2dpSuspend(); |
| 156 | checkOutputForAllStrategies(); |
| 157 | // outputs must be closed after checkOutputForAllStrategies() is executed |
| 158 | if (!outputs.isEmpty()) { |
| 159 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 160 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | // close unused outputs after device disconnection or direct outputs that have been |
| 162 | // opened by checkOutputsForDevice() to query dynamic parameters |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 163 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 164 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && |
| 165 | (desc->mDirectOpenCount == 0))) { |
| 166 | closeOutput(outputs[i]); |
| 167 | } |
| 168 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 169 | // check again after closing A2DP output to reset mA2dpSuspended if needed |
| 170 | checkA2dpSuspend(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 174 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 175 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 176 | updateCallRouting(newDevice); |
| 177 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 178 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 179 | audio_io_handle_t output = mOutputs.keyAt(i); |
| 180 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 181 | audio_devices_t newDevice = getNewOutputDevice(mOutputs.keyAt(i), |
| 182 | true /*fromCache*/); |
| 183 | // do not force device change on duplicated output because if device is 0, it will |
| 184 | // also force a device 0 for the two outputs it is duplicated to which may override |
| 185 | // a valid device selection on those outputs. |
| 186 | bool force = !mOutputs.valueAt(i)->isDuplicated() |
| 187 | && (!deviceDistinguishesOnAddress(device) |
| 188 | // always force when disconnecting (a non-duplicated device) |
| 189 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); |
| 190 | setOutputDevice(output, newDevice, force, 0); |
| 191 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 194 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 195 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 196 | } // end if is output device |
| 197 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 198 | // handle input devices |
| 199 | if (audio_is_input_device(device)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 200 | SortedVector <audio_io_handle_t> inputs; |
| 201 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 202 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 203 | switch (state) |
| 204 | { |
| 205 | // handle input device connection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 206 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 207 | if (index >= 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 208 | ALOGW("setDeviceConnectionState() device already connected: %d", device); |
| 209 | return INVALID_OPERATION; |
| 210 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 211 | sp<HwModule> module = getModuleForDevice(device); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 212 | if (module == NULL) { |
| 213 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", |
| 214 | device); |
| 215 | return INVALID_OPERATION; |
| 216 | } |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 217 | if (checkInputsForDevice(device, state, inputs, devDesc->mAddress) != NO_ERROR) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 218 | return INVALID_OPERATION; |
| 219 | } |
| 220 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 221 | index = mAvailableInputDevices.add(devDesc); |
| 222 | if (index >= 0) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 223 | mAvailableInputDevices[index]->attach(module); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 224 | } else { |
| 225 | return NO_MEMORY; |
| 226 | } |
Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 227 | |
| 228 | // Set connect to HALs |
| 229 | AudioParameter param = AudioParameter(devDesc->mAddress); |
| 230 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_CONNECT), device); |
| 231 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 232 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 233 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 234 | |
| 235 | // handle input device disconnection |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 236 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 237 | if (index < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 238 | ALOGW("setDeviceConnectionState() device not connected: %d", device); |
| 239 | return INVALID_OPERATION; |
| 240 | } |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 241 | |
| 242 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); |
| 243 | |
| 244 | // Set Disconnect to HALs |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 245 | AudioParameter param = AudioParameter(devDesc->mAddress); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 246 | param.addInt(String8(AUDIO_PARAMETER_DEVICE_DISCONNECT), device); |
| 247 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); |
| 248 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 249 | checkInputsForDevice(device, state, inputs, devDesc->mAddress); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 250 | mAvailableInputDevices.remove(devDesc); |
Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 251 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 252 | } break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 253 | |
| 254 | default: |
| 255 | ALOGE("setDeviceConnectionState() invalid state: %x", state); |
| 256 | return BAD_VALUE; |
| 257 | } |
| 258 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 259 | closeAllInputs(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 260 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 261 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 262 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 263 | updateCallRouting(newDevice); |
| 264 | } |
| 265 | |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 266 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 | return NO_ERROR; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 268 | } // end if is input device |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 269 | |
| 270 | ALOGW("setDeviceConnectionState() invalid device: %x", device); |
| 271 | return BAD_VALUE; |
| 272 | } |
| 273 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 274 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 275 | const char *device_address) |
| 276 | { |
Paul McLean | e8610ba | 2015-02-06 13:53:36 -0800 | [diff] [blame] | 277 | sp<DeviceDescriptor> devDesc = getDeviceDescriptor(device, device_address, ""); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 278 | DeviceVector *deviceVector; |
| 279 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 280 | if (audio_is_output_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 281 | deviceVector = &mAvailableOutputDevices; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 282 | } else if (audio_is_input_device(device)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 283 | deviceVector = &mAvailableInputDevices; |
| 284 | } else { |
| 285 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); |
| 286 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 289 | ssize_t index = deviceVector->indexOf(devDesc); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 290 | if (index >= 0) { |
| 291 | return AUDIO_POLICY_DEVICE_STATE_AVAILABLE; |
| 292 | } else { |
| 293 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; |
| 294 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 297 | sp<DeviceDescriptor> AudioPolicyManager::getDeviceDescriptor(const audio_devices_t device, |
| 298 | const char *device_address, |
| 299 | const char *device_name) |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 300 | { |
| 301 | String8 address = (device_address == NULL) ? String8("") : String8(device_address); |
| 302 | // handle legacy remote submix case where the address was not always specified |
| 303 | if (deviceDistinguishesOnAddress(device) && (address.length() == 0)) { |
| 304 | address = String8("0"); |
| 305 | } |
| 306 | |
| 307 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 308 | if (mHwModules[i]->mHandle == 0) { |
| 309 | continue; |
| 310 | } |
| 311 | DeviceVector deviceList = |
| 312 | mHwModules[i]->mDeclaredDevices.getDevicesFromTypeAddr(device, address); |
| 313 | if (!deviceList.isEmpty()) { |
| 314 | return deviceList.itemAt(0); |
| 315 | } |
| 316 | deviceList = mHwModules[i]->mDeclaredDevices.getDevicesFromType(device); |
| 317 | if (!deviceList.isEmpty()) { |
| 318 | return deviceList.itemAt(0); |
| 319 | } |
| 320 | } |
| 321 | |
Paul McLean | e8610ba | 2015-02-06 13:53:36 -0800 | [diff] [blame] | 322 | sp<DeviceDescriptor> devDesc = |
| 323 | new DeviceDescriptor(String8(device_name != NULL ? device_name : ""), device); |
Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 324 | devDesc->mAddress = address; |
| 325 | return devDesc; |
| 326 | } |
| 327 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 328 | void AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, int delayMs) |
| 329 | { |
| 330 | bool createTxPatch = false; |
| 331 | struct audio_patch patch; |
| 332 | patch.num_sources = 1; |
| 333 | patch.num_sinks = 1; |
| 334 | status_t status; |
| 335 | audio_patch_handle_t afPatchHandle; |
| 336 | DeviceVector deviceList; |
| 337 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 338 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 339 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); |
| 340 | |
| 341 | // release existing RX patch if any |
| 342 | if (mCallRxPatch != 0) { |
| 343 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 344 | mCallRxPatch.clear(); |
| 345 | } |
| 346 | // release TX patch if any |
| 347 | if (mCallTxPatch != 0) { |
| 348 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 349 | mCallTxPatch.clear(); |
| 350 | } |
| 351 | |
| 352 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls |
| 353 | // via setOutputDevice() on primary output. |
| 354 | // Otherwise, create two audio patches for TX and RX path. |
| 355 | if (availablePrimaryOutputDevices() & rxDevice) { |
| 356 | setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); |
| 357 | // If the TX device is also on the primary HW module, setOutputDevice() will take care |
| 358 | // of it due to legacy implementation. If not, create a patch. |
| 359 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) |
| 360 | == AUDIO_DEVICE_NONE) { |
| 361 | createTxPatch = true; |
| 362 | } |
| 363 | } else { |
| 364 | // create RX path audio patch |
| 365 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); |
| 366 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 367 | "updateCallRouting() selected device not in output device list"); |
| 368 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); |
| 369 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); |
| 370 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 371 | "updateCallRouting() no telephony RX device"); |
| 372 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); |
| 373 | |
| 374 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 375 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 376 | |
| 377 | // request to reuse existing output stream if one is already opened to reach the RX device |
| 378 | SortedVector<audio_io_handle_t> outputs = |
| 379 | getOutputsForDevice(rxDevice, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 380 | audio_io_handle_t output = selectOutput(outputs, |
| 381 | AUDIO_OUTPUT_FLAG_NONE, |
| 382 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 383 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 384 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 385 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 386 | "updateCallRouting() RX device output is duplicated"); |
| 387 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 388 | patch.num_sources = 2; |
| 389 | } |
| 390 | |
| 391 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 392 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 393 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", |
| 394 | status); |
| 395 | if (status == NO_ERROR) { |
| 396 | mCallRxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 397 | &patch, mUidCached); |
| 398 | mCallRxPatch->mAfPatchHandle = afPatchHandle; |
| 399 | mCallRxPatch->mUid = mUidCached; |
| 400 | } |
| 401 | createTxPatch = true; |
| 402 | } |
| 403 | if (createTxPatch) { |
| 404 | |
| 405 | struct audio_patch patch; |
| 406 | patch.num_sources = 1; |
| 407 | patch.num_sinks = 1; |
| 408 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); |
| 409 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 410 | "updateCallRouting() selected device not in input device list"); |
| 411 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); |
| 412 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); |
| 413 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); |
| 414 | ALOG_ASSERT(!deviceList.isEmpty(), |
| 415 | "updateCallRouting() no telephony TX device"); |
| 416 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); |
| 417 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); |
| 418 | |
| 419 | SortedVector<audio_io_handle_t> outputs = |
| 420 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 421 | audio_io_handle_t output = selectOutput(outputs, |
| 422 | AUDIO_OUTPUT_FLAG_NONE, |
| 423 | AUDIO_FORMAT_INVALID); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 424 | // request to reuse existing output stream if one is already opened to reach the TX |
| 425 | // path output device |
| 426 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 427 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 428 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 429 | "updateCallRouting() RX device output is duplicated"); |
| 430 | outputDesc->toAudioPortConfig(&patch.sources[1]); |
| 431 | patch.num_sources = 2; |
| 432 | } |
| 433 | |
| 434 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 435 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, 0); |
| 436 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", |
| 437 | status); |
| 438 | if (status == NO_ERROR) { |
| 439 | mCallTxPatch = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 440 | &patch, mUidCached); |
| 441 | mCallTxPatch->mAfPatchHandle = afPatchHandle; |
| 442 | mCallTxPatch->mUid = mUidCached; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 447 | void AudioPolicyManager::setPhoneState(audio_mode_t state) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 448 | { |
| 449 | ALOGV("setPhoneState() state %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 450 | if (state < 0 || state >= AUDIO_MODE_CNT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 451 | ALOGW("setPhoneState() invalid state %d", state); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | if (state == mPhoneState ) { |
| 456 | ALOGW("setPhoneState() setting same state %d", state); |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | // if leaving call state, handle special case of active streams |
| 461 | // pertaining to sonification strategy see handleIncallSonification() |
| 462 | if (isInCall()) { |
| 463 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 464 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 465 | if (stream == AUDIO_STREAM_PATCH) { |
| 466 | continue; |
| 467 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 468 | handleIncallSonification((audio_stream_type_t)stream, false, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 469 | } |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 470 | |
| 471 | // force reevaluating accessibility routing when call starts |
| 472 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // store previous phone state for management of sonification strategy below |
| 476 | int oldState = mPhoneState; |
| 477 | mPhoneState = state; |
| 478 | bool force = false; |
| 479 | |
| 480 | // are we entering or starting a call |
| 481 | if (!isStateInCall(oldState) && isStateInCall(state)) { |
| 482 | ALOGV(" Entering call in setPhoneState()"); |
| 483 | // force routing command to audio hardware when starting a call |
| 484 | // even if no device change is needed |
| 485 | force = true; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 486 | for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 487 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 488 | ApmGains::sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 489 | } |
| 490 | } else if (isStateInCall(oldState) && !isStateInCall(state)) { |
| 491 | ALOGV(" Exiting call in setPhoneState()"); |
| 492 | // force routing command to audio hardware when exiting a call |
| 493 | // even if no device change is needed |
| 494 | force = true; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 495 | for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 496 | mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 497 | ApmGains::sVolumeProfiles[AUDIO_STREAM_DTMF][j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 498 | } |
| 499 | } else if (isStateInCall(state) && (state != oldState)) { |
| 500 | ALOGV(" Switching between telephony and VoIP in setPhoneState()"); |
| 501 | // force routing command to audio hardware when switching between telephony and VoIP |
| 502 | // even if no device change is needed |
| 503 | force = true; |
| 504 | } |
| 505 | |
| 506 | // check for device and output changes triggered by new phone state |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 507 | checkA2dpSuspend(); |
| 508 | checkOutputForAllStrategies(); |
| 509 | updateDevicesAndOutputs(); |
| 510 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 511 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 512 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 513 | int delayMs = 0; |
| 514 | if (isStateInCall(state)) { |
| 515 | nsecs_t sysTime = systemTime(); |
| 516 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 517 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 518 | // mute media and sonification strategies and delay device switch by the largest |
| 519 | // latency of any output where either strategy is active. |
| 520 | // This avoid sending the ring tone or music tail into the earpiece or headset. |
| 521 | if ((desc->isStrategyActive(STRATEGY_MEDIA, |
| 522 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 523 | sysTime) || |
| 524 | desc->isStrategyActive(STRATEGY_SONIFICATION, |
| 525 | SONIFICATION_HEADSET_MUSIC_DELAY, |
| 526 | sysTime)) && |
| 527 | (delayMs < (int)desc->mLatency*2)) { |
| 528 | delayMs = desc->mLatency*2; |
| 529 | } |
| 530 | setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i)); |
| 531 | setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 532 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); |
| 533 | setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i)); |
| 534 | setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS, |
| 535 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); |
| 536 | } |
| 537 | } |
| 538 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 539 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, |
| 540 | // the device returned is not necessarily reachable via this output |
| 541 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); |
| 542 | // force routing command to audio hardware when ending call |
| 543 | // even if no device change is needed |
| 544 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { |
| 545 | rxDevice = hwOutputDesc->device(); |
| 546 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 547 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 548 | if (state == AUDIO_MODE_IN_CALL) { |
| 549 | updateCallRouting(rxDevice, delayMs); |
| 550 | } else if (oldState == AUDIO_MODE_IN_CALL) { |
| 551 | if (mCallRxPatch != 0) { |
| 552 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); |
| 553 | mCallRxPatch.clear(); |
| 554 | } |
| 555 | if (mCallTxPatch != 0) { |
| 556 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); |
| 557 | mCallTxPatch.clear(); |
| 558 | } |
| 559 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 560 | } else { |
| 561 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); |
| 562 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 563 | // if entering in call state, handle special case of active streams |
| 564 | // pertaining to sonification strategy see handleIncallSonification() |
| 565 | if (isStateInCall(state)) { |
| 566 | ALOGV("setPhoneState() in call state management: new state is %d", state); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 567 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 568 | if (stream == AUDIO_STREAM_PATCH) { |
| 569 | continue; |
| 570 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 571 | handleIncallSonification((audio_stream_type_t)stream, true, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
| 575 | // 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] | 576 | if (state == AUDIO_MODE_RINGTONE && |
| 577 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 578 | mLimitRingtoneVolume = true; |
| 579 | } else { |
| 580 | mLimitRingtoneVolume = false; |
| 581 | } |
| 582 | } |
| 583 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 584 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 585 | audio_policy_forced_cfg_t config) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 586 | { |
| 587 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState); |
| 588 | |
| 589 | bool forceVolumeReeval = false; |
| 590 | switch(usage) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 591 | case AUDIO_POLICY_FORCE_FOR_COMMUNICATION: |
| 592 | if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO && |
| 593 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 594 | ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config); |
| 595 | return; |
| 596 | } |
| 597 | forceVolumeReeval = true; |
| 598 | mForceUse[usage] = config; |
| 599 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 600 | case AUDIO_POLICY_FORCE_FOR_MEDIA: |
| 601 | if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP && |
| 602 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 603 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 604 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE && |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 605 | config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 606 | ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config); |
| 607 | return; |
| 608 | } |
| 609 | mForceUse[usage] = config; |
| 610 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 611 | case AUDIO_POLICY_FORCE_FOR_RECORD: |
| 612 | if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 613 | config != AUDIO_POLICY_FORCE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 614 | ALOGW("setForceUse() invalid config %d for FOR_RECORD", config); |
| 615 | return; |
| 616 | } |
| 617 | mForceUse[usage] = config; |
| 618 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 619 | case AUDIO_POLICY_FORCE_FOR_DOCK: |
| 620 | if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK && |
| 621 | config != AUDIO_POLICY_FORCE_BT_DESK_DOCK && |
| 622 | config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY && |
| 623 | config != AUDIO_POLICY_FORCE_ANALOG_DOCK && |
| 624 | config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 625 | ALOGW("setForceUse() invalid config %d for FOR_DOCK", config); |
| 626 | } |
| 627 | forceVolumeReeval = true; |
| 628 | mForceUse[usage] = config; |
| 629 | break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 630 | case AUDIO_POLICY_FORCE_FOR_SYSTEM: |
| 631 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 632 | config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 633 | ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config); |
| 634 | } |
| 635 | forceVolumeReeval = true; |
| 636 | mForceUse[usage] = config; |
| 637 | break; |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 638 | case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO: |
| 639 | if (config != AUDIO_POLICY_FORCE_NONE && |
| 640 | config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) { |
| 641 | ALOGW("setForceUse() invalid config %d forHDMI_SYSTEM_AUDIO", config); |
| 642 | } |
| 643 | mForceUse[usage] = config; |
| 644 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 645 | default: |
| 646 | ALOGW("setForceUse() invalid usage %d", usage); |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | // check for device and output changes triggered by new force usage |
| 651 | checkA2dpSuspend(); |
| 652 | checkOutputForAllStrategies(); |
| 653 | updateDevicesAndOutputs(); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 654 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
| 655 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); |
| 656 | updateCallRouting(newDevice); |
| 657 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 658 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 659 | audio_io_handle_t output = mOutputs.keyAt(i); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 660 | audio_devices_t newDevice = getNewOutputDevice(output, true /*fromCache*/); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 661 | if ((mPhoneState != AUDIO_MODE_IN_CALL) || (output != mPrimaryOutput)) { |
| 662 | setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE)); |
| 663 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 664 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { |
| 665 | applyStreamVolumes(output, newDevice, 0, true); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | audio_io_handle_t activeInput = getActiveInput(); |
| 670 | if (activeInput != 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 671 | setInputDevice(activeInput, getNewInputDevice(activeInput)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | } |
| 675 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 676 | 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] | 677 | { |
| 678 | return mForceUse[usage]; |
| 679 | } |
| 680 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 681 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 682 | { |
| 683 | ALOGV("setSystemProperty() property %s, value %s", property, value); |
| 684 | } |
| 685 | |
| 686 | // Find a direct output profile compatible with the parameters passed, even if the input flags do |
| 687 | // not explicitly request a direct output |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 688 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 689 | audio_devices_t device, |
| 690 | uint32_t samplingRate, |
| 691 | audio_format_t format, |
| 692 | audio_channel_mask_t channelMask, |
| 693 | audio_output_flags_t flags) |
| 694 | { |
| 695 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 696 | if (mHwModules[i]->mHandle == 0) { |
| 697 | continue; |
| 698 | } |
| 699 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 700 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 701 | bool found = profile->isCompatibleProfile(device, String8(""), samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 702 | NULL /*updatedSamplingRate*/, format, channelMask, |
| 703 | flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD ? |
| 704 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD : AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 705 | if (found && (mAvailableOutputDevices.types() & profile->mSupportedDevices.types())) { |
| 706 | return profile; |
| 707 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | return 0; |
| 711 | } |
| 712 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 713 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 714 | uint32_t samplingRate, |
| 715 | audio_format_t format, |
| 716 | audio_channel_mask_t channelMask, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 717 | audio_output_flags_t flags, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 718 | const audio_offload_info_t *offloadInfo) |
| 719 | { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 720 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 721 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 722 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", |
| 723 | device, stream, samplingRate, format, channelMask, flags); |
| 724 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 725 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, |
| 726 | stream, samplingRate,format, channelMask, |
| 727 | flags, offloadInfo); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 730 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, |
| 731 | audio_io_handle_t *output, |
| 732 | audio_session_t session, |
| 733 | audio_stream_type_t *stream, |
| 734 | uint32_t samplingRate, |
| 735 | audio_format_t format, |
| 736 | audio_channel_mask_t channelMask, |
| 737 | audio_output_flags_t flags, |
| 738 | const audio_offload_info_t *offloadInfo) |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 739 | { |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 740 | audio_attributes_t attributes; |
| 741 | if (attr != NULL) { |
| 742 | if (!isValidAttributes(attr)) { |
| 743 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", |
| 744 | attr->usage, attr->content_type, attr->flags, |
| 745 | attr->tags); |
| 746 | return BAD_VALUE; |
| 747 | } |
| 748 | attributes = *attr; |
| 749 | } else { |
| 750 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { |
| 751 | ALOGE("getOutputForAttr(): invalid stream type"); |
| 752 | return BAD_VALUE; |
| 753 | } |
| 754 | stream_type_to_audio_attributes(*stream, &attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 755 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 756 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 757 | for (size_t i = 0; i < mPolicyMixes.size(); i++) { |
| 758 | sp<AudioOutputDescriptor> desc; |
| 759 | if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_PLAYERS) { |
| 760 | for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) { |
| 761 | if ((RULE_MATCH_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule && |
| 762 | mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage == attributes.usage) || |
| 763 | (RULE_EXCLUDE_ATTRIBUTE_USAGE == mPolicyMixes[i]->mMix.mCriteria[j].mRule && |
| 764 | mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mUsage != attributes.usage)) { |
| 765 | desc = mPolicyMixes[i]->mOutput; |
| 766 | break; |
| 767 | } |
| 768 | if (strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 && |
| 769 | strncmp(attributes.tags + strlen("addr="), |
| 770 | mPolicyMixes[i]->mMix.mRegistrationId.string(), |
| 771 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) { |
| 772 | desc = mPolicyMixes[i]->mOutput; |
| 773 | break; |
| 774 | } |
| 775 | } |
| 776 | } else if (mPolicyMixes[i]->mMix.mMixType == MIX_TYPE_RECORDERS) { |
| 777 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE && |
| 778 | strncmp(attributes.tags, "addr=", strlen("addr=")) == 0 && |
| 779 | strncmp(attributes.tags + strlen("addr="), |
| 780 | mPolicyMixes[i]->mMix.mRegistrationId.string(), |
| 781 | AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - strlen("addr=") - 1) == 0) { |
| 782 | desc = mPolicyMixes[i]->mOutput; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 783 | } |
| 784 | } |
| 785 | if (desc != 0) { |
| 786 | if (!audio_is_linear_pcm(format)) { |
| 787 | return BAD_VALUE; |
| 788 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 789 | desc->mPolicyMix = &mPolicyMixes[i]->mMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 790 | *stream = streamTypefromAttributesInt(&attributes); |
| 791 | *output = desc->mIoHandle; |
| 792 | ALOGV("getOutputForAttr() returns output %d", *output); |
| 793 | return NO_ERROR; |
| 794 | } |
| 795 | } |
| 796 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { |
| 797 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); |
| 798 | return BAD_VALUE; |
| 799 | } |
| 800 | |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 801 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x", |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 802 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 803 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 804 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 805 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 806 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 807 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 808 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); |
| 809 | } |
| 810 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 811 | 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] | 812 | device, samplingRate, format, channelMask, flags); |
| 813 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 814 | *stream = streamTypefromAttributesInt(&attributes); |
| 815 | *output = getOutputForDevice(device, session, *stream, |
| 816 | samplingRate, format, channelMask, |
| 817 | flags, offloadInfo); |
| 818 | if (*output == AUDIO_IO_HANDLE_NONE) { |
| 819 | return INVALID_OPERATION; |
| 820 | } |
| 821 | return NO_ERROR; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( |
| 825 | audio_devices_t device, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 826 | audio_session_t session __unused, |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 827 | audio_stream_type_t stream, |
| 828 | uint32_t samplingRate, |
| 829 | audio_format_t format, |
| 830 | audio_channel_mask_t channelMask, |
| 831 | audio_output_flags_t flags, |
| 832 | const audio_offload_info_t *offloadInfo) |
| 833 | { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 834 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 835 | uint32_t latency = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 836 | status_t status; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 837 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 838 | #ifdef AUDIO_POLICY_TEST |
| 839 | if (mCurOutput != 0) { |
| 840 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", |
| 841 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); |
| 842 | |
| 843 | if (mTestOutputs[mCurOutput] == 0) { |
| 844 | ALOGV("getOutput() opening test output"); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 845 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 846 | outputDesc->mDevice = mTestDevice; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 847 | outputDesc->mLatency = mTestLatencyMs; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 848 | outputDesc->mFlags = |
| 849 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 850 | outputDesc->mRefCount[stream] = 0; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 851 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 852 | config.sample_rate = mTestSamplingRate; |
| 853 | config.channel_mask = mTestChannels; |
| 854 | config.format = mTestFormat; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 855 | if (offloadInfo != NULL) { |
| 856 | config.offload_info = *offloadInfo; |
| 857 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 858 | status = mpClientInterface->openOutput(0, |
| 859 | &mTestOutputs[mCurOutput], |
| 860 | &config, |
| 861 | &outputDesc->mDevice, |
| 862 | String8(""), |
| 863 | &outputDesc->mLatency, |
| 864 | outputDesc->mFlags); |
| 865 | if (status == NO_ERROR) { |
| 866 | outputDesc->mSamplingRate = config.sample_rate; |
| 867 | outputDesc->mFormat = config.format; |
| 868 | outputDesc->mChannelMask = config.channel_mask; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 869 | AudioParameter outputCmd = AudioParameter(); |
| 870 | outputCmd.addInt(String8("set_id"),mCurOutput); |
| 871 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); |
| 872 | addOutput(mTestOutputs[mCurOutput], outputDesc); |
| 873 | } |
| 874 | } |
| 875 | return mTestOutputs[mCurOutput]; |
| 876 | } |
| 877 | #endif //AUDIO_POLICY_TEST |
| 878 | |
| 879 | // open a direct output if required by specified parameters |
| 880 | //force direct flag if offload flag is set: offloading implies a direct output stream |
| 881 | // and all common behaviors are driven by checking only the direct flag |
| 882 | // this should normally be set appropriately in the policy configuration file |
| 883 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 884 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 885 | } |
Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 886 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { |
| 887 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); |
| 888 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 889 | // only allow deep buffering for music stream type |
| 890 | if (stream != AUDIO_STREAM_MUSIC) { |
| 891 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); |
| 892 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 893 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 894 | sp<IOProfile> profile; |
| 895 | |
| 896 | // 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] | 897 | // and not explicitly requested |
| 898 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && |
| 899 | audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE && |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 900 | audio_channel_count_from_out_mask(channelMask) <= 2) { |
| 901 | goto non_direct_output; |
| 902 | } |
| 903 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 904 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 905 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 906 | // detects there is an active non offloadable effect. |
| 907 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 908 | // This may prevent offloading in rare situations where effects are left active by apps |
| 909 | // in the background. |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 910 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 911 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || |
| 912 | !isNonOffloadableEffectEnabled()) { |
| 913 | profile = getProfileForDirectOutput(device, |
| 914 | samplingRate, |
| 915 | format, |
| 916 | channelMask, |
| 917 | (audio_output_flags_t)flags); |
| 918 | } |
| 919 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 920 | if (profile != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 921 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 922 | |
| 923 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 924 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 925 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { |
| 926 | outputDesc = desc; |
| 927 | // reuse direct output if currently open and configured with same parameters |
| 928 | if ((samplingRate == outputDesc->mSamplingRate) && |
| 929 | (format == outputDesc->mFormat) && |
| 930 | (channelMask == outputDesc->mChannelMask)) { |
| 931 | outputDesc->mDirectOpenCount++; |
| 932 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); |
| 933 | return mOutputs.keyAt(i); |
| 934 | } |
| 935 | } |
| 936 | } |
| 937 | // close direct output if currently open and configured with different parameters |
| 938 | if (outputDesc != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 939 | closeOutput(outputDesc->mIoHandle); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 940 | } |
| 941 | outputDesc = new AudioOutputDescriptor(profile); |
| 942 | outputDesc->mDevice = device; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | outputDesc->mLatency = 0; |
| 944 | outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 945 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 946 | config.sample_rate = samplingRate; |
| 947 | config.channel_mask = channelMask; |
| 948 | config.format = format; |
Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 949 | if (offloadInfo != NULL) { |
| 950 | config.offload_info = *offloadInfo; |
| 951 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 952 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 953 | &output, |
| 954 | &config, |
| 955 | &outputDesc->mDevice, |
| 956 | String8(""), |
| 957 | &outputDesc->mLatency, |
| 958 | outputDesc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 959 | |
| 960 | // only accept an output with the requested parameters |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 961 | if (status != NO_ERROR || |
| 962 | (samplingRate != 0 && samplingRate != config.sample_rate) || |
| 963 | (format != AUDIO_FORMAT_DEFAULT && format != config.format) || |
| 964 | (channelMask != 0 && channelMask != config.channel_mask)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 965 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," |
| 966 | "format %d %d, channelMask %04x %04x", output, samplingRate, |
| 967 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, |
| 968 | outputDesc->mChannelMask); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 969 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 970 | mpClientInterface->closeOutput(output); |
| 971 | } |
Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 972 | // fall back to mixer output if possible when the direct output could not be open |
| 973 | if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) { |
| 974 | goto non_direct_output; |
| 975 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 976 | // fall back to mixer output if possible when the direct output could not be open |
| 977 | if (audio_is_linear_pcm(format) && samplingRate <= MAX_MIXER_SAMPLING_RATE) { |
| 978 | goto non_direct_output; |
| 979 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 980 | return AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 981 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 982 | outputDesc->mSamplingRate = config.sample_rate; |
| 983 | outputDesc->mChannelMask = config.channel_mask; |
| 984 | outputDesc->mFormat = config.format; |
| 985 | outputDesc->mRefCount[stream] = 0; |
| 986 | outputDesc->mStopTime[stream] = 0; |
| 987 | outputDesc->mDirectOpenCount = 1; |
| 988 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 989 | audio_io_handle_t srcOutput = getOutputForEffect(); |
| 990 | addOutput(output, outputDesc); |
| 991 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 992 | if (dstOutput == output) { |
| 993 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput); |
| 994 | } |
| 995 | mPreviousOutputs = mOutputs; |
| 996 | ALOGV("getOutput() returns new direct output %d", output); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 997 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 998 | return output; |
| 999 | } |
| 1000 | |
Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1001 | non_direct_output: |
| 1002 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1003 | // ignoring channel mask due to downmix capability in mixer |
| 1004 | |
| 1005 | // open a non direct output |
| 1006 | |
| 1007 | // for non direct outputs, only PCM is supported |
| 1008 | if (audio_is_linear_pcm(format)) { |
| 1009 | // get which output is suitable for the specified stream. The actual |
| 1010 | // routing change will happen when startOutput() will be called |
| 1011 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); |
| 1012 | |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1013 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier |
| 1014 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); |
| 1015 | output = selectOutput(outputs, flags, format); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1016 | } |
| 1017 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," |
| 1018 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); |
| 1019 | |
| 1020 | ALOGV("getOutput() returns output %d", output); |
| 1021 | |
| 1022 | return output; |
| 1023 | } |
| 1024 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1025 | 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] | 1026 | audio_output_flags_t flags, |
| 1027 | audio_format_t format) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1028 | { |
| 1029 | // select one output among several that provide a path to a particular device or set of |
| 1030 | // devices (the list was previously build by getOutputsForDevice()). |
| 1031 | // The priority is as follows: |
| 1032 | // 1: the output with the highest number of requested policy flags |
| 1033 | // 2: the primary output |
| 1034 | // 3: the first output in the list |
| 1035 | |
| 1036 | if (outputs.size() == 0) { |
| 1037 | return 0; |
| 1038 | } |
| 1039 | if (outputs.size() == 1) { |
| 1040 | return outputs[0]; |
| 1041 | } |
| 1042 | |
| 1043 | int maxCommonFlags = 0; |
| 1044 | audio_io_handle_t outputFlags = 0; |
| 1045 | audio_io_handle_t outputPrimary = 0; |
| 1046 | |
| 1047 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1048 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | if (!outputDesc->isDuplicated()) { |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1050 | // if a valid format is specified, skip output if not compatible |
| 1051 | if (format != AUDIO_FORMAT_INVALID) { |
| 1052 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
| 1053 | if (format != outputDesc->mFormat) { |
| 1054 | continue; |
| 1055 | } |
| 1056 | } else if (!audio_is_linear_pcm(format)) { |
| 1057 | continue; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1061 | int commonFlags = popcount(outputDesc->mProfile->mFlags & flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1062 | if (commonFlags > maxCommonFlags) { |
| 1063 | outputFlags = outputs[i]; |
| 1064 | maxCommonFlags = commonFlags; |
| 1065 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); |
| 1066 | } |
| 1067 | if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 1068 | outputPrimary = outputs[i]; |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | if (outputFlags != 0) { |
| 1074 | return outputFlags; |
| 1075 | } |
| 1076 | if (outputPrimary != 0) { |
| 1077 | return outputPrimary; |
| 1078 | } |
| 1079 | |
| 1080 | return outputs[0]; |
| 1081 | } |
| 1082 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1083 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1084 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1085 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1086 | { |
| 1087 | ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session); |
| 1088 | ssize_t index = mOutputs.indexOfKey(output); |
| 1089 | if (index < 0) { |
| 1090 | ALOGW("startOutput() unknown output %d", output); |
| 1091 | return BAD_VALUE; |
| 1092 | } |
| 1093 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1094 | // cannot start playback of STREAM_TTS if any other output is being used |
| 1095 | uint32_t beaconMuteLatency = 0; |
| 1096 | if (stream == AUDIO_STREAM_TTS) { |
| 1097 | ALOGV("\t found BEACON stream"); |
| 1098 | if (isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { |
| 1099 | return INVALID_OPERATION; |
| 1100 | } else { |
| 1101 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); |
| 1102 | } |
| 1103 | } else { |
| 1104 | // some playback other than beacon starts |
| 1105 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); |
| 1106 | } |
| 1107 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1108 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1109 | |
| 1110 | // increment usage count for this stream on the requested output: |
| 1111 | // NOTE that the usage count is the same for duplicated output and hardware output which is |
| 1112 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() |
| 1113 | outputDesc->changeRefCount(stream, 1); |
| 1114 | |
| 1115 | if (outputDesc->mRefCount[stream] == 1) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1116 | // starting an output being rerouted? |
| 1117 | audio_devices_t newDevice; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1118 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1119 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 1120 | } else { |
| 1121 | newDevice = getNewOutputDevice(output, false /*fromCache*/); |
| 1122 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1123 | routing_strategy strategy = getStrategy(stream); |
| 1124 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1125 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || |
| 1126 | (beaconMuteLatency > 0); |
| 1127 | uint32_t waitMs = beaconMuteLatency; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1128 | bool force = false; |
| 1129 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1130 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1131 | if (desc != outputDesc) { |
| 1132 | // force a device change if any other output is managed by the same hw |
| 1133 | // module and has a current device selection that differs from selected device. |
| 1134 | // In this case, the audio HAL must receive the new device selection so that it can |
| 1135 | // change the device currently selected by the other active output. |
| 1136 | if (outputDesc->sharesHwModuleWith(desc) && |
| 1137 | desc->device() != newDevice) { |
| 1138 | force = true; |
| 1139 | } |
| 1140 | // 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] | 1141 | // a notification so that audio focus effect can propagate, or that a mute/unmute |
| 1142 | // event occurred for beacon |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1143 | uint32_t latency = desc->latency(); |
| 1144 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { |
| 1145 | waitMs = latency; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | uint32_t muteWaitMs = setOutputDevice(output, newDevice, force); |
| 1150 | |
| 1151 | // handle special case for sonification while in call |
| 1152 | if (isInCall()) { |
| 1153 | handleIncallSonification(stream, true, false); |
| 1154 | } |
| 1155 | |
| 1156 | // apply volume rules for current stream and device if necessary |
| 1157 | checkAndSetVolume(stream, |
| 1158 | mStreams[stream].getVolumeIndex(newDevice), |
| 1159 | output, |
| 1160 | newDevice); |
| 1161 | |
| 1162 | // update the outputs if starting an output with a stream that can affect notification |
| 1163 | // routing |
| 1164 | handleNotificationRoutingForStream(stream); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1165 | |
| 1166 | // Automatically enable the remote submix input when output is started on a re routing mix |
| 1167 | // of type MIX_TYPE_RECORDERS |
| 1168 | if (audio_is_remote_submix_device(newDevice) && outputDesc->mPolicyMix != NULL && |
| 1169 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1170 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1171 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1172 | outputDesc->mPolicyMix->mRegistrationId, |
| 1173 | "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1174 | } |
| 1175 | |
Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1176 | // force reevaluating accessibility routing when ringtone or alarm starts |
| 1177 | if (strategy == STRATEGY_SONIFICATION) { |
| 1178 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); |
| 1179 | } |
| 1180 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1181 | if (waitMs > muteWaitMs) { |
| 1182 | usleep((waitMs - muteWaitMs) * 2 * 1000); |
| 1183 | } |
| 1184 | } |
| 1185 | return NO_ERROR; |
| 1186 | } |
| 1187 | |
| 1188 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1189 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1190 | audio_stream_type_t stream, |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1191 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1192 | { |
| 1193 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); |
| 1194 | ssize_t index = mOutputs.indexOfKey(output); |
| 1195 | if (index < 0) { |
| 1196 | ALOGW("stopOutput() unknown output %d", output); |
| 1197 | return BAD_VALUE; |
| 1198 | } |
| 1199 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1200 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1201 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1202 | // always handle stream stop, check which stream type is stopping |
| 1203 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); |
| 1204 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1205 | // handle special case for sonification while in call |
| 1206 | if (isInCall()) { |
| 1207 | handleIncallSonification(stream, false, false); |
| 1208 | } |
| 1209 | |
| 1210 | if (outputDesc->mRefCount[stream] > 0) { |
| 1211 | // decrement usage count of this stream on the output |
| 1212 | outputDesc->changeRefCount(stream, -1); |
| 1213 | // store time at which the stream was stopped - see isStreamActive() |
| 1214 | if (outputDesc->mRefCount[stream] == 0) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1215 | // Automatically disable the remote submix input when output is stopped on a |
| 1216 | // re routing mix of type MIX_TYPE_RECORDERS |
| 1217 | if (audio_is_remote_submix_device(outputDesc->mDevice) && |
| 1218 | outputDesc->mPolicyMix != NULL && |
| 1219 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1220 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1221 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1222 | outputDesc->mPolicyMix->mRegistrationId, |
| 1223 | "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1226 | outputDesc->mStopTime[stream] = systemTime(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1227 | audio_devices_t newDevice = getNewOutputDevice(output, false /*fromCache*/); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1228 | // delay the device switch by twice the latency because stopOutput() is executed when |
| 1229 | // the track stop() command is received and at that time the audio track buffer can |
| 1230 | // still contain data that needs to be drained. The latency only covers the audio HAL |
| 1231 | // and kernel buffers. Also the latency does not always include additional delay in the |
| 1232 | // audio path (audio DSP, CODEC ...) |
| 1233 | setOutputDevice(output, newDevice, false, outputDesc->mLatency*2); |
| 1234 | |
| 1235 | // force restoring the device selection on other active outputs if it differs from the |
| 1236 | // one being selected for this output |
| 1237 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1238 | audio_io_handle_t curOutput = mOutputs.keyAt(i); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1239 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1240 | if (curOutput != output && |
| 1241 | desc->isActive() && |
| 1242 | outputDesc->sharesHwModuleWith(desc) && |
| 1243 | (newDevice != desc->device())) { |
| 1244 | setOutputDevice(curOutput, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1245 | getNewOutputDevice(curOutput, false /*fromCache*/), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1246 | true, |
| 1247 | outputDesc->mLatency*2); |
| 1248 | } |
| 1249 | } |
| 1250 | // update the outputs if stopping one with a stream that can affect notification routing |
| 1251 | handleNotificationRoutingForStream(stream); |
| 1252 | } |
| 1253 | return NO_ERROR; |
| 1254 | } else { |
| 1255 | ALOGW("stopOutput() refcount is already 0 for output %d", output); |
| 1256 | return INVALID_OPERATION; |
| 1257 | } |
| 1258 | } |
| 1259 | |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1260 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1261 | audio_stream_type_t stream __unused, |
| 1262 | audio_session_t session __unused) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1263 | { |
| 1264 | ALOGV("releaseOutput() %d", output); |
| 1265 | ssize_t index = mOutputs.indexOfKey(output); |
| 1266 | if (index < 0) { |
| 1267 | ALOGW("releaseOutput() releasing unknown output %d", output); |
| 1268 | return; |
| 1269 | } |
| 1270 | |
| 1271 | #ifdef AUDIO_POLICY_TEST |
| 1272 | int testIndex = testOutputIndex(output); |
| 1273 | if (testIndex != 0) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1274 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1275 | if (outputDesc->isActive()) { |
| 1276 | mpClientInterface->closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1277 | mOutputs.removeItem(output); |
| 1278 | mTestOutputs[testIndex] = 0; |
| 1279 | } |
| 1280 | return; |
| 1281 | } |
| 1282 | #endif //AUDIO_POLICY_TEST |
| 1283 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1284 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(index); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1285 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1286 | if (desc->mDirectOpenCount <= 0) { |
| 1287 | ALOGW("releaseOutput() invalid open count %d for output %d", |
| 1288 | desc->mDirectOpenCount, output); |
| 1289 | return; |
| 1290 | } |
| 1291 | if (--desc->mDirectOpenCount == 0) { |
| 1292 | closeOutput(output); |
| 1293 | // If effects where present on the output, audioflinger moved them to the primary |
| 1294 | // output by default: move them back to the appropriate output. |
| 1295 | audio_io_handle_t dstOutput = getOutputForEffect(); |
| 1296 | if (dstOutput != mPrimaryOutput) { |
| 1297 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput); |
| 1298 | } |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1299 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1305 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, |
| 1306 | audio_io_handle_t *input, |
| 1307 | audio_session_t session, |
| 1308 | uint32_t samplingRate, |
| 1309 | audio_format_t format, |
| 1310 | audio_channel_mask_t channelMask, |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1311 | audio_input_flags_t flags, |
| 1312 | input_type_t *inputType) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1313 | { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1314 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," |
| 1315 | "session %d, flags %#x", |
| 1316 | attr->source, samplingRate, format, channelMask, session, flags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1317 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1318 | *input = AUDIO_IO_HANDLE_NONE; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1319 | *inputType = API_INPUT_INVALID; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1320 | audio_devices_t device; |
| 1321 | // handle legacy remote submix case where the address was not always specified |
| 1322 | String8 address = String8(""); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1323 | bool isSoundTrigger = false; |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1324 | audio_source_t inputSource = attr->source; |
| 1325 | audio_source_t halInputSource; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1326 | AudioMix *policyMix = NULL; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1327 | |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1328 | if (inputSource == AUDIO_SOURCE_DEFAULT) { |
| 1329 | inputSource = AUDIO_SOURCE_MIC; |
| 1330 | } |
| 1331 | halInputSource = inputSource; |
| 1332 | |
| 1333 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1334 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { |
| 1335 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 1336 | address = String8(attr->tags + strlen("addr=")); |
| 1337 | ssize_t index = mPolicyMixes.indexOfKey(address); |
| 1338 | if (index < 0) { |
| 1339 | ALOGW("getInputForAttr() no policy for address %s", address.string()); |
| 1340 | return BAD_VALUE; |
| 1341 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1342 | if (mPolicyMixes[index]->mMix.mMixType != MIX_TYPE_PLAYERS) { |
| 1343 | ALOGW("getInputForAttr() bad policy mix type for address %s", address.string()); |
| 1344 | return BAD_VALUE; |
| 1345 | } |
| 1346 | policyMix = &mPolicyMixes[index]->mMix; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1347 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1348 | } else { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1349 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1350 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1351 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1352 | return BAD_VALUE; |
| 1353 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1354 | if (policyMix != NULL) { |
| 1355 | address = policyMix->mRegistrationId; |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1356 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { |
| 1357 | // there is an external policy, but this input is attached to a mix of recorders, |
| 1358 | // meaning it receives audio injected into the framework, so the recorder doesn't |
| 1359 | // know about it and is therefore considered "legacy" |
| 1360 | *inputType = API_INPUT_LEGACY; |
| 1361 | } else { |
| 1362 | // recording a mix of players defined by an external policy, we're rerouting for |
| 1363 | // an external policy |
| 1364 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; |
| 1365 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1366 | } else if (audio_is_remote_submix_device(device)) { |
| 1367 | address = String8("0"); |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1368 | *inputType = API_INPUT_MIX_CAPTURE; |
| 1369 | } else { |
| 1370 | *inputType = API_INPUT_LEGACY; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1371 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1372 | // adapt channel selection to input source |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1373 | switch (inputSource) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1374 | case AUDIO_SOURCE_VOICE_UPLINK: |
| 1375 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK; |
| 1376 | break; |
| 1377 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 1378 | channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1379 | break; |
| 1380 | case AUDIO_SOURCE_VOICE_CALL: |
| 1381 | channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK; |
| 1382 | break; |
| 1383 | default: |
| 1384 | break; |
| 1385 | } |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1386 | if (inputSource == AUDIO_SOURCE_HOTWORD) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1387 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 1388 | if (index >= 0) { |
| 1389 | *input = mSoundTriggerSessions.valueFor(session); |
| 1390 | isSoundTrigger = true; |
| 1391 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); |
| 1392 | ALOGV("SoundTrigger capture on session %d input %d", session, *input); |
| 1393 | } else { |
| 1394 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 1395 | } |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1399 | sp<IOProfile> profile = getInputProfile(device, address, |
| 1400 | samplingRate, format, channelMask, |
| 1401 | flags); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1402 | if (profile == 0) { |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1403 | //retry without flags |
| 1404 | audio_input_flags_t log_flags = flags; |
| 1405 | flags = AUDIO_INPUT_FLAG_NONE; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1406 | profile = getInputProfile(device, address, |
| 1407 | samplingRate, format, channelMask, |
| 1408 | flags); |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1409 | if (profile == 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1410 | ALOGW("getInputForAttr() could not find profile for device 0x%X, samplingRate %u," |
| 1411 | "format %#x, channelMask 0x%X, flags %#x", |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1412 | device, samplingRate, format, channelMask, log_flags); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1413 | return BAD_VALUE; |
Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1414 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | if (profile->mModule->mHandle == 0) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1418 | ALOGE("getInputForAttr(): HW module %s not opened", profile->mModule->mName); |
| 1419 | return NO_INIT; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 1423 | config.sample_rate = samplingRate; |
| 1424 | config.channel_mask = channelMask; |
| 1425 | config.format = format; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1426 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1427 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1428 | input, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1429 | &config, |
| 1430 | &device, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1431 | address, |
Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1432 | halInputSource, |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1433 | flags); |
| 1434 | |
| 1435 | // only accept input with the exact requested set of parameters |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1436 | if (status != NO_ERROR || *input == AUDIO_IO_HANDLE_NONE || |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1437 | (samplingRate != config.sample_rate) || |
| 1438 | (format != config.format) || |
| 1439 | (channelMask != config.channel_mask)) { |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1440 | ALOGW("getInputForAttr() failed opening input: samplingRate %d, format %d, channelMask %x", |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1441 | samplingRate, format, channelMask); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1442 | if (*input != AUDIO_IO_HANDLE_NONE) { |
| 1443 | mpClientInterface->closeInput(*input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1444 | } |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1445 | return BAD_VALUE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1448 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); |
Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1449 | inputDesc->mInputSource = inputSource; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1450 | inputDesc->mRefCount = 0; |
| 1451 | inputDesc->mOpenRefCount = 1; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1452 | inputDesc->mSamplingRate = samplingRate; |
| 1453 | inputDesc->mFormat = format; |
| 1454 | inputDesc->mChannelMask = channelMask; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1455 | inputDesc->mDevice = device; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1456 | inputDesc->mSessions.add(session); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1457 | inputDesc->mIsSoundTrigger = isSoundTrigger; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1458 | inputDesc->mPolicyMix = policyMix; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1459 | |
Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1460 | ALOGV("getInputForAttr() returns input type = %d", inputType); |
| 1461 | |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1462 | addInput(*input, inputDesc); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1463 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1464 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1467 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, |
| 1468 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1469 | { |
| 1470 | ALOGV("startInput() input %d", input); |
| 1471 | ssize_t index = mInputs.indexOfKey(input); |
| 1472 | if (index < 0) { |
| 1473 | ALOGW("startInput() unknown input %d", input); |
| 1474 | return BAD_VALUE; |
| 1475 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1476 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1477 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1478 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1479 | if (index < 0) { |
| 1480 | ALOGW("startInput() unknown session %d on input %d", session, input); |
| 1481 | return BAD_VALUE; |
| 1482 | } |
| 1483 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1484 | // virtual input devices are compatible with other input devices |
| 1485 | if (!isVirtualInputDevice(inputDesc->mDevice)) { |
| 1486 | |
| 1487 | // for a non-virtual input device, check if there is another (non-virtual) active input |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1488 | audio_io_handle_t activeInput = getActiveInput(); |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1489 | if (activeInput != 0 && activeInput != input) { |
| 1490 | |
| 1491 | // If the already active input uses AUDIO_SOURCE_HOTWORD then it is closed, |
| 1492 | // otherwise the active input continues and the new input cannot be started. |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1493 | sp<AudioInputDescriptor> activeDesc = mInputs.valueFor(activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1494 | if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1495 | ALOGW("startInput(%d) preempting low-priority input %d", input, activeInput); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1496 | stopInput(activeInput, activeDesc->mSessions.itemAt(0)); |
| 1497 | releaseInput(activeInput, activeDesc->mSessions.itemAt(0)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1498 | } else { |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1499 | ALOGE("startInput(%d) failed: other input %d already started", input, activeInput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1500 | return INVALID_OPERATION; |
| 1501 | } |
| 1502 | } |
| 1503 | } |
| 1504 | |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1505 | if (inputDesc->mRefCount == 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1506 | if (activeInputsCount() == 0) { |
| 1507 | SoundTrigger::setCaptureState(true); |
| 1508 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1509 | setInputDevice(input, getNewInputDevice(input), true /* force */); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1510 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1511 | // automatically enable the remote submix output when input is started if not |
| 1512 | // used by a policy mix of type MIX_TYPE_RECORDERS |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1513 | // For remote submix (a virtual device), we open only one input per capture request. |
| 1514 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1515 | String8 address = String8(""); |
| 1516 | if (inputDesc->mPolicyMix == NULL) { |
| 1517 | address = String8("0"); |
| 1518 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1519 | address = inputDesc->mPolicyMix->mRegistrationId; |
| 1520 | } |
| 1521 | if (address != "") { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1522 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1523 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1524 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1525 | } |
Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1526 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1527 | } |
| 1528 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1529 | ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource); |
| 1530 | |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1531 | inputDesc->mRefCount++; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1532 | return NO_ERROR; |
| 1533 | } |
| 1534 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1535 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, |
| 1536 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1537 | { |
| 1538 | ALOGV("stopInput() input %d", input); |
| 1539 | ssize_t index = mInputs.indexOfKey(input); |
| 1540 | if (index < 0) { |
| 1541 | ALOGW("stopInput() unknown input %d", input); |
| 1542 | return BAD_VALUE; |
| 1543 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1544 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1545 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1546 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1547 | if (index < 0) { |
| 1548 | ALOGW("stopInput() unknown session %d on input %d", session, input); |
| 1549 | return BAD_VALUE; |
| 1550 | } |
| 1551 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1552 | if (inputDesc->mRefCount == 0) { |
| 1553 | ALOGW("stopInput() input %d already stopped", input); |
| 1554 | return INVALID_OPERATION; |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | inputDesc->mRefCount--; |
| 1558 | if (inputDesc->mRefCount == 0) { |
| 1559 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1560 | // automatically disable the remote submix output when input is stopped if not |
| 1561 | // used by a policy mix of type MIX_TYPE_RECORDERS |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1562 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1563 | String8 address = String8(""); |
| 1564 | if (inputDesc->mPolicyMix == NULL) { |
| 1565 | address = String8("0"); |
| 1566 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { |
| 1567 | address = inputDesc->mPolicyMix->mRegistrationId; |
| 1568 | } |
| 1569 | if (address != "") { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 1570 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1571 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 1572 | address, "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1573 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1574 | } |
| 1575 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 1576 | resetInputDevice(input); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1577 | |
| 1578 | if (activeInputsCount() == 0) { |
| 1579 | SoundTrigger::setCaptureState(false); |
| 1580 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1581 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1582 | return NO_ERROR; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1585 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, |
| 1586 | audio_session_t session) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1587 | { |
| 1588 | ALOGV("releaseInput() %d", input); |
| 1589 | ssize_t index = mInputs.indexOfKey(input); |
| 1590 | if (index < 0) { |
| 1591 | ALOGW("releaseInput() releasing unknown input %d", input); |
| 1592 | return; |
| 1593 | } |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1594 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); |
| 1595 | ALOG_ASSERT(inputDesc != 0); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1596 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1597 | index = inputDesc->mSessions.indexOf(session); |
Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1598 | if (index < 0) { |
| 1599 | ALOGW("releaseInput() unknown session %d on input %d", session, input); |
| 1600 | return; |
| 1601 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1602 | inputDesc->mSessions.remove(session); |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1603 | if (inputDesc->mOpenRefCount == 0) { |
| 1604 | ALOGW("releaseInput() invalid open ref count %d", inputDesc->mOpenRefCount); |
| 1605 | return; |
| 1606 | } |
| 1607 | inputDesc->mOpenRefCount--; |
| 1608 | if (inputDesc->mOpenRefCount > 0) { |
| 1609 | ALOGV("releaseInput() exit > 0"); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1613 | closeInput(input); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1614 | mpClientInterface->onAudioPortListUpdate(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1615 | ALOGV("releaseInput() exit"); |
| 1616 | } |
| 1617 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1618 | void AudioPolicyManager::closeAllInputs() { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1619 | bool patchRemoved = false; |
| 1620 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1621 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1622 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); |
| 1623 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 1624 | if (patch_index >= 0) { |
| 1625 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); |
| 1626 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 1627 | mAudioPatches.removeItemsAt(patch_index); |
| 1628 | patchRemoved = true; |
| 1629 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1630 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); |
| 1631 | } |
| 1632 | mInputs.clear(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 1633 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 1634 | |
| 1635 | if (patchRemoved) { |
| 1636 | mpClientInterface->onAudioPatchListUpdate(); |
| 1637 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1640 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1641 | int indexMin, |
| 1642 | int indexMax) |
| 1643 | { |
| 1644 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1645 | if (indexMin < 0 || indexMin >= indexMax) { |
| 1646 | ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax); |
| 1647 | return; |
| 1648 | } |
| 1649 | mStreams[stream].mIndexMin = indexMin; |
| 1650 | mStreams[stream].mIndexMax = indexMax; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1651 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1652 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1653 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMin = indexMin; |
| 1654 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexMax = indexMax; |
| 1655 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1658 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1659 | int index, |
| 1660 | audio_devices_t device) |
| 1661 | { |
| 1662 | |
| 1663 | if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) { |
| 1664 | return BAD_VALUE; |
| 1665 | } |
| 1666 | if (!audio_is_output_device(device)) { |
| 1667 | return BAD_VALUE; |
| 1668 | } |
| 1669 | |
| 1670 | // Force max volume if stream cannot be muted |
| 1671 | if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax; |
| 1672 | |
| 1673 | ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d", |
| 1674 | stream, device, index); |
| 1675 | |
| 1676 | // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and |
| 1677 | // clear all device specific values |
| 1678 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1679 | mStreams[stream].mIndexCur.clear(); |
| 1680 | } |
| 1681 | mStreams[stream].mIndexCur.add(device, index); |
| 1682 | |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1683 | // update volume on all outputs whose current device is also selected by the same |
| 1684 | // strategy as the device specified by the caller |
| 1685 | audio_devices_t strategyDevice = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1686 | |
| 1687 | |
| 1688 | //FIXME: AUDIO_STREAM_ACCESSIBILITY volume follows AUDIO_STREAM_MUSIC for now |
| 1689 | audio_devices_t accessibilityDevice = AUDIO_DEVICE_NONE; |
| 1690 | if (stream == AUDIO_STREAM_MUSIC) { |
| 1691 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mIndexCur.add(device, index); |
| 1692 | accessibilityDevice = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, true /*fromCache*/); |
| 1693 | } |
| 1694 | if ((device != AUDIO_DEVICE_OUT_DEFAULT) && |
| 1695 | (device & (strategyDevice | accessibilityDevice)) == 0) { |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1696 | return NO_ERROR; |
| 1697 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1698 | status_t status = NO_ERROR; |
| 1699 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 1700 | audio_devices_t curDevice = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 1701 | ApmGains::getDeviceForVolume(mOutputs.valueAt(i)->device()); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 1702 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & strategyDevice) != 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1703 | status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice); |
| 1704 | if (volStatus != NO_ERROR) { |
| 1705 | status = volStatus; |
| 1706 | } |
| 1707 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 1708 | if ((device == AUDIO_DEVICE_OUT_DEFAULT) || ((curDevice & accessibilityDevice) != 0)) { |
| 1709 | status_t volStatus = checkAndSetVolume(AUDIO_STREAM_ACCESSIBILITY, |
| 1710 | index, mOutputs.keyAt(i), curDevice); |
| 1711 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1712 | } |
| 1713 | return status; |
| 1714 | } |
| 1715 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1716 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1717 | int *index, |
| 1718 | audio_devices_t device) |
| 1719 | { |
| 1720 | if (index == NULL) { |
| 1721 | return BAD_VALUE; |
| 1722 | } |
| 1723 | if (!audio_is_output_device(device)) { |
| 1724 | return BAD_VALUE; |
| 1725 | } |
| 1726 | // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to |
| 1727 | // the strategy the stream belongs to. |
| 1728 | if (device == AUDIO_DEVICE_OUT_DEFAULT) { |
| 1729 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); |
| 1730 | } |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 1731 | device = ApmGains::getDeviceForVolume(device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1732 | |
| 1733 | *index = mStreams[stream].getVolumeIndex(device); |
| 1734 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); |
| 1735 | return NO_ERROR; |
| 1736 | } |
| 1737 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1738 | audio_io_handle_t AudioPolicyManager::selectOutputForEffects( |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1739 | const SortedVector<audio_io_handle_t>& outputs) |
| 1740 | { |
| 1741 | // select one output among several suitable for global effects. |
| 1742 | // The priority is as follows: |
| 1743 | // 1: An offloaded output. If the effect ends up not being offloadable, |
| 1744 | // AudioFlinger will invalidate the track and the offloaded output |
| 1745 | // will be closed causing the effect to be moved to a PCM output. |
| 1746 | // 2: A deep buffer output |
| 1747 | // 3: the first output in the list |
| 1748 | |
| 1749 | if (outputs.size() == 0) { |
| 1750 | return 0; |
| 1751 | } |
| 1752 | |
| 1753 | audio_io_handle_t outputOffloaded = 0; |
| 1754 | audio_io_handle_t outputDeepBuffer = 0; |
| 1755 | |
| 1756 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1757 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 1758 | ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1759 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { |
| 1760 | outputOffloaded = outputs[i]; |
| 1761 | } |
| 1762 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { |
| 1763 | outputDeepBuffer = outputs[i]; |
| 1764 | } |
| 1765 | } |
| 1766 | |
| 1767 | ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d", |
| 1768 | outputOffloaded, outputDeepBuffer); |
| 1769 | if (outputOffloaded != 0) { |
| 1770 | return outputOffloaded; |
| 1771 | } |
| 1772 | if (outputDeepBuffer != 0) { |
| 1773 | return outputDeepBuffer; |
| 1774 | } |
| 1775 | |
| 1776 | return outputs[0]; |
| 1777 | } |
| 1778 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1779 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1780 | { |
| 1781 | // apply simple rule where global effects are attached to the same output as MUSIC streams |
| 1782 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1783 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1784 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 1785 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs); |
| 1786 | |
| 1787 | audio_io_handle_t output = selectOutputForEffects(dstOutputs); |
| 1788 | ALOGV("getOutputForEffect() got output %d for fx %s flags %x", |
| 1789 | output, (desc == NULL) ? "unspecified" : desc->name, (desc == NULL) ? 0 : desc->flags); |
| 1790 | |
| 1791 | return output; |
| 1792 | } |
| 1793 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1794 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1795 | audio_io_handle_t io, |
| 1796 | uint32_t strategy, |
| 1797 | int session, |
| 1798 | int id) |
| 1799 | { |
| 1800 | ssize_t index = mOutputs.indexOfKey(io); |
| 1801 | if (index < 0) { |
| 1802 | index = mInputs.indexOfKey(io); |
| 1803 | if (index < 0) { |
| 1804 | ALOGW("registerEffect() unknown io %d", io); |
| 1805 | return INVALID_OPERATION; |
| 1806 | } |
| 1807 | } |
| 1808 | |
| 1809 | if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) { |
| 1810 | ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB", |
| 1811 | desc->name, desc->memoryUsage); |
| 1812 | return INVALID_OPERATION; |
| 1813 | } |
| 1814 | mTotalEffectsMemory += desc->memoryUsage; |
| 1815 | ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d", |
| 1816 | desc->name, io, strategy, session, id); |
| 1817 | ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory); |
| 1818 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1819 | sp<EffectDescriptor> effectDesc = new EffectDescriptor(); |
| 1820 | memcpy (&effectDesc->mDesc, desc, sizeof(effect_descriptor_t)); |
| 1821 | effectDesc->mIo = io; |
| 1822 | effectDesc->mStrategy = (routing_strategy)strategy; |
| 1823 | effectDesc->mSession = session; |
| 1824 | effectDesc->mEnabled = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1825 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1826 | mEffects.add(id, effectDesc); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1827 | |
| 1828 | return NO_ERROR; |
| 1829 | } |
| 1830 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1831 | status_t AudioPolicyManager::unregisterEffect(int id) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1832 | { |
| 1833 | ssize_t index = mEffects.indexOfKey(id); |
| 1834 | if (index < 0) { |
| 1835 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1836 | return INVALID_OPERATION; |
| 1837 | } |
| 1838 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1839 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1840 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1841 | setEffectEnabled(effectDesc, false); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1842 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1843 | if (mTotalEffectsMemory < effectDesc->mDesc.memoryUsage) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1844 | ALOGW("unregisterEffect() memory %d too big for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1845 | effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
| 1846 | effectDesc->mDesc.memoryUsage = mTotalEffectsMemory; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1847 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1848 | mTotalEffectsMemory -= effectDesc->mDesc.memoryUsage; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1849 | ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1850 | effectDesc->mDesc.name, id, effectDesc->mDesc.memoryUsage, mTotalEffectsMemory); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1851 | |
| 1852 | mEffects.removeItem(id); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1853 | |
| 1854 | return NO_ERROR; |
| 1855 | } |
| 1856 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1857 | status_t AudioPolicyManager::setEffectEnabled(int id, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1858 | { |
| 1859 | ssize_t index = mEffects.indexOfKey(id); |
| 1860 | if (index < 0) { |
| 1861 | ALOGW("unregisterEffect() unknown effect ID %d", id); |
| 1862 | return INVALID_OPERATION; |
| 1863 | } |
| 1864 | |
| 1865 | return setEffectEnabled(mEffects.valueAt(index), enabled); |
| 1866 | } |
| 1867 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1868 | status_t AudioPolicyManager::setEffectEnabled(const sp<EffectDescriptor>& effectDesc, bool enabled) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1869 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1870 | if (enabled == effectDesc->mEnabled) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1871 | ALOGV("setEffectEnabled(%s) effect already %s", |
| 1872 | enabled?"true":"false", enabled?"enabled":"disabled"); |
| 1873 | return INVALID_OPERATION; |
| 1874 | } |
| 1875 | |
| 1876 | if (enabled) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1877 | if (mTotalEffectsCpuLoad + effectDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1878 | 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] | 1879 | effectDesc->mDesc.name, (float)effectDesc->mDesc.cpuLoad/10); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1880 | return INVALID_OPERATION; |
| 1881 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1882 | mTotalEffectsCpuLoad += effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1883 | ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad); |
| 1884 | } else { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1885 | if (mTotalEffectsCpuLoad < effectDesc->mDesc.cpuLoad) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1886 | ALOGW("setEffectEnabled(false) CPU load %d too high for total %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1887 | effectDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad); |
| 1888 | effectDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1889 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1890 | mTotalEffectsCpuLoad -= effectDesc->mDesc.cpuLoad; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1891 | ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad); |
| 1892 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1893 | effectDesc->mEnabled = enabled; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1894 | return NO_ERROR; |
| 1895 | } |
| 1896 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1897 | bool AudioPolicyManager::isNonOffloadableEffectEnabled() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1898 | { |
| 1899 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1900 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 1901 | if (effectDesc->mEnabled && (effectDesc->mStrategy == STRATEGY_MEDIA) && |
| 1902 | ((effectDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1903 | ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d", |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1904 | effectDesc->mDesc.name, effectDesc->mSession); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1905 | return true; |
| 1906 | } |
| 1907 | } |
| 1908 | return false; |
| 1909 | } |
| 1910 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1911 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1912 | { |
| 1913 | nsecs_t sysTime = systemTime(); |
| 1914 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1915 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1916 | if (outputDesc->isStreamActive(stream, inPastMs, sysTime)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1917 | return true; |
| 1918 | } |
| 1919 | } |
| 1920 | return false; |
| 1921 | } |
| 1922 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1923 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1924 | uint32_t inPastMs) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1925 | { |
| 1926 | nsecs_t sysTime = systemTime(); |
| 1927 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1928 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1929 | if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1930 | outputDesc->isStreamActive(stream, inPastMs, sysTime)) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1931 | // do not consider re routing (when the output is going to a dynamic policy) |
| 1932 | // as "remote playback" |
| 1933 | if (outputDesc->mPolicyMix == NULL) { |
Jean-Michel Trivi | 1767df7 | 2014-12-09 18:11:49 -0800 | [diff] [blame] | 1934 | return true; |
| 1935 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1941 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1942 | { |
| 1943 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1944 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 1945 | if (inputDescriptor->mRefCount == 0) { |
| 1946 | continue; |
| 1947 | } |
| 1948 | if (inputDescriptor->mInputSource == (int)source) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1949 | return true; |
| 1950 | } |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 1951 | // AUDIO_SOURCE_HOTWORD is equivalent to AUDIO_SOURCE_VOICE_RECOGNITION only if it |
| 1952 | // corresponds to an active capture triggered by a hardware hotword recognition |
| 1953 | if ((source == AUDIO_SOURCE_VOICE_RECOGNITION) && |
| 1954 | (inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD)) { |
| 1955 | // FIXME: we should not assume that the first session is the active one and keep |
| 1956 | // activity count per session. Same in startInput(). |
| 1957 | ssize_t index = mSoundTriggerSessions.indexOfKey(inputDescriptor->mSessions.itemAt(0)); |
| 1958 | if (index >= 0) { |
| 1959 | return true; |
| 1960 | } |
| 1961 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1962 | } |
| 1963 | return false; |
| 1964 | } |
| 1965 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1966 | // Register a list of custom mixes with their attributes and format. |
| 1967 | // When a mix is registered, corresponding input and output profiles are |
| 1968 | // added to the remote submix hw module. The profile contains only the |
| 1969 | // parameters (sampling rate, format...) specified by the mix. |
| 1970 | // The corresponding input remote submix device is also connected. |
| 1971 | // |
| 1972 | // When a remote submix device is connected, the address is checked to select the |
| 1973 | // appropriate profile and the corresponding input or output stream is opened. |
| 1974 | // |
| 1975 | // When capture starts, getInputForAttr() will: |
| 1976 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 1977 | // - 2 if none found, getDeviceForInputSource() will: |
| 1978 | // - 2.1 look for a mix matching the attributes source |
| 1979 | // - 2.2 if none found, default to device selection by policy rules |
| 1980 | // At this time, the corresponding output remote submix device is also connected |
| 1981 | // and active playback use cases can be transferred to this mix if needed when reconnecting |
| 1982 | // after AudioTracks are invalidated |
| 1983 | // |
| 1984 | // When playback starts, getOutputForAttr() will: |
| 1985 | // - 1 look for a mix matching the address passed in attribtutes tags if any |
| 1986 | // - 2 if none found, look for a mix matching the attributes usage |
| 1987 | // - 3 if none found, default to device and output selection by policy rules. |
| 1988 | |
| 1989 | status_t AudioPolicyManager::registerPolicyMixes(Vector<AudioMix> mixes) |
| 1990 | { |
| 1991 | sp<HwModule> module; |
| 1992 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 1993 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && |
| 1994 | mHwModules[i]->mHandle != 0) { |
| 1995 | module = mHwModules[i]; |
| 1996 | break; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | if (module == 0) { |
| 2001 | return INVALID_OPERATION; |
| 2002 | } |
| 2003 | |
| 2004 | ALOGV("registerPolicyMixes() num mixes %d", mixes.size()); |
| 2005 | |
| 2006 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2007 | String8 address = mixes[i].mRegistrationId; |
| 2008 | ssize_t index = mPolicyMixes.indexOfKey(address); |
| 2009 | if (index >= 0) { |
| 2010 | ALOGE("registerPolicyMixes(): mix for address %s already registered", address.string()); |
| 2011 | continue; |
| 2012 | } |
| 2013 | audio_config_t outputConfig = mixes[i].mFormat; |
| 2014 | audio_config_t inputConfig = mixes[i].mFormat; |
| 2015 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in |
| 2016 | // stereo and let audio flinger do the channel conversion if needed. |
| 2017 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2018 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; |
| 2019 | module->addOutputProfile(address, &outputConfig, |
| 2020 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); |
| 2021 | module->addInputProfile(address, &inputConfig, |
| 2022 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); |
| 2023 | sp<AudioPolicyMix> policyMix = new AudioPolicyMix(); |
| 2024 | policyMix->mMix = mixes[i]; |
| 2025 | mPolicyMixes.add(address, policyMix); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2026 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2027 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2028 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2029 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2030 | } else { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2031 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2032 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2033 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2034 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2035 | } |
| 2036 | return NO_ERROR; |
| 2037 | } |
| 2038 | |
| 2039 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) |
| 2040 | { |
| 2041 | sp<HwModule> module; |
| 2042 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2043 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[i]->mName) == 0 && |
| 2044 | mHwModules[i]->mHandle != 0) { |
| 2045 | module = mHwModules[i]; |
| 2046 | break; |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | if (module == 0) { |
| 2051 | return INVALID_OPERATION; |
| 2052 | } |
| 2053 | |
| 2054 | ALOGV("unregisterPolicyMixes() num mixes %d", mixes.size()); |
| 2055 | |
| 2056 | for (size_t i = 0; i < mixes.size(); i++) { |
| 2057 | String8 address = mixes[i].mRegistrationId; |
| 2058 | ssize_t index = mPolicyMixes.indexOfKey(address); |
| 2059 | if (index < 0) { |
| 2060 | ALOGE("unregisterPolicyMixes(): mix for address %s not registered", address.string()); |
| 2061 | continue; |
| 2062 | } |
| 2063 | |
| 2064 | mPolicyMixes.removeItemsAt(index); |
| 2065 | |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2066 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == |
| 2067 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) |
| 2068 | { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2069 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2070 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2071 | address.string(), "remote-submix"); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2072 | } |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2073 | |
| 2074 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == |
| 2075 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE) |
| 2076 | { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2077 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2078 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2079 | address.string(), "remote-submix"); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2080 | } |
| 2081 | module->removeOutputProfile(address); |
| 2082 | module->removeInputProfile(address); |
| 2083 | } |
| 2084 | return NO_ERROR; |
| 2085 | } |
| 2086 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2087 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2088 | status_t AudioPolicyManager::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2089 | { |
| 2090 | const size_t SIZE = 256; |
| 2091 | char buffer[SIZE]; |
| 2092 | String8 result; |
| 2093 | |
| 2094 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); |
| 2095 | result.append(buffer); |
| 2096 | |
| 2097 | snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput); |
| 2098 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2099 | snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState); |
| 2100 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2101 | snprintf(buffer, SIZE, " Force use for communications %d\n", |
| 2102 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2103 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2104 | 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] | 2105 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2106 | 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] | 2107 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2108 | 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] | 2109 | result.append(buffer); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2110 | 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] | 2111 | result.append(buffer); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2112 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", |
| 2113 | mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO]); |
| 2114 | result.append(buffer); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2115 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2116 | snprintf(buffer, SIZE, " Available output devices:\n"); |
| 2117 | result.append(buffer); |
| 2118 | write(fd, result.string(), result.size()); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2119 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2120 | mAvailableOutputDevices[i]->dump(fd, 2, i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2121 | } |
| 2122 | snprintf(buffer, SIZE, "\n Available input devices:\n"); |
| 2123 | write(fd, buffer, strlen(buffer)); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2124 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2125 | mAvailableInputDevices[i]->dump(fd, 2, i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2126 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2127 | |
| 2128 | snprintf(buffer, SIZE, "\nHW Modules dump:\n"); |
| 2129 | write(fd, buffer, strlen(buffer)); |
| 2130 | for (size_t i = 0; i < mHwModules.size(); i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2131 | snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2132 | write(fd, buffer, strlen(buffer)); |
| 2133 | mHwModules[i]->dump(fd); |
| 2134 | } |
| 2135 | |
| 2136 | snprintf(buffer, SIZE, "\nOutputs dump:\n"); |
| 2137 | write(fd, buffer, strlen(buffer)); |
| 2138 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2139 | snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i)); |
| 2140 | write(fd, buffer, strlen(buffer)); |
| 2141 | mOutputs.valueAt(i)->dump(fd); |
| 2142 | } |
| 2143 | |
| 2144 | snprintf(buffer, SIZE, "\nInputs dump:\n"); |
| 2145 | write(fd, buffer, strlen(buffer)); |
| 2146 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2147 | snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i)); |
| 2148 | write(fd, buffer, strlen(buffer)); |
| 2149 | mInputs.valueAt(i)->dump(fd); |
| 2150 | } |
| 2151 | |
| 2152 | snprintf(buffer, SIZE, "\nStreams dump:\n"); |
| 2153 | write(fd, buffer, strlen(buffer)); |
| 2154 | snprintf(buffer, SIZE, |
| 2155 | " Stream Can be muted Index Min Index Max Index Cur [device : index]...\n"); |
| 2156 | write(fd, buffer, strlen(buffer)); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2157 | for (size_t i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2158 | snprintf(buffer, SIZE, " %02zu ", i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2159 | write(fd, buffer, strlen(buffer)); |
| 2160 | mStreams[i].dump(fd); |
| 2161 | } |
| 2162 | |
| 2163 | snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n", |
| 2164 | (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory); |
| 2165 | write(fd, buffer, strlen(buffer)); |
| 2166 | |
| 2167 | snprintf(buffer, SIZE, "Registered effects:\n"); |
| 2168 | write(fd, buffer, strlen(buffer)); |
| 2169 | for (size_t i = 0; i < mEffects.size(); i++) { |
| 2170 | snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i)); |
| 2171 | write(fd, buffer, strlen(buffer)); |
| 2172 | mEffects.valueAt(i)->dump(fd); |
| 2173 | } |
| 2174 | |
Eric Laurent | 4d41695 | 2014-08-10 14:07:09 -0700 | [diff] [blame] | 2175 | snprintf(buffer, SIZE, "\nAudio Patches:\n"); |
| 2176 | write(fd, buffer, strlen(buffer)); |
| 2177 | for (size_t i = 0; i < mAudioPatches.size(); i++) { |
| 2178 | mAudioPatches[i]->dump(fd, 2, i); |
| 2179 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2180 | |
| 2181 | return NO_ERROR; |
| 2182 | } |
| 2183 | |
| 2184 | // This function checks for the parameters which can be offloaded. |
| 2185 | // This can be enhanced depending on the capability of the DSP and policy |
| 2186 | // of the system. |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2187 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2188 | { |
| 2189 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2190 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2191 | offloadInfo.sample_rate, offloadInfo.channel_mask, |
| 2192 | offloadInfo.format, |
| 2193 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, |
| 2194 | offloadInfo.has_video); |
| 2195 | |
| 2196 | // Check if offload has been disabled |
| 2197 | char propValue[PROPERTY_VALUE_MAX]; |
| 2198 | if (property_get("audio.offload.disable", propValue, "0")) { |
| 2199 | if (atoi(propValue) != 0) { |
| 2200 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); |
| 2201 | return false; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | // Check if stream type is music, then only allow offload as of now. |
| 2206 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) |
| 2207 | { |
| 2208 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); |
| 2209 | return false; |
| 2210 | } |
| 2211 | |
| 2212 | //TODO: enable audio offloading with video when ready |
| 2213 | if (offloadInfo.has_video) |
| 2214 | { |
| 2215 | ALOGV("isOffloadSupported: has_video == true, returning false"); |
| 2216 | return false; |
| 2217 | } |
| 2218 | |
| 2219 | //If duration is less than minimum value defined in property, return false |
| 2220 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { |
| 2221 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { |
| 2222 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); |
| 2223 | return false; |
| 2224 | } |
| 2225 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { |
| 2226 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); |
| 2227 | return false; |
| 2228 | } |
| 2229 | |
| 2230 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from |
| 2231 | // creating an offloaded track and tearing it down immediately after start when audioflinger |
| 2232 | // detects there is an active non offloadable effect. |
| 2233 | // FIXME: We should check the audio session here but we do not have it in this context. |
| 2234 | // This may prevent offloading in rare situations where effects are left active by apps |
| 2235 | // in the background. |
| 2236 | if (isNonOffloadableEffectEnabled()) { |
| 2237 | return false; |
| 2238 | } |
| 2239 | |
| 2240 | // See if there is a profile to support this. |
| 2241 | // AUDIO_DEVICE_NONE |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2242 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2243 | offloadInfo.sample_rate, |
| 2244 | offloadInfo.format, |
| 2245 | offloadInfo.channel_mask, |
| 2246 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2247 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); |
| 2248 | return (profile != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2251 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, |
| 2252 | audio_port_type_t type, |
| 2253 | unsigned int *num_ports, |
| 2254 | struct audio_port *ports, |
| 2255 | unsigned int *generation) |
| 2256 | { |
| 2257 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || |
| 2258 | generation == NULL) { |
| 2259 | return BAD_VALUE; |
| 2260 | } |
| 2261 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); |
| 2262 | if (ports == NULL) { |
| 2263 | *num_ports = 0; |
| 2264 | } |
| 2265 | |
| 2266 | size_t portsWritten = 0; |
| 2267 | size_t portsMax = *num_ports; |
| 2268 | *num_ports = 0; |
| 2269 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { |
| 2270 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2271 | for (size_t i = 0; |
| 2272 | i < mAvailableOutputDevices.size() && portsWritten < portsMax; i++) { |
| 2273 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2274 | } |
| 2275 | *num_ports += mAvailableOutputDevices.size(); |
| 2276 | } |
| 2277 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
| 2278 | for (size_t i = 0; |
| 2279 | i < mAvailableInputDevices.size() && portsWritten < portsMax; i++) { |
| 2280 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); |
| 2281 | } |
| 2282 | *num_ports += mAvailableInputDevices.size(); |
| 2283 | } |
| 2284 | } |
| 2285 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { |
| 2286 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { |
| 2287 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { |
| 2288 | mInputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2289 | } |
| 2290 | *num_ports += mInputs.size(); |
| 2291 | } |
| 2292 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2293 | size_t numOutputs = 0; |
| 2294 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2295 | if (!mOutputs[i]->isDuplicated()) { |
| 2296 | numOutputs++; |
| 2297 | if (portsWritten < portsMax) { |
| 2298 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); |
| 2299 | } |
| 2300 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2301 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2302 | *num_ports += numOutputs; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2306 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2307 | return NO_ERROR; |
| 2308 | } |
| 2309 | |
| 2310 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) |
| 2311 | { |
| 2312 | return NO_ERROR; |
| 2313 | } |
| 2314 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 2315 | sp<AudioOutputDescriptor> AudioPolicyManager::getOutputFromId( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2316 | audio_port_handle_t id) const |
| 2317 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2318 | sp<AudioOutputDescriptor> outputDesc = NULL; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2319 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 2320 | outputDesc = mOutputs.valueAt(i); |
| 2321 | if (outputDesc->mId == id) { |
| 2322 | break; |
| 2323 | } |
| 2324 | } |
| 2325 | return outputDesc; |
| 2326 | } |
| 2327 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 2328 | sp<AudioInputDescriptor> AudioPolicyManager::getInputFromId( |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2329 | audio_port_handle_t id) const |
| 2330 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2331 | sp<AudioInputDescriptor> inputDesc = NULL; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2332 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 2333 | inputDesc = mInputs.valueAt(i); |
| 2334 | if (inputDesc->mId == id) { |
| 2335 | break; |
| 2336 | } |
| 2337 | } |
| 2338 | return inputDesc; |
| 2339 | } |
| 2340 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 2341 | sp <HwModule> AudioPolicyManager::getModuleForDevice( |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2342 | audio_devices_t device) const |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2343 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2344 | sp <HwModule> module; |
| 2345 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2346 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2347 | if (mHwModules[i]->mHandle == 0) { |
| 2348 | continue; |
| 2349 | } |
| 2350 | if (audio_is_output_device(device)) { |
| 2351 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2352 | { |
| 2353 | if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices.types() & device) { |
| 2354 | return mHwModules[i]; |
| 2355 | } |
| 2356 | } |
| 2357 | } else { |
| 2358 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) { |
| 2359 | if (mHwModules[i]->mInputProfiles[j]->mSupportedDevices.types() & |
| 2360 | device & ~AUDIO_DEVICE_BIT_IN) { |
| 2361 | return mHwModules[i]; |
| 2362 | } |
| 2363 | } |
| 2364 | } |
| 2365 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2366 | return module; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 2369 | sp <HwModule> AudioPolicyManager::getModuleFromName(const char *name) const |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2370 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2371 | sp <HwModule> module; |
| 2372 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2373 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 2374 | { |
| 2375 | if (strcmp(mHwModules[i]->mName, name) == 0) { |
| 2376 | return mHwModules[i]; |
| 2377 | } |
| 2378 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2379 | return module; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2380 | } |
| 2381 | |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 2382 | audio_devices_t AudioPolicyManager::availablePrimaryOutputDevices() |
| 2383 | { |
| 2384 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 2385 | audio_devices_t devices = outputDesc->mProfile->mSupportedDevices.types(); |
| 2386 | return devices & mAvailableOutputDevices.types(); |
| 2387 | } |
| 2388 | |
| 2389 | audio_devices_t AudioPolicyManager::availablePrimaryInputDevices() |
| 2390 | { |
| 2391 | audio_module_handle_t primaryHandle = |
| 2392 | mOutputs.valueFor(mPrimaryOutput)->mProfile->mModule->mHandle; |
| 2393 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
| 2394 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { |
| 2395 | if (mAvailableInputDevices[i]->mModule->mHandle == primaryHandle) { |
| 2396 | devices |= mAvailableInputDevices[i]->mDeviceType; |
| 2397 | } |
| 2398 | } |
| 2399 | return devices; |
| 2400 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 2401 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2402 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, |
| 2403 | audio_patch_handle_t *handle, |
| 2404 | uid_t uid) |
| 2405 | { |
| 2406 | ALOGV("createAudioPatch()"); |
| 2407 | |
| 2408 | if (handle == NULL || patch == NULL) { |
| 2409 | return BAD_VALUE; |
| 2410 | } |
| 2411 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); |
| 2412 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2413 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || |
| 2414 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { |
| 2415 | return BAD_VALUE; |
| 2416 | } |
| 2417 | // only one source per audio patch supported for now |
| 2418 | if (patch->num_sources > 1) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2419 | return INVALID_OPERATION; |
| 2420 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2421 | |
| 2422 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2423 | return INVALID_OPERATION; |
| 2424 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2425 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2426 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { |
| 2427 | return INVALID_OPERATION; |
| 2428 | } |
| 2429 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2430 | |
| 2431 | sp<AudioPatch> patchDesc; |
| 2432 | ssize_t index = mAudioPatches.indexOfKey(*handle); |
| 2433 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2434 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, |
| 2435 | patch->sources[0].role, |
| 2436 | patch->sources[0].type); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2437 | #if LOG_NDEBUG == 0 |
| 2438 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2439 | ALOGV("createAudioPatch sink %d: id %d role %d type %d", i, patch->sinks[i].id, |
| 2440 | patch->sinks[i].role, |
| 2441 | patch->sinks[i].type); |
| 2442 | } |
| 2443 | #endif |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2444 | |
| 2445 | if (index >= 0) { |
| 2446 | patchDesc = mAudioPatches.valueAt(index); |
| 2447 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2448 | mUidCached, patchDesc->mUid, uid); |
| 2449 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2450 | return INVALID_OPERATION; |
| 2451 | } |
| 2452 | } else { |
| 2453 | *handle = 0; |
| 2454 | } |
| 2455 | |
| 2456 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2457 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2458 | if (outputDesc == NULL) { |
| 2459 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2460 | return BAD_VALUE; |
| 2461 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2462 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", |
| 2463 | outputDesc->mIoHandle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2464 | if (patchDesc != 0) { |
| 2465 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
| 2466 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", |
| 2467 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); |
| 2468 | return BAD_VALUE; |
| 2469 | } |
| 2470 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2471 | DeviceVector devices; |
| 2472 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2473 | // Only support mix to devices connection |
| 2474 | // TODO add support for mix to mix connection |
| 2475 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2476 | ALOGV("createAudioPatch() source mix but sink is not a device"); |
| 2477 | return INVALID_OPERATION; |
| 2478 | } |
| 2479 | sp<DeviceDescriptor> devDesc = |
| 2480 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2481 | if (devDesc == 0) { |
| 2482 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); |
| 2483 | return BAD_VALUE; |
| 2484 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2485 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2486 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2487 | devDesc->mAddress, |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2488 | patch->sources[0].sample_rate, |
| 2489 | NULL, // updatedSamplingRate |
| 2490 | patch->sources[0].format, |
| 2491 | patch->sources[0].channel_mask, |
| 2492 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { |
| 2493 | ALOGV("createAudioPatch() profile not supported for device %08x", |
| 2494 | devDesc->mDeviceType); |
| 2495 | return INVALID_OPERATION; |
| 2496 | } |
| 2497 | devices.add(devDesc); |
| 2498 | } |
| 2499 | if (devices.size() == 0) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2500 | return INVALID_OPERATION; |
| 2501 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2502 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2503 | // TODO: reconfigure output format and channels here |
| 2504 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2505 | devices.types(), outputDesc->mIoHandle); |
| 2506 | setOutputDevice(outputDesc->mIoHandle, devices.types(), true, 0, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2507 | index = mAudioPatches.indexOfKey(*handle); |
| 2508 | if (index >= 0) { |
| 2509 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2510 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); |
| 2511 | } |
| 2512 | patchDesc = mAudioPatches.valueAt(index); |
| 2513 | patchDesc->mUid = uid; |
| 2514 | ALOGV("createAudioPatch() success"); |
| 2515 | } else { |
| 2516 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); |
| 2517 | return INVALID_OPERATION; |
| 2518 | } |
| 2519 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2520 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
| 2521 | // input device to input mix connection |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2522 | // only one sink supported when connecting an input device to a mix |
| 2523 | if (patch->num_sinks > 1) { |
| 2524 | return INVALID_OPERATION; |
| 2525 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2526 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2527 | if (inputDesc == NULL) { |
| 2528 | return BAD_VALUE; |
| 2529 | } |
| 2530 | if (patchDesc != 0) { |
| 2531 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { |
| 2532 | return BAD_VALUE; |
| 2533 | } |
| 2534 | } |
| 2535 | sp<DeviceDescriptor> devDesc = |
| 2536 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
| 2537 | if (devDesc == 0) { |
| 2538 | return BAD_VALUE; |
| 2539 | } |
| 2540 | |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2541 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->mDeviceType, |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2542 | devDesc->mAddress, |
| 2543 | patch->sinks[0].sample_rate, |
| 2544 | NULL, /*updatedSampleRate*/ |
| 2545 | patch->sinks[0].format, |
| 2546 | patch->sinks[0].channel_mask, |
| 2547 | // FIXME for the parameter type, |
| 2548 | // and the NONE |
| 2549 | (audio_output_flags_t) |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2550 | AUDIO_INPUT_FLAG_NONE)) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2551 | return INVALID_OPERATION; |
| 2552 | } |
| 2553 | // TODO: reconfigure output format and channels here |
| 2554 | ALOGV("createAudioPatch() setting device %08x on output %d", |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2555 | devDesc->mDeviceType, inputDesc->mIoHandle); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2556 | setInputDevice(inputDesc->mIoHandle, devDesc->mDeviceType, true, handle); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2557 | index = mAudioPatches.indexOfKey(*handle); |
| 2558 | if (index >= 0) { |
| 2559 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { |
| 2560 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); |
| 2561 | } |
| 2562 | patchDesc = mAudioPatches.valueAt(index); |
| 2563 | patchDesc->mUid = uid; |
| 2564 | ALOGV("createAudioPatch() success"); |
| 2565 | } else { |
| 2566 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); |
| 2567 | return INVALID_OPERATION; |
| 2568 | } |
| 2569 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2570 | // device to device connection |
| 2571 | if (patchDesc != 0) { |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2572 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2573 | return BAD_VALUE; |
| 2574 | } |
| 2575 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2576 | sp<DeviceDescriptor> srcDeviceDesc = |
| 2577 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); |
Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2578 | if (srcDeviceDesc == 0) { |
| 2579 | return BAD_VALUE; |
| 2580 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2581 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2582 | //update source and sink with our own data as the data passed in the patch may |
| 2583 | // be incomplete. |
| 2584 | struct audio_patch newPatch = *patch; |
| 2585 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2586 | |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2587 | for (size_t i = 0; i < patch->num_sinks; i++) { |
| 2588 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { |
| 2589 | ALOGV("createAudioPatch() source device but one sink is not a device"); |
| 2590 | return INVALID_OPERATION; |
| 2591 | } |
| 2592 | |
| 2593 | sp<DeviceDescriptor> sinkDeviceDesc = |
| 2594 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); |
| 2595 | if (sinkDeviceDesc == 0) { |
| 2596 | return BAD_VALUE; |
| 2597 | } |
| 2598 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); |
| 2599 | |
| 2600 | if (srcDeviceDesc->mModule != sinkDeviceDesc->mModule) { |
| 2601 | // only one sink supported when connected devices across HW modules |
| 2602 | if (patch->num_sinks > 1) { |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2603 | return INVALID_OPERATION; |
| 2604 | } |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2605 | SortedVector<audio_io_handle_t> outputs = |
| 2606 | getOutputsForDevice(sinkDeviceDesc->mDeviceType, |
| 2607 | mOutputs); |
| 2608 | // if the sink device is reachable via an opened output stream, request to go via |
| 2609 | // this output stream by adding a second source to the patch description |
Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2610 | audio_io_handle_t output = selectOutput(outputs, |
| 2611 | AUDIO_OUTPUT_FLAG_NONE, |
| 2612 | AUDIO_FORMAT_INVALID); |
Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2613 | if (output != AUDIO_IO_HANDLE_NONE) { |
| 2614 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
| 2615 | if (outputDesc->isDuplicated()) { |
| 2616 | return INVALID_OPERATION; |
| 2617 | } |
| 2618 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); |
| 2619 | newPatch.num_sources = 2; |
| 2620 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2621 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2622 | } |
| 2623 | // TODO: check from routing capabilities in config file and other conflicting patches |
| 2624 | |
| 2625 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 2626 | if (index >= 0) { |
| 2627 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 2628 | } |
| 2629 | |
| 2630 | status_t status = mpClientInterface->createAudioPatch(&newPatch, |
| 2631 | &afPatchHandle, |
| 2632 | 0); |
| 2633 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", |
| 2634 | status, afPatchHandle); |
| 2635 | if (status == NO_ERROR) { |
| 2636 | if (index < 0) { |
| 2637 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 2638 | &newPatch, uid); |
| 2639 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 2640 | } else { |
| 2641 | patchDesc->mPatch = newPatch; |
| 2642 | } |
| 2643 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 2644 | *handle = patchDesc->mHandle; |
| 2645 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2646 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2647 | } else { |
| 2648 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", |
| 2649 | status); |
| 2650 | return INVALID_OPERATION; |
| 2651 | } |
| 2652 | } else { |
| 2653 | return BAD_VALUE; |
| 2654 | } |
| 2655 | } else { |
| 2656 | return BAD_VALUE; |
| 2657 | } |
| 2658 | return NO_ERROR; |
| 2659 | } |
| 2660 | |
| 2661 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, |
| 2662 | uid_t uid) |
| 2663 | { |
| 2664 | ALOGV("releaseAudioPatch() patch %d", handle); |
| 2665 | |
| 2666 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2667 | |
| 2668 | if (index < 0) { |
| 2669 | return BAD_VALUE; |
| 2670 | } |
| 2671 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 2672 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", |
| 2673 | mUidCached, patchDesc->mUid, uid); |
| 2674 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { |
| 2675 | return INVALID_OPERATION; |
| 2676 | } |
| 2677 | |
| 2678 | struct audio_patch *patch = &patchDesc->mPatch; |
| 2679 | patchDesc->mUid = mUidCached; |
| 2680 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2681 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(patch->sources[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2682 | if (outputDesc == NULL) { |
| 2683 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); |
| 2684 | return BAD_VALUE; |
| 2685 | } |
| 2686 | |
| 2687 | setOutputDevice(outputDesc->mIoHandle, |
| 2688 | getNewOutputDevice(outputDesc->mIoHandle, true /*fromCache*/), |
| 2689 | true, |
| 2690 | 0, |
| 2691 | NULL); |
| 2692 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2693 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2694 | sp<AudioInputDescriptor> inputDesc = getInputFromId(patch->sinks[0].id); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2695 | if (inputDesc == NULL) { |
| 2696 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); |
| 2697 | return BAD_VALUE; |
| 2698 | } |
| 2699 | setInputDevice(inputDesc->mIoHandle, |
| 2700 | getNewInputDevice(inputDesc->mIoHandle), |
| 2701 | true, |
| 2702 | NULL); |
| 2703 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { |
| 2704 | audio_patch_handle_t afPatchHandle = patchDesc->mAfPatchHandle; |
| 2705 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 2706 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", |
| 2707 | status, patchDesc->mAfPatchHandle); |
| 2708 | removeAudioPatch(patchDesc->mHandle); |
| 2709 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2710 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2711 | } else { |
| 2712 | return BAD_VALUE; |
| 2713 | } |
| 2714 | } else { |
| 2715 | return BAD_VALUE; |
| 2716 | } |
| 2717 | return NO_ERROR; |
| 2718 | } |
| 2719 | |
| 2720 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, |
| 2721 | struct audio_patch *patches, |
| 2722 | unsigned int *generation) |
| 2723 | { |
| 2724 | if (num_patches == NULL || (*num_patches != 0 && patches == NULL) || |
| 2725 | generation == NULL) { |
| 2726 | return BAD_VALUE; |
| 2727 | } |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2728 | ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2729 | *num_patches, patches, mAudioPatches.size()); |
| 2730 | if (patches == NULL) { |
| 2731 | *num_patches = 0; |
| 2732 | } |
| 2733 | |
| 2734 | size_t patchesWritten = 0; |
| 2735 | size_t patchesMax = *num_patches; |
| 2736 | for (size_t i = 0; |
| 2737 | i < mAudioPatches.size() && patchesWritten < patchesMax; i++) { |
| 2738 | patches[patchesWritten] = mAudioPatches[i]->mPatch; |
| 2739 | patches[patchesWritten++].id = mAudioPatches[i]->mHandle; |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2740 | ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2741 | i, mAudioPatches[i]->mPatch.num_sources, mAudioPatches[i]->mPatch.num_sinks); |
| 2742 | } |
| 2743 | *num_patches = mAudioPatches.size(); |
| 2744 | |
| 2745 | *generation = curAudioPortGeneration(); |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2746 | ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2747 | return NO_ERROR; |
| 2748 | } |
| 2749 | |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2750 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2751 | { |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2752 | ALOGV("setAudioPortConfig()"); |
| 2753 | |
| 2754 | if (config == NULL) { |
| 2755 | return BAD_VALUE; |
| 2756 | } |
| 2757 | ALOGV("setAudioPortConfig() on port handle %d", config->id); |
| 2758 | // Only support gain configuration for now |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2759 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { |
| 2760 | return INVALID_OPERATION; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2763 | sp<AudioPortConfig> audioPortConfig; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2764 | if (config->type == AUDIO_PORT_TYPE_MIX) { |
| 2765 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2766 | sp<AudioOutputDescriptor> outputDesc = getOutputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2767 | if (outputDesc == NULL) { |
| 2768 | return BAD_VALUE; |
| 2769 | } |
Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2770 | ALOG_ASSERT(!outputDesc->isDuplicated(), |
| 2771 | "setAudioPortConfig() called on duplicated output %d", |
| 2772 | outputDesc->mIoHandle); |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2773 | audioPortConfig = outputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2774 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2775 | sp<AudioInputDescriptor> inputDesc = getInputFromId(config->id); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2776 | if (inputDesc == NULL) { |
| 2777 | return BAD_VALUE; |
| 2778 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2779 | audioPortConfig = inputDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2780 | } else { |
| 2781 | return BAD_VALUE; |
| 2782 | } |
| 2783 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { |
| 2784 | sp<DeviceDescriptor> deviceDesc; |
| 2785 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { |
| 2786 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); |
| 2787 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { |
| 2788 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); |
| 2789 | } else { |
| 2790 | return BAD_VALUE; |
| 2791 | } |
| 2792 | if (deviceDesc == NULL) { |
| 2793 | return BAD_VALUE; |
| 2794 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2795 | audioPortConfig = deviceDesc; |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2796 | } else { |
| 2797 | return BAD_VALUE; |
| 2798 | } |
| 2799 | |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2800 | struct audio_port_config backupConfig; |
| 2801 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); |
| 2802 | if (status == NO_ERROR) { |
| 2803 | struct audio_port_config newConfig; |
| 2804 | audioPortConfig->toAudioPortConfig(&newConfig, config); |
| 2805 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2806 | } |
Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 2807 | if (status != NO_ERROR) { |
| 2808 | audioPortConfig->applyAudioPortConfig(&backupConfig); |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2809 | } |
Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 2810 | |
| 2811 | return status; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | void AudioPolicyManager::clearAudioPatches(uid_t uid) |
| 2815 | { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2816 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2817 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); |
| 2818 | if (patchDesc->mUid == uid) { |
Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 2819 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2820 | } |
| 2821 | } |
| 2822 | } |
| 2823 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2824 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, |
| 2825 | audio_io_handle_t *ioHandle, |
| 2826 | audio_devices_t *device) |
| 2827 | { |
| 2828 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(); |
| 2829 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(); |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 2830 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 2831 | |
| 2832 | mSoundTriggerSessions.add(*session, *ioHandle); |
| 2833 | |
| 2834 | return NO_ERROR; |
| 2835 | } |
| 2836 | |
| 2837 | status_t AudioPolicyManager::releaseSoundTriggerSession(audio_session_t session) |
| 2838 | { |
| 2839 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); |
| 2840 | if (index < 0) { |
| 2841 | ALOGW("acquireSoundTriggerSession() session %d not registered", session); |
| 2842 | return BAD_VALUE; |
| 2843 | } |
| 2844 | |
| 2845 | mSoundTriggerSessions.removeItem(session); |
| 2846 | return NO_ERROR; |
| 2847 | } |
| 2848 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2849 | status_t AudioPolicyManager::addAudioPatch(audio_patch_handle_t handle, |
| 2850 | const sp<AudioPatch>& patch) |
| 2851 | { |
| 2852 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2853 | |
| 2854 | if (index >= 0) { |
| 2855 | ALOGW("addAudioPatch() patch %d already in", handle); |
| 2856 | return ALREADY_EXISTS; |
| 2857 | } |
| 2858 | mAudioPatches.add(handle, patch); |
| 2859 | ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d" |
| 2860 | "sink handle %d", |
| 2861 | handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks, |
| 2862 | patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id); |
| 2863 | return NO_ERROR; |
| 2864 | } |
| 2865 | |
| 2866 | status_t AudioPolicyManager::removeAudioPatch(audio_patch_handle_t handle) |
| 2867 | { |
| 2868 | ssize_t index = mAudioPatches.indexOfKey(handle); |
| 2869 | |
| 2870 | if (index < 0) { |
| 2871 | ALOGW("removeAudioPatch() patch %d not in", handle); |
| 2872 | return ALREADY_EXISTS; |
| 2873 | } |
| 2874 | ALOGV("removeAudioPatch() handle %d af handle %d", handle, |
| 2875 | mAudioPatches.valueAt(index)->mAfPatchHandle); |
| 2876 | mAudioPatches.removeItemsAt(index); |
| 2877 | return NO_ERROR; |
| 2878 | } |
| 2879 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2880 | // ---------------------------------------------------------------------------- |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2881 | // AudioPolicyManager |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2882 | // ---------------------------------------------------------------------------- |
| 2883 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2884 | uint32_t AudioPolicyManager::nextUniqueId() |
| 2885 | { |
| 2886 | return android_atomic_inc(&mNextUniqueId); |
| 2887 | } |
| 2888 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2889 | uint32_t AudioPolicyManager::nextAudioPortGeneration() |
| 2890 | { |
| 2891 | return android_atomic_inc(&mAudioPortGeneration); |
| 2892 | } |
| 2893 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2894 | int32_t volatile AudioPolicyManager::mNextUniqueId = 1; |
| 2895 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2896 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2897 | : |
| 2898 | #ifdef AUDIO_POLICY_TEST |
| 2899 | Thread(false), |
| 2900 | #endif //AUDIO_POLICY_TEST |
| 2901 | mPrimaryOutput((audio_io_handle_t)0), |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2902 | mPhoneState(AUDIO_MODE_NORMAL), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2903 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), |
| 2904 | mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0), |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2905 | mA2dpSuspended(false), |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2906 | mSpeakerDrcEnabled(false), |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 2907 | mAudioPortGeneration(1), |
| 2908 | mBeaconMuteRefCount(0), |
| 2909 | mBeaconPlayingRefCount(0), |
| 2910 | mBeaconMuted(false) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2911 | { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2912 | mUidCached = getuid(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2913 | mpClientInterface = clientInterface; |
| 2914 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2915 | for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) { |
| 2916 | mForceUse[i] = AUDIO_POLICY_FORCE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 2919 | mDefaultOutputDevice = new DeviceDescriptor(String8("Speaker"), AUDIO_DEVICE_OUT_SPEAKER); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2920 | if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) { |
| 2921 | if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) { |
| 2922 | ALOGE("could not load audio policy configuration file, setting defaults"); |
| 2923 | defaultAudioPolicyConfig(); |
| 2924 | } |
| 2925 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2926 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2927 | |
| 2928 | // must be done after reading the policy |
| 2929 | initializeVolumeCurves(); |
| 2930 | |
| 2931 | // open all output streams needed to access attached devices |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2932 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); |
| 2933 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2934 | for (size_t i = 0; i < mHwModules.size(); i++) { |
| 2935 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName); |
| 2936 | if (mHwModules[i]->mHandle == 0) { |
| 2937 | ALOGW("could not open HW module %s", mHwModules[i]->mName); |
| 2938 | continue; |
| 2939 | } |
| 2940 | // open all output streams needed to access attached devices |
| 2941 | // except for direct output streams that are only opened when they are actually |
| 2942 | // required by an app. |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2943 | // This also validates mAvailableOutputDevices list |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2944 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 2945 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2946 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2947 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2948 | if (outProfile->mSupportedDevices.isEmpty()) { |
| 2949 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->mName); |
| 2950 | continue; |
| 2951 | } |
| 2952 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2953 | if ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { |
| 2954 | continue; |
| 2955 | } |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2956 | audio_devices_t profileType = outProfile->mSupportedDevices.types(); |
| 2957 | if ((profileType & mDefaultOutputDevice->mDeviceType) != AUDIO_DEVICE_NONE) { |
| 2958 | profileType = mDefaultOutputDevice->mDeviceType; |
| 2959 | } else { |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2960 | // chose first device present in mSupportedDevices also part of |
| 2961 | // outputDeviceTypes |
| 2962 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
| 2963 | profileType = outProfile->mSupportedDevices[k]->mDeviceType; |
| 2964 | if ((profileType & outputDeviceTypes) != 0) { |
| 2965 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 2966 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2967 | } |
| 2968 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 2969 | if ((profileType & outputDeviceTypes) == 0) { |
| 2970 | continue; |
| 2971 | } |
| 2972 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(outProfile); |
| 2973 | |
| 2974 | outputDesc->mDevice = profileType; |
| 2975 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 2976 | config.sample_rate = outputDesc->mSamplingRate; |
| 2977 | config.channel_mask = outputDesc->mChannelMask; |
| 2978 | config.format = outputDesc->mFormat; |
| 2979 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 2980 | status_t status = mpClientInterface->openOutput(outProfile->mModule->mHandle, |
| 2981 | &output, |
| 2982 | &config, |
| 2983 | &outputDesc->mDevice, |
| 2984 | String8(""), |
| 2985 | &outputDesc->mLatency, |
| 2986 | outputDesc->mFlags); |
| 2987 | |
| 2988 | if (status != NO_ERROR) { |
| 2989 | ALOGW("Cannot open output stream for device %08x on hw module %s", |
| 2990 | outputDesc->mDevice, |
| 2991 | mHwModules[i]->mName); |
| 2992 | } else { |
| 2993 | outputDesc->mSamplingRate = config.sample_rate; |
| 2994 | outputDesc->mChannelMask = config.channel_mask; |
| 2995 | outputDesc->mFormat = config.format; |
| 2996 | |
| 2997 | for (size_t k = 0; k < outProfile->mSupportedDevices.size(); k++) { |
| 2998 | audio_devices_t type = outProfile->mSupportedDevices[k]->mDeviceType; |
| 2999 | ssize_t index = |
| 3000 | mAvailableOutputDevices.indexOf(outProfile->mSupportedDevices[k]); |
| 3001 | // 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] | 3002 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { |
| 3003 | mAvailableOutputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | if (mPrimaryOutput == 0 && |
| 3007 | outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 3008 | mPrimaryOutput = output; |
| 3009 | } |
| 3010 | addOutput(output, outputDesc); |
| 3011 | setOutputDevice(output, |
| 3012 | outputDesc->mDevice, |
| 3013 | true); |
| 3014 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3015 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3016 | // open input streams needed to access attached devices to validate |
| 3017 | // mAvailableInputDevices list |
| 3018 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 3019 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3020 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3021 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3022 | if (inProfile->mSupportedDevices.isEmpty()) { |
| 3023 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->mName); |
| 3024 | continue; |
| 3025 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3026 | // chose first device present in mSupportedDevices also part of |
| 3027 | // inputDeviceTypes |
| 3028 | audio_devices_t profileType = AUDIO_DEVICE_NONE; |
| 3029 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
| 3030 | profileType = inProfile->mSupportedDevices[k]->mDeviceType; |
| 3031 | if (profileType & inputDeviceTypes) { |
| 3032 | break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3033 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3034 | } |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3035 | if ((profileType & inputDeviceTypes) == 0) { |
| 3036 | continue; |
| 3037 | } |
| 3038 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(inProfile); |
| 3039 | |
| 3040 | inputDesc->mInputSource = AUDIO_SOURCE_MIC; |
| 3041 | inputDesc->mDevice = profileType; |
| 3042 | |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3043 | // find the address |
| 3044 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); |
| 3045 | // the inputs vector must be of size 1, but we don't want to crash here |
| 3046 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress |
| 3047 | : String8(""); |
| 3048 | ALOGV(" for input device 0x%x using address %s", profileType, address.string()); |
| 3049 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); |
| 3050 | |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3051 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3052 | config.sample_rate = inputDesc->mSamplingRate; |
| 3053 | config.channel_mask = inputDesc->mChannelMask; |
| 3054 | config.format = inputDesc->mFormat; |
| 3055 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 3056 | status_t status = mpClientInterface->openInput(inProfile->mModule->mHandle, |
| 3057 | &input, |
| 3058 | &config, |
| 3059 | &inputDesc->mDevice, |
Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3060 | address, |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3061 | AUDIO_SOURCE_MIC, |
| 3062 | AUDIO_INPUT_FLAG_NONE); |
| 3063 | |
| 3064 | if (status == NO_ERROR) { |
| 3065 | for (size_t k = 0; k < inProfile->mSupportedDevices.size(); k++) { |
| 3066 | audio_devices_t type = inProfile->mSupportedDevices[k]->mDeviceType; |
| 3067 | ssize_t index = |
| 3068 | mAvailableInputDevices.indexOf(inProfile->mSupportedDevices[k]); |
| 3069 | // 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] | 3070 | if (index >= 0 && !mAvailableInputDevices[index]->isAttached()) { |
| 3071 | mAvailableInputDevices[index]->attach(mHwModules[i]); |
Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3072 | } |
| 3073 | } |
| 3074 | mpClientInterface->closeInput(input); |
| 3075 | } else { |
| 3076 | ALOGW("Cannot open input stream for device %08x on hw module %s", |
| 3077 | inputDesc->mDevice, |
| 3078 | mHwModules[i]->mName); |
| 3079 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3080 | } |
| 3081 | } |
| 3082 | // make sure all attached devices have been allocated a unique ID |
| 3083 | for (size_t i = 0; i < mAvailableOutputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3084 | if (!mAvailableOutputDevices[i]->isAttached()) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3085 | ALOGW("Input device %08x unreachable", mAvailableOutputDevices[i]->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3086 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); |
| 3087 | continue; |
| 3088 | } |
| 3089 | i++; |
| 3090 | } |
| 3091 | for (size_t i = 0; i < mAvailableInputDevices.size();) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3092 | if (!mAvailableInputDevices[i]->isAttached()) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3093 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3094 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); |
| 3095 | continue; |
| 3096 | } |
| 3097 | i++; |
| 3098 | } |
| 3099 | // make sure default device is reachable |
| 3100 | if (mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3101 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->mDeviceType); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3102 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3103 | |
| 3104 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); |
| 3105 | |
| 3106 | updateDevicesAndOutputs(); |
| 3107 | |
| 3108 | #ifdef AUDIO_POLICY_TEST |
| 3109 | if (mPrimaryOutput != 0) { |
| 3110 | AudioParameter outputCmd = AudioParameter(); |
| 3111 | outputCmd.addInt(String8("set_id"), 0); |
| 3112 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 3113 | |
| 3114 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; |
| 3115 | mTestSamplingRate = 44100; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3116 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; |
| 3117 | mTestChannels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3118 | mTestLatencyMs = 0; |
| 3119 | mCurOutput = 0; |
| 3120 | mDirectOutput = false; |
| 3121 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3122 | mTestOutputs[i] = 0; |
| 3123 | } |
| 3124 | |
| 3125 | const size_t SIZE = 256; |
| 3126 | char buffer[SIZE]; |
| 3127 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); |
| 3128 | run(buffer, ANDROID_PRIORITY_AUDIO); |
| 3129 | } |
| 3130 | #endif //AUDIO_POLICY_TEST |
| 3131 | } |
| 3132 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3133 | AudioPolicyManager::~AudioPolicyManager() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3134 | { |
| 3135 | #ifdef AUDIO_POLICY_TEST |
| 3136 | exit(); |
| 3137 | #endif //AUDIO_POLICY_TEST |
| 3138 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3139 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3140 | } |
| 3141 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 3142 | mpClientInterface->closeInput(mInputs.keyAt(i)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3143 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3144 | mAvailableOutputDevices.clear(); |
| 3145 | mAvailableInputDevices.clear(); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3146 | mOutputs.clear(); |
| 3147 | mInputs.clear(); |
| 3148 | mHwModules.clear(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3149 | } |
| 3150 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3151 | status_t AudioPolicyManager::initCheck() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3152 | { |
| 3153 | return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR; |
| 3154 | } |
| 3155 | |
| 3156 | #ifdef AUDIO_POLICY_TEST |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3157 | bool AudioPolicyManager::threadLoop() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3158 | { |
| 3159 | ALOGV("entering threadLoop()"); |
| 3160 | while (!exitPending()) |
| 3161 | { |
| 3162 | String8 command; |
| 3163 | int valueInt; |
| 3164 | String8 value; |
| 3165 | |
| 3166 | Mutex::Autolock _l(mLock); |
| 3167 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); |
| 3168 | |
| 3169 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); |
| 3170 | AudioParameter param = AudioParameter(command); |
| 3171 | |
| 3172 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && |
| 3173 | valueInt != 0) { |
| 3174 | ALOGV("Test command %s received", command.string()); |
| 3175 | String8 target; |
| 3176 | if (param.get(String8("target"), target) != NO_ERROR) { |
| 3177 | target = "Manager"; |
| 3178 | } |
| 3179 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { |
| 3180 | param.remove(String8("test_cmd_policy_output")); |
| 3181 | mCurOutput = valueInt; |
| 3182 | } |
| 3183 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { |
| 3184 | param.remove(String8("test_cmd_policy_direct")); |
| 3185 | if (value == "false") { |
| 3186 | mDirectOutput = false; |
| 3187 | } else if (value == "true") { |
| 3188 | mDirectOutput = true; |
| 3189 | } |
| 3190 | } |
| 3191 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { |
| 3192 | param.remove(String8("test_cmd_policy_input")); |
| 3193 | mTestInput = valueInt; |
| 3194 | } |
| 3195 | |
| 3196 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { |
| 3197 | param.remove(String8("test_cmd_policy_format")); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3198 | int format = AUDIO_FORMAT_INVALID; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3199 | if (value == "PCM 16 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3200 | format = AUDIO_FORMAT_PCM_16_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3201 | } else if (value == "PCM 8 bits") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3202 | format = AUDIO_FORMAT_PCM_8_BIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3203 | } else if (value == "Compressed MP3") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3204 | format = AUDIO_FORMAT_MP3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3205 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3206 | if (format != AUDIO_FORMAT_INVALID) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3207 | if (target == "Manager") { |
| 3208 | mTestFormat = format; |
| 3209 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3210 | AudioParameter outputParam = AudioParameter(); |
| 3211 | outputParam.addInt(String8("format"), format); |
| 3212 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3213 | } |
| 3214 | } |
| 3215 | } |
| 3216 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { |
| 3217 | param.remove(String8("test_cmd_policy_channels")); |
| 3218 | int channels = 0; |
| 3219 | |
| 3220 | if (value == "Channels Stereo") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3221 | channels = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3222 | } else if (value == "Channels Mono") { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3223 | channels = AUDIO_CHANNEL_OUT_MONO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3224 | } |
| 3225 | if (channels != 0) { |
| 3226 | if (target == "Manager") { |
| 3227 | mTestChannels = channels; |
| 3228 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3229 | AudioParameter outputParam = AudioParameter(); |
| 3230 | outputParam.addInt(String8("channels"), channels); |
| 3231 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3232 | } |
| 3233 | } |
| 3234 | } |
| 3235 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { |
| 3236 | param.remove(String8("test_cmd_policy_sampleRate")); |
| 3237 | if (valueInt >= 0 && valueInt <= 96000) { |
| 3238 | int samplingRate = valueInt; |
| 3239 | if (target == "Manager") { |
| 3240 | mTestSamplingRate = samplingRate; |
| 3241 | } else if (mTestOutputs[mCurOutput] != 0) { |
| 3242 | AudioParameter outputParam = AudioParameter(); |
| 3243 | outputParam.addInt(String8("sampling_rate"), samplingRate); |
| 3244 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); |
| 3245 | } |
| 3246 | } |
| 3247 | } |
| 3248 | |
| 3249 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { |
| 3250 | param.remove(String8("test_cmd_policy_reopen")); |
| 3251 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3252 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3253 | mpClientInterface->closeOutput(mPrimaryOutput); |
| 3254 | |
| 3255 | audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle; |
| 3256 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3257 | mOutputs.removeItem(mPrimaryOutput); |
| 3258 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3259 | sp<AudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3260 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3261 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3262 | config.sample_rate = outputDesc->mSamplingRate; |
| 3263 | config.channel_mask = outputDesc->mChannelMask; |
| 3264 | config.format = outputDesc->mFormat; |
| 3265 | status_t status = mpClientInterface->openOutput(moduleHandle, |
| 3266 | &mPrimaryOutput, |
| 3267 | &config, |
| 3268 | &outputDesc->mDevice, |
| 3269 | String8(""), |
| 3270 | &outputDesc->mLatency, |
| 3271 | outputDesc->mFlags); |
| 3272 | if (status != NO_ERROR) { |
| 3273 | ALOGE("Failed to reopen hardware output stream, " |
| 3274 | "samplingRate: %d, format %d, channels %d", |
| 3275 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3276 | } else { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3277 | outputDesc->mSamplingRate = config.sample_rate; |
| 3278 | outputDesc->mChannelMask = config.channel_mask; |
| 3279 | outputDesc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3280 | AudioParameter outputCmd = AudioParameter(); |
| 3281 | outputCmd.addInt(String8("set_id"), 0); |
| 3282 | mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString()); |
| 3283 | addOutput(mPrimaryOutput, outputDesc); |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | |
| 3288 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); |
| 3289 | } |
| 3290 | } |
| 3291 | return false; |
| 3292 | } |
| 3293 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3294 | void AudioPolicyManager::exit() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3295 | { |
| 3296 | { |
| 3297 | AutoMutex _l(mLock); |
| 3298 | requestExit(); |
| 3299 | mWaitWorkCV.signal(); |
| 3300 | } |
| 3301 | requestExitAndWait(); |
| 3302 | } |
| 3303 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3304 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3305 | { |
| 3306 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { |
| 3307 | if (output == mTestOutputs[i]) return i; |
| 3308 | } |
| 3309 | return 0; |
| 3310 | } |
| 3311 | #endif //AUDIO_POLICY_TEST |
| 3312 | |
| 3313 | // --- |
| 3314 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3315 | void AudioPolicyManager::addOutput(audio_io_handle_t output, sp<AudioOutputDescriptor> outputDesc) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3316 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3317 | outputDesc->mIoHandle = output; |
| 3318 | outputDesc->mId = nextUniqueId(); |
| 3319 | mOutputs.add(output, outputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3320 | nextAudioPortGeneration(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3321 | } |
| 3322 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3323 | void AudioPolicyManager::addInput(audio_io_handle_t input, sp<AudioInputDescriptor> inputDesc) |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3324 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3325 | inputDesc->mIoHandle = input; |
| 3326 | inputDesc->mId = nextUniqueId(); |
| 3327 | mInputs.add(input, inputDesc); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3328 | nextAudioPortGeneration(); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3329 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3330 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3331 | void AudioPolicyManager::findIoHandlesByAddress(sp<AudioOutputDescriptor> desc /*in*/, |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3332 | const audio_devices_t device /*in*/, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3333 | const String8 address /*in*/, |
| 3334 | SortedVector<audio_io_handle_t>& outputs /*out*/) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3335 | sp<DeviceDescriptor> devDesc = |
| 3336 | desc->mProfile->mSupportedDevices.getDevice(device, address); |
| 3337 | if (devDesc != 0) { |
| 3338 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", |
| 3339 | desc->mIoHandle, address.string()); |
| 3340 | outputs.add(desc->mIoHandle); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3341 | } |
| 3342 | } |
| 3343 | |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3344 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor> devDesc, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3345 | audio_policy_dev_state_t state, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3346 | SortedVector<audio_io_handle_t>& outputs, |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3347 | const String8 address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3348 | { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3349 | audio_devices_t device = devDesc->mDeviceType; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3350 | sp<AudioOutputDescriptor> desc; |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3351 | // erase all current sample rates, formats and channel masks |
| 3352 | devDesc->clearCapabilities(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3353 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3354 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3355 | // first list already open outputs that can be routed to this device |
| 3356 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3357 | desc = mOutputs.valueAt(i); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3358 | if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices.types() & device)) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3359 | if (!deviceDistinguishesOnAddress(device)) { |
| 3360 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); |
| 3361 | outputs.add(mOutputs.keyAt(i)); |
| 3362 | } else { |
| 3363 | ALOGV(" checking address match due to device 0x%x", device); |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3364 | findIoHandlesByAddress(desc, device, address, outputs); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3365 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3366 | } |
| 3367 | } |
| 3368 | // then look for output profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3369 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3370 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3371 | { |
| 3372 | if (mHwModules[i]->mHandle == 0) { |
| 3373 | continue; |
| 3374 | } |
| 3375 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3376 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3377 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
| 3378 | if (profile->mSupportedDevices.types() & device) { |
| 3379 | if (!deviceDistinguishesOnAddress(device) || |
| 3380 | address == profile->mSupportedDevices[0]->mAddress) { |
| 3381 | profiles.add(profile); |
| 3382 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); |
| 3383 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3384 | } |
| 3385 | } |
| 3386 | } |
| 3387 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3388 | ALOGV(" found %d profiles, %d outputs", profiles.size(), outputs.size()); |
| 3389 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3390 | if (profiles.isEmpty() && outputs.isEmpty()) { |
| 3391 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3392 | return BAD_VALUE; |
| 3393 | } |
| 3394 | |
| 3395 | // open outputs for matching profiles if needed. Direct outputs are also opened to |
| 3396 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3397 | 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] | 3398 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3399 | |
| 3400 | // nothing to do if one output is already opened for this profile |
| 3401 | size_t j; |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3402 | for (j = 0; j < outputs.size(); j++) { |
| 3403 | desc = mOutputs.valueFor(outputs.itemAt(j)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3404 | if (!desc->isDuplicated() && desc->mProfile == profile) { |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3405 | // matching profile: save the sample rates, format and channel masks supported |
| 3406 | // by the profile in our device descriptor |
| 3407 | devDesc->importAudioPort(profile); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3408 | break; |
| 3409 | } |
| 3410 | } |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3411 | if (j != outputs.size()) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3412 | continue; |
| 3413 | } |
| 3414 | |
Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3415 | ALOGV("opening output for device %08x with params %s profile %p", |
| 3416 | device, address.string(), profile.get()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3417 | desc = new AudioOutputDescriptor(profile); |
| 3418 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3419 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3420 | config.sample_rate = desc->mSamplingRate; |
| 3421 | config.channel_mask = desc->mChannelMask; |
| 3422 | config.format = desc->mFormat; |
| 3423 | config.offload_info.sample_rate = desc->mSamplingRate; |
| 3424 | config.offload_info.channel_mask = desc->mChannelMask; |
| 3425 | config.offload_info.format = desc->mFormat; |
| 3426 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; |
| 3427 | status_t status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3428 | &output, |
| 3429 | &config, |
| 3430 | &desc->mDevice, |
| 3431 | address, |
| 3432 | &desc->mLatency, |
| 3433 | desc->mFlags); |
| 3434 | if (status == NO_ERROR) { |
| 3435 | desc->mSamplingRate = config.sample_rate; |
| 3436 | desc->mChannelMask = config.channel_mask; |
| 3437 | desc->mFormat = config.format; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3438 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3439 | // Here is where the out_set_parameters() for card & device gets called |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3440 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3441 | char *param = audio_device_address_to_parameter(device, address); |
| 3442 | mpClientInterface->setParameters(output, String8(param)); |
| 3443 | free(param); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3444 | } |
| 3445 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3446 | // Here is where we step through and resolve any "dynamic" fields |
| 3447 | String8 reply; |
| 3448 | char *value; |
| 3449 | if (profile->mSamplingRates[0] == 0) { |
| 3450 | reply = mpClientInterface->getParameters(output, |
| 3451 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3452 | ALOGV("checkOutputsForDevice() supported sampling rates %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3453 | reply.string()); |
| 3454 | value = strpbrk((char *)reply.string(), "="); |
| 3455 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3456 | profile->loadSamplingRates(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3457 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3458 | } |
| 3459 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3460 | reply = mpClientInterface->getParameters(output, |
| 3461 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3462 | ALOGV("checkOutputsForDevice() supported formats %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3463 | reply.string()); |
| 3464 | value = strpbrk((char *)reply.string(), "="); |
| 3465 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3466 | profile->loadFormats(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3467 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3468 | } |
| 3469 | if (profile->mChannelMasks[0] == 0) { |
| 3470 | reply = mpClientInterface->getParameters(output, |
| 3471 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3472 | ALOGV("checkOutputsForDevice() supported channel masks %s", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3473 | reply.string()); |
| 3474 | value = strpbrk((char *)reply.string(), "="); |
| 3475 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3476 | profile->loadOutChannels(value + 1); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3477 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3478 | } |
| 3479 | if (((profile->mSamplingRates[0] == 0) && |
| 3480 | (profile->mSamplingRates.size() < 2)) || |
| 3481 | ((profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) && |
| 3482 | (profile->mFormats.size() < 2)) || |
| 3483 | ((profile->mChannelMasks[0] == 0) && |
| 3484 | (profile->mChannelMasks.size() < 2))) { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3485 | ALOGW("checkOutputsForDevice() missing param"); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3486 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3487 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | 1e693b5 | 2014-07-09 15:03:28 -0700 | [diff] [blame] | 3488 | } else if (profile->mSamplingRates[0] == 0 || profile->mFormats[0] == 0 || |
| 3489 | profile->mChannelMasks[0] == 0) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3490 | mpClientInterface->closeOutput(output); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3491 | config.sample_rate = profile->pickSamplingRate(); |
| 3492 | config.channel_mask = profile->pickChannelMask(); |
| 3493 | config.format = profile->pickFormat(); |
| 3494 | config.offload_info.sample_rate = config.sample_rate; |
| 3495 | config.offload_info.channel_mask = config.channel_mask; |
| 3496 | config.offload_info.format = config.format; |
| 3497 | status = mpClientInterface->openOutput(profile->mModule->mHandle, |
| 3498 | &output, |
| 3499 | &config, |
| 3500 | &desc->mDevice, |
| 3501 | address, |
| 3502 | &desc->mLatency, |
| 3503 | desc->mFlags); |
| 3504 | if (status == NO_ERROR) { |
| 3505 | desc->mSamplingRate = config.sample_rate; |
| 3506 | desc->mChannelMask = config.channel_mask; |
| 3507 | desc->mFormat = config.format; |
| 3508 | } else { |
| 3509 | output = AUDIO_IO_HANDLE_NONE; |
| 3510 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3513 | if (output != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3514 | addOutput(output, desc); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3515 | if (deviceDistinguishesOnAddress(device) && address != "0") { |
| 3516 | ssize_t index = mPolicyMixes.indexOfKey(address); |
| 3517 | if (index >= 0) { |
| 3518 | mPolicyMixes[index]->mOutput = desc; |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3519 | desc->mPolicyMix = &mPolicyMixes[index]->mMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3520 | } else { |
| 3521 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", |
| 3522 | address.string()); |
| 3523 | } |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 3524 | } else if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) { |
| 3525 | // no duplicated output for direct outputs and |
| 3526 | // outputs used by dynamic policy mixes |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3527 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3528 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3529 | // set initial stream volume for device |
| 3530 | applyStreamVolumes(output, device, 0, true); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3531 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3532 | //TODO: configure audio effect output stage here |
| 3533 | |
| 3534 | // open a duplicating output thread for the new output and the primary output |
| 3535 | duplicatedOutput = mpClientInterface->openDuplicateOutput(output, |
| 3536 | mPrimaryOutput); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3537 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3538 | // add duplicated output descriptor |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3539 | sp<AudioOutputDescriptor> dupOutputDesc = |
| 3540 | new AudioOutputDescriptor(NULL); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3541 | dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput); |
| 3542 | dupOutputDesc->mOutput2 = mOutputs.valueFor(output); |
| 3543 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; |
| 3544 | dupOutputDesc->mFormat = desc->mFormat; |
| 3545 | dupOutputDesc->mChannelMask = desc->mChannelMask; |
| 3546 | dupOutputDesc->mLatency = desc->mLatency; |
| 3547 | addOutput(duplicatedOutput, dupOutputDesc); |
| 3548 | applyStreamVolumes(duplicatedOutput, device, 0, true); |
| 3549 | } else { |
| 3550 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", |
| 3551 | mPrimaryOutput, output); |
| 3552 | mpClientInterface->closeOutput(output); |
| 3553 | mOutputs.removeItem(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3554 | nextAudioPortGeneration(); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3555 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3556 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3557 | } |
| 3558 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3559 | } else { |
| 3560 | output = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3561 | } |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3562 | if (output == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3563 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3564 | profiles.removeAt(profile_index); |
| 3565 | profile_index--; |
| 3566 | } else { |
| 3567 | outputs.add(output); |
Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 3568 | devDesc->importAudioPort(profile); |
| 3569 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3570 | if (deviceDistinguishesOnAddress(device)) { |
| 3571 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", |
| 3572 | device, address.string()); |
| 3573 | setOutputDevice(output, device, true/*force*/, 0/*delay*/, |
| 3574 | NULL/*patch handle*/, address.string()); |
| 3575 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3576 | ALOGV("checkOutputsForDevice(): adding output %d", output); |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | if (profiles.isEmpty()) { |
| 3581 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); |
| 3582 | return BAD_VALUE; |
| 3583 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3584 | } else { // Disconnect |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3585 | // check if one opened output is not needed any more after disconnecting one device |
| 3586 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 3587 | desc = mOutputs.valueAt(i); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3588 | if (!desc->isDuplicated()) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3589 | // exact match on device |
| 3590 | if (deviceDistinguishesOnAddress(device) && |
| 3591 | (desc->mProfile->mSupportedDevices.types() == device)) { |
keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3592 | findIoHandlesByAddress(desc, device, address, outputs); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3593 | } else if (!(desc->mProfile->mSupportedDevices.types() |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3594 | & mAvailableOutputDevices.types())) { |
| 3595 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", |
| 3596 | mOutputs.keyAt(i)); |
| 3597 | outputs.add(mOutputs.keyAt(i)); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3598 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3599 | } |
| 3600 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3601 | // Clear any profiles associated with the disconnected device. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3602 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 3603 | { |
| 3604 | if (mHwModules[i]->mHandle == 0) { |
| 3605 | continue; |
| 3606 | } |
| 3607 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) |
| 3608 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3609 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3610 | if (profile->mSupportedDevices.types() & device) { |
| 3611 | ALOGV("checkOutputsForDevice(): " |
| 3612 | "clearing direct output profile %zu on module %zu", j, i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3613 | if (profile->mSamplingRates[0] == 0) { |
| 3614 | profile->mSamplingRates.clear(); |
| 3615 | profile->mSamplingRates.add(0); |
| 3616 | } |
| 3617 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3618 | profile->mFormats.clear(); |
| 3619 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3620 | } |
| 3621 | if (profile->mChannelMasks[0] == 0) { |
| 3622 | profile->mChannelMasks.clear(); |
| 3623 | profile->mChannelMasks.add(0); |
| 3624 | } |
| 3625 | } |
| 3626 | } |
| 3627 | } |
| 3628 | } |
| 3629 | return NO_ERROR; |
| 3630 | } |
| 3631 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3632 | status_t AudioPolicyManager::checkInputsForDevice(audio_devices_t device, |
| 3633 | audio_policy_dev_state_t state, |
| 3634 | SortedVector<audio_io_handle_t>& inputs, |
| 3635 | const String8 address) |
| 3636 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3637 | sp<AudioInputDescriptor> desc; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3638 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { |
| 3639 | // first list already open inputs that can be routed to this device |
| 3640 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3641 | desc = mInputs.valueAt(input_index); |
| 3642 | if (desc->mProfile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
| 3643 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); |
| 3644 | inputs.add(mInputs.keyAt(input_index)); |
| 3645 | } |
| 3646 | } |
| 3647 | |
| 3648 | // then look for input profiles that can be routed to this device |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3649 | SortedVector< sp<IOProfile> > profiles; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3650 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) |
| 3651 | { |
| 3652 | if (mHwModules[module_idx]->mHandle == 0) { |
| 3653 | continue; |
| 3654 | } |
| 3655 | for (size_t profile_index = 0; |
| 3656 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); |
| 3657 | profile_index++) |
| 3658 | { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3659 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; |
| 3660 | |
| 3661 | if (profile->mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN)) { |
| 3662 | if (!deviceDistinguishesOnAddress(device) || |
| 3663 | address == profile->mSupportedDevices[0]->mAddress) { |
| 3664 | profiles.add(profile); |
| 3665 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", |
| 3666 | profile_index, module_idx); |
| 3667 | } |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | |
| 3672 | if (profiles.isEmpty() && inputs.isEmpty()) { |
| 3673 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3674 | return BAD_VALUE; |
| 3675 | } |
| 3676 | |
| 3677 | // open inputs for matching profiles if needed. Direct inputs are also opened to |
| 3678 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() |
| 3679 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { |
| 3680 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3681 | sp<IOProfile> profile = profiles[profile_index]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3682 | // nothing to do if one input is already opened for this profile |
| 3683 | size_t input_index; |
| 3684 | for (input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3685 | desc = mInputs.valueAt(input_index); |
| 3686 | if (desc->mProfile == profile) { |
| 3687 | break; |
| 3688 | } |
| 3689 | } |
| 3690 | if (input_index != mInputs.size()) { |
| 3691 | continue; |
| 3692 | } |
| 3693 | |
| 3694 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); |
| 3695 | desc = new AudioInputDescriptor(profile); |
| 3696 | desc->mDevice = device; |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3697 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; |
| 3698 | config.sample_rate = desc->mSamplingRate; |
| 3699 | config.channel_mask = desc->mChannelMask; |
| 3700 | config.format = desc->mFormat; |
| 3701 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; |
| 3702 | status_t status = mpClientInterface->openInput(profile->mModule->mHandle, |
| 3703 | &input, |
| 3704 | &config, |
| 3705 | &desc->mDevice, |
| 3706 | address, |
| 3707 | AUDIO_SOURCE_MIC, |
| 3708 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3709 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3710 | if (status == NO_ERROR) { |
| 3711 | desc->mSamplingRate = config.sample_rate; |
| 3712 | desc->mChannelMask = config.channel_mask; |
| 3713 | desc->mFormat = config.format; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3714 | |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3715 | if (!address.isEmpty()) { |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3716 | char *param = audio_device_address_to_parameter(device, address); |
| 3717 | mpClientInterface->setParameters(input, String8(param)); |
| 3718 | free(param); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3719 | } |
| 3720 | |
| 3721 | // Here is where we step through and resolve any "dynamic" fields |
| 3722 | String8 reply; |
| 3723 | char *value; |
| 3724 | if (profile->mSamplingRates[0] == 0) { |
| 3725 | reply = mpClientInterface->getParameters(input, |
| 3726 | String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES)); |
| 3727 | ALOGV("checkInputsForDevice() direct input sup sampling rates %s", |
| 3728 | reply.string()); |
| 3729 | value = strpbrk((char *)reply.string(), "="); |
| 3730 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3731 | profile->loadSamplingRates(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3732 | } |
| 3733 | } |
| 3734 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3735 | reply = mpClientInterface->getParameters(input, |
| 3736 | String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS)); |
| 3737 | ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string()); |
| 3738 | value = strpbrk((char *)reply.string(), "="); |
| 3739 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3740 | profile->loadFormats(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3741 | } |
| 3742 | } |
| 3743 | if (profile->mChannelMasks[0] == 0) { |
| 3744 | reply = mpClientInterface->getParameters(input, |
| 3745 | String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS)); |
| 3746 | ALOGV("checkInputsForDevice() direct input sup channel masks %s", |
| 3747 | reply.string()); |
| 3748 | value = strpbrk((char *)reply.string(), "="); |
| 3749 | if (value != NULL) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3750 | profile->loadInChannels(value + 1); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) || |
| 3754 | ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) || |
| 3755 | ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) { |
| 3756 | ALOGW("checkInputsForDevice() direct input missing param"); |
| 3757 | mpClientInterface->closeInput(input); |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3758 | input = AUDIO_IO_HANDLE_NONE; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
| 3761 | if (input != 0) { |
| 3762 | addInput(input, desc); |
| 3763 | } |
| 3764 | } // endif input != 0 |
| 3765 | |
Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3766 | if (input == AUDIO_IO_HANDLE_NONE) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3767 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3768 | profiles.removeAt(profile_index); |
| 3769 | profile_index--; |
| 3770 | } else { |
| 3771 | inputs.add(input); |
| 3772 | ALOGV("checkInputsForDevice(): adding input %d", input); |
| 3773 | } |
| 3774 | } // end scan profiles |
| 3775 | |
| 3776 | if (profiles.isEmpty()) { |
| 3777 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); |
| 3778 | return BAD_VALUE; |
| 3779 | } |
| 3780 | } else { |
| 3781 | // Disconnect |
| 3782 | // check if one opened input is not needed any more after disconnecting one device |
| 3783 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { |
| 3784 | desc = mInputs.valueAt(input_index); |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3785 | if (!(desc->mProfile->mSupportedDevices.types() & mAvailableInputDevices.types() & |
| 3786 | ~AUDIO_DEVICE_BIT_IN)) { |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3787 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", |
| 3788 | mInputs.keyAt(input_index)); |
| 3789 | inputs.add(mInputs.keyAt(input_index)); |
| 3790 | } |
| 3791 | } |
| 3792 | // Clear any profiles associated with the disconnected device. |
| 3793 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { |
| 3794 | if (mHwModules[module_index]->mHandle == 0) { |
| 3795 | continue; |
| 3796 | } |
| 3797 | for (size_t profile_index = 0; |
| 3798 | profile_index < mHwModules[module_index]->mInputProfiles.size(); |
| 3799 | profile_index++) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3800 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 3801 | if (profile->mSupportedDevices.types() & device & ~AUDIO_DEVICE_BIT_IN) { |
Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 3802 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3803 | profile_index, module_index); |
| 3804 | if (profile->mSamplingRates[0] == 0) { |
| 3805 | profile->mSamplingRates.clear(); |
| 3806 | profile->mSamplingRates.add(0); |
| 3807 | } |
| 3808 | if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) { |
| 3809 | profile->mFormats.clear(); |
| 3810 | profile->mFormats.add(AUDIO_FORMAT_DEFAULT); |
| 3811 | } |
| 3812 | if (profile->mChannelMasks[0] == 0) { |
| 3813 | profile->mChannelMasks.clear(); |
| 3814 | profile->mChannelMasks.add(0); |
| 3815 | } |
| 3816 | } |
| 3817 | } |
| 3818 | } |
| 3819 | } // end disconnect |
| 3820 | |
| 3821 | return NO_ERROR; |
| 3822 | } |
| 3823 | |
| 3824 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3825 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3826 | { |
| 3827 | ALOGV("closeOutput(%d)", output); |
| 3828 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3829 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3830 | if (outputDesc == NULL) { |
| 3831 | ALOGW("closeOutput() unknown output %d", output); |
| 3832 | return; |
| 3833 | } |
| 3834 | |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3835 | for (size_t i = 0; i < mPolicyMixes.size(); i++) { |
| 3836 | if (mPolicyMixes[i]->mOutput == outputDesc) { |
| 3837 | mPolicyMixes[i]->mOutput.clear(); |
| 3838 | } |
| 3839 | } |
| 3840 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3841 | // look for duplicated outputs connected to the output being removed. |
| 3842 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3843 | sp<AudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3844 | if (dupOutputDesc->isDuplicated() && |
| 3845 | (dupOutputDesc->mOutput1 == outputDesc || |
| 3846 | dupOutputDesc->mOutput2 == outputDesc)) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3847 | sp<AudioOutputDescriptor> outputDesc2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3848 | if (dupOutputDesc->mOutput1 == outputDesc) { |
| 3849 | outputDesc2 = dupOutputDesc->mOutput2; |
| 3850 | } else { |
| 3851 | outputDesc2 = dupOutputDesc->mOutput1; |
| 3852 | } |
| 3853 | // As all active tracks on duplicated output will be deleted, |
| 3854 | // and as they were also referenced on the other output, the reference |
| 3855 | // count for their stream type must be adjusted accordingly on |
| 3856 | // the other output. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3857 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3858 | int refCount = dupOutputDesc->mRefCount[j]; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3859 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3860 | } |
| 3861 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); |
| 3862 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); |
| 3863 | |
| 3864 | mpClientInterface->closeOutput(duplicatedOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3865 | mOutputs.removeItem(duplicatedOutput); |
| 3866 | } |
| 3867 | } |
| 3868 | |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3869 | nextAudioPortGeneration(); |
| 3870 | |
| 3871 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 3872 | if (index >= 0) { |
| 3873 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3874 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3875 | mAudioPatches.removeItemsAt(index); |
| 3876 | mpClientInterface->onAudioPatchListUpdate(); |
| 3877 | } |
| 3878 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3879 | AudioParameter param; |
| 3880 | param.add(String8("closing"), String8("true")); |
| 3881 | mpClientInterface->setParameters(output, param.toString()); |
| 3882 | |
| 3883 | mpClientInterface->closeOutput(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3884 | mOutputs.removeItem(output); |
| 3885 | mPreviousOutputs = mOutputs; |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | void AudioPolicyManager::closeInput(audio_io_handle_t input) |
| 3889 | { |
| 3890 | ALOGV("closeInput(%d)", input); |
| 3891 | |
| 3892 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
| 3893 | if (inputDesc == NULL) { |
| 3894 | ALOGW("closeInput() unknown input %d", input); |
| 3895 | return; |
| 3896 | } |
| 3897 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3898 | nextAudioPortGeneration(); |
Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 3899 | |
| 3900 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 3901 | if (index >= 0) { |
| 3902 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 3903 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
| 3904 | mAudioPatches.removeItemsAt(index); |
| 3905 | mpClientInterface->onAudioPatchListUpdate(); |
| 3906 | } |
| 3907 | |
| 3908 | mpClientInterface->closeInput(input); |
| 3909 | mInputs.removeItem(input); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3910 | } |
| 3911 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3912 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice(audio_devices_t device, |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3913 | DefaultKeyedVector<audio_io_handle_t, sp<AudioOutputDescriptor> > openOutputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3914 | { |
| 3915 | SortedVector<audio_io_handle_t> outputs; |
| 3916 | |
| 3917 | ALOGVV("getOutputsForDevice() device %04x", device); |
| 3918 | for (size_t i = 0; i < openOutputs.size(); i++) { |
| 3919 | ALOGVV("output %d isDuplicated=%d device=%04x", |
| 3920 | i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices()); |
| 3921 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { |
| 3922 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); |
| 3923 | outputs.add(openOutputs.keyAt(i)); |
| 3924 | } |
| 3925 | } |
| 3926 | return outputs; |
| 3927 | } |
| 3928 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3929 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3930 | SortedVector<audio_io_handle_t>& outputs2) |
| 3931 | { |
| 3932 | if (outputs1.size() != outputs2.size()) { |
| 3933 | return false; |
| 3934 | } |
| 3935 | for (size_t i = 0; i < outputs1.size(); i++) { |
| 3936 | if (outputs1[i] != outputs2[i]) { |
| 3937 | return false; |
| 3938 | } |
| 3939 | } |
| 3940 | return true; |
| 3941 | } |
| 3942 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3943 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3944 | { |
| 3945 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 3946 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); |
| 3947 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); |
| 3948 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); |
| 3949 | |
Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 3950 | // also take into account external policy-related changes: add all outputs which are |
| 3951 | // associated with policies in the "before" and "after" output vectors |
| 3952 | ALOGVV("checkOutputForStrategy(): policy related outputs"); |
| 3953 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { |
| 3954 | const sp<AudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); |
| 3955 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 3956 | srcOutputs.add(desc->mIoHandle); |
| 3957 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); |
| 3958 | } |
| 3959 | } |
| 3960 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { |
| 3961 | const sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 3962 | if (desc != 0 && desc->mPolicyMix != NULL) { |
| 3963 | dstOutputs.add(desc->mIoHandle); |
| 3964 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); |
| 3965 | } |
| 3966 | } |
| 3967 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3968 | if (!vectorsEqual(srcOutputs,dstOutputs)) { |
| 3969 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", |
| 3970 | strategy, srcOutputs[0], dstOutputs[0]); |
| 3971 | // mute strategy while moving tracks from one output to another |
| 3972 | for (size_t i = 0; i < srcOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3973 | sp<AudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3974 | if (desc->isStrategyActive(strategy)) { |
| 3975 | setStrategyMute(strategy, true, srcOutputs[i]); |
| 3976 | setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice); |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | // Move effects associated to this strategy from previous output to new output |
| 3981 | if (strategy == STRATEGY_MEDIA) { |
| 3982 | audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs); |
| 3983 | SortedVector<audio_io_handle_t> moved; |
| 3984 | for (size_t i = 0; i < mEffects.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3985 | sp<EffectDescriptor> effectDesc = mEffects.valueAt(i); |
| 3986 | if (effectDesc->mSession == AUDIO_SESSION_OUTPUT_MIX && |
| 3987 | effectDesc->mIo != fxOutput) { |
| 3988 | if (moved.indexOf(effectDesc->mIo) < 0) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3989 | ALOGV("checkOutputForStrategy() moving effect %d to output %d", |
| 3990 | mEffects.keyAt(i), fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3991 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, effectDesc->mIo, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3992 | fxOutput); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3993 | moved.add(effectDesc->mIo); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3994 | } |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3995 | effectDesc->mIo = fxOutput; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3996 | } |
| 3997 | } |
| 3998 | } |
| 3999 | // Move tracks associated to this strategy from previous output to new output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4000 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4001 | if (i == AUDIO_STREAM_PATCH) { |
| 4002 | continue; |
| 4003 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4004 | if (getStrategy((audio_stream_type_t)i) == strategy) { |
| 4005 | mpClientInterface->invalidateStream((audio_stream_type_t)i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | } |
| 4007 | } |
| 4008 | } |
| 4009 | } |
| 4010 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4011 | void AudioPolicyManager::checkOutputForAllStrategies() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4012 | { |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4013 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 4014 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4015 | checkOutputForStrategy(STRATEGY_PHONE); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4016 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) |
| 4017 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4018 | checkOutputForStrategy(STRATEGY_SONIFICATION); |
| 4019 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4020 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4021 | checkOutputForStrategy(STRATEGY_MEDIA); |
| 4022 | checkOutputForStrategy(STRATEGY_DTMF); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4023 | checkOutputForStrategy(STRATEGY_REROUTING); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4024 | } |
| 4025 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4026 | audio_io_handle_t AudioPolicyManager::getA2dpOutput() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4027 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4028 | for (size_t i = 0; i < mOutputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4029 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4030 | if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) { |
| 4031 | return mOutputs.keyAt(i); |
| 4032 | } |
| 4033 | } |
| 4034 | |
| 4035 | return 0; |
| 4036 | } |
| 4037 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4038 | void AudioPolicyManager::checkA2dpSuspend() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4039 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4040 | audio_io_handle_t a2dpOutput = getA2dpOutput(); |
| 4041 | if (a2dpOutput == 0) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4042 | mA2dpSuspended = false; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4043 | return; |
| 4044 | } |
| 4045 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4046 | bool isScoConnected = |
Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4047 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & |
| 4048 | ~AUDIO_DEVICE_BIT_IN) != 0) || |
| 4049 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4050 | // suspend A2DP output if: |
| 4051 | // (NOT already suspended) && |
| 4052 | // ((SCO device is connected && |
| 4053 | // (forced usage for communication || for record is SCO))) || |
| 4054 | // (phone state is ringing || in call) |
| 4055 | // |
| 4056 | // restore A2DP output if: |
| 4057 | // (Already suspended) && |
| 4058 | // ((SCO device is NOT connected || |
| 4059 | // (forced usage NOT for communication && NOT for record is SCO))) && |
| 4060 | // (phone state is NOT ringing && NOT in call) |
| 4061 | // |
| 4062 | if (mA2dpSuspended) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4063 | if ((!isScoConnected || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4064 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO) && |
| 4065 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] != AUDIO_POLICY_FORCE_BT_SCO))) && |
| 4066 | ((mPhoneState != AUDIO_MODE_IN_CALL) && |
| 4067 | (mPhoneState != AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4068 | |
| 4069 | mpClientInterface->restoreOutput(a2dpOutput); |
| 4070 | mA2dpSuspended = false; |
| 4071 | } |
| 4072 | } else { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4073 | if ((isScoConnected && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4074 | ((mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 4075 | (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO))) || |
| 4076 | ((mPhoneState == AUDIO_MODE_IN_CALL) || |
| 4077 | (mPhoneState == AUDIO_MODE_RINGTONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4078 | |
| 4079 | mpClientInterface->suspendOutput(a2dpOutput); |
| 4080 | mA2dpSuspended = true; |
| 4081 | } |
| 4082 | } |
| 4083 | } |
| 4084 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4085 | audio_devices_t AudioPolicyManager::getNewOutputDevice(audio_io_handle_t output, bool fromCache) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4086 | { |
| 4087 | audio_devices_t device = AUDIO_DEVICE_NONE; |
| 4088 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4089 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4090 | |
| 4091 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4092 | if (index >= 0) { |
| 4093 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4094 | if (patchDesc->mUid != mUidCached) { |
| 4095 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", |
| 4096 | outputDesc->device(), outputDesc->mPatchHandle); |
| 4097 | return outputDesc->device(); |
| 4098 | } |
| 4099 | } |
| 4100 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4101 | // 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] | 4102 | // 1: the strategy enforced audible is active and enforced on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4103 | // use device for strategy enforced audible |
| 4104 | // 2: we are in call or the strategy phone is active on the output: |
| 4105 | // use device for strategy phone |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4106 | // 3: the strategy for enforced audible is active but not enforced on the output: |
| 4107 | // use the device for strategy enforced audible |
| 4108 | // 4: the strategy sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4109 | // use device for strategy sonification |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4110 | // 5: the strategy "respectful" sonification is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4111 | // use device for strategy "respectful" sonification |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4112 | // 6: the strategy accessibility is active on the output: |
| 4113 | // use device for strategy accessibility |
| 4114 | // 7: the strategy media is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4115 | // use device for strategy media |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4116 | // 8: the strategy DTMF is active on the output: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4117 | // use device for strategy DTMF |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4118 | // 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] | 4119 | // use device for strategy t-t-s |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4120 | if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE) && |
| 4121 | mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4122 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
| 4123 | } else if (isInCall() || |
| 4124 | outputDesc->isStrategyActive(STRATEGY_PHONE)) { |
| 4125 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); |
Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4126 | } else if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) { |
| 4127 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4128 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) { |
| 4129 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); |
| 4130 | } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) { |
| 4131 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4132 | } else if (outputDesc->isStrategyActive(STRATEGY_ACCESSIBILITY)) { |
| 4133 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4134 | } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) { |
| 4135 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); |
| 4136 | } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) { |
| 4137 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4138 | } else if (outputDesc->isStrategyActive(STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { |
| 4139 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4140 | } else if (outputDesc->isStrategyActive(STRATEGY_REROUTING)) { |
| 4141 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4142 | } |
| 4143 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4144 | ALOGV("getNewOutputDevice() selected device %x", device); |
| 4145 | return device; |
| 4146 | } |
| 4147 | |
| 4148 | audio_devices_t AudioPolicyManager::getNewInputDevice(audio_io_handle_t input) |
| 4149 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4150 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4151 | |
| 4152 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4153 | if (index >= 0) { |
| 4154 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4155 | if (patchDesc->mUid != mUidCached) { |
| 4156 | ALOGV("getNewInputDevice() device %08x forced by patch %d", |
| 4157 | inputDesc->mDevice, inputDesc->mPatchHandle); |
| 4158 | return inputDesc->mDevice; |
| 4159 | } |
| 4160 | } |
| 4161 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4162 | audio_devices_t device = getDeviceAndMixForInputSource(inputDesc->mInputSource); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4163 | |
| 4164 | ALOGV("getNewInputDevice() selected device %x", device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4165 | return device; |
| 4166 | } |
| 4167 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4168 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4169 | return (uint32_t)getStrategy(stream); |
| 4170 | } |
| 4171 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4172 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4173 | // By checking the range of stream before calling getStrategy, we avoid |
| 4174 | // getStrategy's behavior for invalid streams. getStrategy would do a ALOGE |
| 4175 | // 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] | 4176 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4177 | return AUDIO_DEVICE_NONE; |
| 4178 | } |
| 4179 | audio_devices_t devices; |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 4180 | routing_strategy strategy = getStrategy(stream); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4181 | devices = getDeviceForStrategy(strategy, true /*fromCache*/); |
| 4182 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(devices, mOutputs); |
| 4183 | for (size_t i = 0; i < outputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4184 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4185 | if (outputDesc->isStrategyActive(strategy)) { |
| 4186 | devices = outputDesc->device(); |
| 4187 | break; |
| 4188 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4189 | } |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4190 | |
| 4191 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it |
| 4192 | and doesn't really need to.*/ |
| 4193 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { |
| 4194 | devices |= AUDIO_DEVICE_OUT_SPEAKER; |
| 4195 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
| 4196 | } |
| 4197 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4198 | return devices; |
| 4199 | } |
| 4200 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 4201 | routing_strategy AudioPolicyManager::getStrategy( |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4202 | audio_stream_type_t stream) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4203 | |
| 4204 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH"); |
| 4205 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4206 | // stream to strategy mapping |
| 4207 | switch (stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4208 | case AUDIO_STREAM_VOICE_CALL: |
| 4209 | case AUDIO_STREAM_BLUETOOTH_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4210 | return STRATEGY_PHONE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4211 | case AUDIO_STREAM_RING: |
| 4212 | case AUDIO_STREAM_ALARM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4213 | return STRATEGY_SONIFICATION; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4214 | case AUDIO_STREAM_NOTIFICATION: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4215 | return STRATEGY_SONIFICATION_RESPECTFUL; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4216 | case AUDIO_STREAM_DTMF: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4217 | return STRATEGY_DTMF; |
| 4218 | default: |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4219 | ALOGE("unknown stream type %d", stream); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4220 | case AUDIO_STREAM_SYSTEM: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4221 | // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs |
| 4222 | // while key clicks are played produces a poor result |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4223 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4224 | return STRATEGY_MEDIA; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4225 | case AUDIO_STREAM_ENFORCED_AUDIBLE: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4226 | return STRATEGY_ENFORCED_AUDIBLE; |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4227 | case AUDIO_STREAM_TTS: |
| 4228 | return STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4229 | case AUDIO_STREAM_ACCESSIBILITY: |
| 4230 | return STRATEGY_ACCESSIBILITY; |
| 4231 | case AUDIO_STREAM_REROUTING: |
| 4232 | return STRATEGY_REROUTING; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4233 | } |
| 4234 | } |
| 4235 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4236 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { |
| 4237 | // flags to strategy mapping |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4238 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 4239 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; |
| 4240 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4241 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 4242 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; |
| 4243 | } |
| 4244 | |
| 4245 | // usage to strategy mapping |
| 4246 | switch (attr->usage) { |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4247 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 4248 | if (isStreamActive(AUDIO_STREAM_RING) || isStreamActive(AUDIO_STREAM_ALARM)) { |
| 4249 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4250 | } |
| 4251 | if (isInCall()) { |
| 4252 | return (uint32_t) STRATEGY_PHONE; |
| 4253 | } |
Eric Laurent | 0f78eab | 2014-11-25 11:01:34 -0800 | [diff] [blame] | 4254 | return (uint32_t) STRATEGY_ACCESSIBILITY; |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4255 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4256 | case AUDIO_USAGE_MEDIA: |
| 4257 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4258 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 4259 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 4260 | return (uint32_t) STRATEGY_MEDIA; |
| 4261 | |
| 4262 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 4263 | return (uint32_t) STRATEGY_PHONE; |
| 4264 | |
| 4265 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 4266 | return (uint32_t) STRATEGY_DTMF; |
| 4267 | |
| 4268 | case AUDIO_USAGE_ALARM: |
| 4269 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 4270 | return (uint32_t) STRATEGY_SONIFICATION; |
| 4271 | |
| 4272 | case AUDIO_USAGE_NOTIFICATION: |
| 4273 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 4274 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 4275 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 4276 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 4277 | return (uint32_t) STRATEGY_SONIFICATION_RESPECTFUL; |
| 4278 | |
| 4279 | case AUDIO_USAGE_UNKNOWN: |
| 4280 | default: |
| 4281 | return (uint32_t) STRATEGY_MEDIA; |
| 4282 | } |
| 4283 | } |
| 4284 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4285 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4286 | switch(stream) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4287 | case AUDIO_STREAM_MUSIC: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4288 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); |
| 4289 | updateDevicesAndOutputs(); |
| 4290 | break; |
| 4291 | default: |
| 4292 | break; |
| 4293 | } |
| 4294 | } |
| 4295 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4296 | bool AudioPolicyManager::isAnyOutputActive(audio_stream_type_t streamToIgnore) { |
| 4297 | for (size_t s = 0 ; s < AUDIO_STREAM_CNT ; s++) { |
| 4298 | if (s == (size_t) streamToIgnore) { |
| 4299 | continue; |
| 4300 | } |
| 4301 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4302 | const sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); |
| 4303 | if (outputDesc->mRefCount[s] != 0) { |
| 4304 | return true; |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | return false; |
| 4309 | } |
| 4310 | |
| 4311 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { |
| 4312 | switch(event) { |
| 4313 | case STARTING_OUTPUT: |
| 4314 | mBeaconMuteRefCount++; |
| 4315 | break; |
| 4316 | case STOPPING_OUTPUT: |
| 4317 | if (mBeaconMuteRefCount > 0) { |
| 4318 | mBeaconMuteRefCount--; |
| 4319 | } |
| 4320 | break; |
| 4321 | case STARTING_BEACON: |
| 4322 | mBeaconPlayingRefCount++; |
| 4323 | break; |
| 4324 | case STOPPING_BEACON: |
| 4325 | if (mBeaconPlayingRefCount > 0) { |
| 4326 | mBeaconPlayingRefCount--; |
| 4327 | } |
| 4328 | break; |
| 4329 | } |
| 4330 | |
| 4331 | if (mBeaconMuteRefCount > 0) { |
| 4332 | // any playback causes beacon to be muted |
| 4333 | return setBeaconMute(true); |
| 4334 | } else { |
| 4335 | // no other playback: unmute when beacon starts playing, mute when it stops |
| 4336 | return setBeaconMute(mBeaconPlayingRefCount == 0); |
| 4337 | } |
| 4338 | } |
| 4339 | |
| 4340 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { |
| 4341 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", |
| 4342 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); |
| 4343 | // keep track of muted state to avoid repeating mute/unmute operations |
| 4344 | if (mBeaconMuted != mute) { |
| 4345 | // mute/unmute AUDIO_STREAM_TTS on all outputs |
| 4346 | ALOGV("\t muting %d", mute); |
| 4347 | uint32_t maxLatency = 0; |
| 4348 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4349 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4350 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, |
| 4351 | desc->mIoHandle, |
| 4352 | 0 /*delay*/, AUDIO_DEVICE_NONE); |
| 4353 | const uint32_t latency = desc->latency() * 2; |
| 4354 | if (latency > maxLatency) { |
| 4355 | maxLatency = latency; |
| 4356 | } |
| 4357 | } |
| 4358 | mBeaconMuted = mute; |
| 4359 | return maxLatency; |
| 4360 | } |
| 4361 | return 0; |
| 4362 | } |
| 4363 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4364 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4365 | bool fromCache) |
| 4366 | { |
| 4367 | uint32_t device = AUDIO_DEVICE_NONE; |
| 4368 | |
| 4369 | if (fromCache) { |
| 4370 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", |
| 4371 | strategy, mDeviceForStrategy[strategy]); |
| 4372 | return mDeviceForStrategy[strategy]; |
| 4373 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4374 | audio_devices_t availableOutputDeviceTypes = mAvailableOutputDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4375 | switch (strategy) { |
| 4376 | |
Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4377 | case STRATEGY_TRANSMITTED_THROUGH_SPEAKER: |
| 4378 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4379 | if (!device) { |
| 4380 | ALOGE("getDeviceForStrategy() no device found for "\ |
| 4381 | "STRATEGY_TRANSMITTED_THROUGH_SPEAKER"); |
| 4382 | } |
| 4383 | break; |
| 4384 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4385 | case STRATEGY_SONIFICATION_RESPECTFUL: |
| 4386 | if (isInCall()) { |
| 4387 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4388 | } else if (isStreamActiveRemotely(AUDIO_STREAM_MUSIC, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4389 | SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
| 4390 | // while media is playing on a remote device, use the the sonification behavior. |
| 4391 | // Note that we test this usecase before testing if media is playing because |
| 4392 | // the isStreamActive() method only informs about the activity of a stream, not |
| 4393 | // if it's for local playback. Note also that we use the same delay between both tests |
| 4394 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4395 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4396 | //other acoustic safety mechanisms for notification |
| 4397 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4398 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4399 | } else if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4400 | // while media is playing (or has recently played), use the same device |
| 4401 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4402 | } else { |
| 4403 | // when media is not playing anymore, fall back on the sonification behavior |
| 4404 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/); |
Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4405 | //user "safe" speaker if available instead of normal speaker to avoid triggering |
| 4406 | //other acoustic safety mechanisms for notification |
| 4407 | if (device == AUDIO_DEVICE_OUT_SPEAKER && (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) |
| 4408 | device = AUDIO_DEVICE_OUT_SPEAKER_SAFE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4409 | } |
| 4410 | |
| 4411 | break; |
| 4412 | |
| 4413 | case STRATEGY_DTMF: |
| 4414 | if (!isInCall()) { |
| 4415 | // when off call, DTMF strategy follows the same rules as MEDIA strategy |
| 4416 | device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/); |
| 4417 | break; |
| 4418 | } |
| 4419 | // when in call, DTMF and PHONE strategies follow the same rules |
| 4420 | // FALL THROUGH |
| 4421 | |
| 4422 | case STRATEGY_PHONE: |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4423 | // Force use of only devices on primary output if: |
| 4424 | // - in call AND |
| 4425 | // - cannot route from voice call RX OR |
| 4426 | // - audio HAL version is < 3.0 and TX device is on the primary HW module |
| 4427 | if (mPhoneState == AUDIO_MODE_IN_CALL) { |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 4428 | audio_devices_t txDevice = |
| 4429 | getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4430 | sp<AudioOutputDescriptor> hwOutputDesc = mOutputs.valueFor(mPrimaryOutput); |
| 4431 | if (((mAvailableInputDevices.types() & |
| 4432 | AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) || |
| 4433 | (((txDevice & availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN) != 0) && |
Marco Nelissen | 961ec21 | 2014-08-25 15:58:39 -0700 | [diff] [blame] | 4434 | (hwOutputDesc->getAudioPort()->mModule->mHalVersion < |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4435 | AUDIO_DEVICE_API_VERSION_3_0))) { |
| 4436 | availableOutputDeviceTypes = availablePrimaryOutputDevices(); |
| 4437 | } |
| 4438 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4439 | // for phone strategy, we first consider the forced use and then the available devices by order |
| 4440 | // of priority |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4441 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 4442 | case AUDIO_POLICY_FORCE_BT_SCO: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4443 | if (!isInCall() || strategy != STRATEGY_DTMF) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4444 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4445 | if (device) break; |
| 4446 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4447 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4448 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4449 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_SCO; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4450 | if (device) break; |
| 4451 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 4452 | // FALL THROUGH |
| 4453 | |
| 4454 | default: // FORCE_NONE |
| 4455 | // 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] | 4456 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4457 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4458 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4459 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4460 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4461 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4462 | if (device) break; |
| 4463 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4464 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4465 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4466 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4467 | if (device) break; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 4468 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
| 4469 | if (device) break; |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4470 | if (mPhoneState != AUDIO_MODE_IN_CALL) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4471 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4472 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4473 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4474 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4475 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4476 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4477 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4478 | if (device) break; |
| 4479 | } |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4480 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_EARPIECE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4481 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4482 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4483 | if (device == AUDIO_DEVICE_NONE) { |
| 4484 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE"); |
| 4485 | } |
| 4486 | break; |
| 4487 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4488 | case AUDIO_POLICY_FORCE_SPEAKER: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4489 | // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to |
| 4490 | // A2DP speaker when forcing to speaker output |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4491 | if (!isInCall() && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4492 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4493 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4494 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4495 | if (device) break; |
| 4496 | } |
Eric Laurent | cd71a69 | 2014-12-16 12:01:12 -0800 | [diff] [blame] | 4497 | if (!isInCall()) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4498 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4499 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4500 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4501 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4502 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4503 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4504 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4505 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4506 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4507 | if (device) break; |
| 4508 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4509 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4510 | if (device) break; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4511 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4512 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4513 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4514 | if (device == AUDIO_DEVICE_NONE) { |
| 4515 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER"); |
| 4516 | } |
| 4517 | break; |
| 4518 | } |
| 4519 | break; |
| 4520 | |
| 4521 | case STRATEGY_SONIFICATION: |
| 4522 | |
| 4523 | // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by |
| 4524 | // handleIncallSonification(). |
| 4525 | if (isInCall()) { |
| 4526 | device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/); |
| 4527 | break; |
| 4528 | } |
| 4529 | // FALL THROUGH |
| 4530 | |
| 4531 | case STRATEGY_ENFORCED_AUDIBLE: |
| 4532 | // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION |
| 4533 | // except: |
| 4534 | // - when in call where it doesn't default to STRATEGY_PHONE behavior |
| 4535 | // - in countries where not enforced in which case it follows STRATEGY_MEDIA |
| 4536 | |
| 4537 | if ((strategy == STRATEGY_SONIFICATION) || |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4538 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4539 | device = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4540 | if (device == AUDIO_DEVICE_NONE) { |
| 4541 | ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION"); |
| 4542 | } |
| 4543 | } |
| 4544 | // The second device used for sonification is the same as the device used by media strategy |
| 4545 | // FALL THROUGH |
| 4546 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4547 | // FIXME: STRATEGY_ACCESSIBILITY and STRATEGY_REROUTING follow STRATEGY_MEDIA for now |
| 4548 | case STRATEGY_ACCESSIBILITY: |
Eric Laurent | 066ceec | 2014-11-25 12:35:01 -0800 | [diff] [blame] | 4549 | if (strategy == STRATEGY_ACCESSIBILITY) { |
| 4550 | // do not route accessibility prompts to a digital output currently configured with a |
| 4551 | // compressed format as they would likely not be mixed and dropped. |
| 4552 | for (size_t i = 0; i < mOutputs.size(); i++) { |
| 4553 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); |
| 4554 | audio_devices_t devices = desc->device() & |
| 4555 | (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC); |
| 4556 | if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) && |
| 4557 | devices != AUDIO_DEVICE_NONE) { |
| 4558 | availableOutputDeviceTypes = availableOutputDeviceTypes & ~devices; |
| 4559 | } |
| 4560 | } |
| 4561 | } |
| 4562 | // FALL THROUGH |
| 4563 | |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4564 | case STRATEGY_REROUTING: |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4565 | case STRATEGY_MEDIA: { |
| 4566 | uint32_t device2 = AUDIO_DEVICE_NONE; |
| 4567 | if (strategy != STRATEGY_SONIFICATION) { |
| 4568 | // no sonification on remote submix (e.g. WFD) |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4569 | if (mAvailableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0")) != 0) { |
| 4570 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_REMOTE_SUBMIX; |
| 4571 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4572 | } |
| 4573 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4574 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) && |
Eric Laurent | 29e6cec | 2014-11-13 18:17:55 -0800 | [diff] [blame] | 4575 | (getA2dpOutput() != 0)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4576 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4577 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4578 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4579 | } |
| 4580 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4581 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4582 | } |
| 4583 | } |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 4584 | if ((device2 == AUDIO_DEVICE_NONE) && |
| 4585 | (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) { |
| 4586 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
| 4587 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4588 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4589 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4590 | } |
Jon Eklund | ac29afa | 2014-07-28 16:06:06 -0500 | [diff] [blame] | 4591 | if ((device2 == AUDIO_DEVICE_NONE)) { |
| 4592 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_LINE; |
| 4593 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4594 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4595 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4596 | } |
| 4597 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4598 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4599 | } |
| 4600 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4601 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_USB_DEVICE; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4602 | } |
| 4603 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4604 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4605 | } |
| 4606 | if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) { |
| 4607 | // no sonification on aux digital (e.g. HDMI) |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4608 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4609 | } |
| 4610 | if ((device2 == AUDIO_DEVICE_NONE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4611 | (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4612 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4613 | } |
| 4614 | if (device2 == AUDIO_DEVICE_NONE) { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4615 | device2 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPEAKER; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4616 | } |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4617 | int device3 = AUDIO_DEVICE_NONE; |
| 4618 | if (strategy == STRATEGY_MEDIA) { |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4619 | // ARC, SPDIF and AUX_LINE can co-exist with others. |
Jungshik Jang | 0c94309 | 2014-07-08 22:11:24 +0900 | [diff] [blame] | 4620 | device3 = availableOutputDeviceTypes & AUDIO_DEVICE_OUT_HDMI_ARC; |
| 4621 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_SPDIF); |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4622 | device3 |= (availableOutputDeviceTypes & AUDIO_DEVICE_OUT_AUX_LINE); |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4623 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4624 | |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4625 | device2 |= device3; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4626 | // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or |
| 4627 | // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise |
| 4628 | device |= device2; |
Jungshik Jang | 839e4f3 | 2014-06-26 17:23:40 +0900 | [diff] [blame] | 4629 | |
Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 4630 | // If hdmi system audio mode is on, remove speaker out of output list. |
| 4631 | if ((strategy == STRATEGY_MEDIA) && |
| 4632 | (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] == |
| 4633 | AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) { |
| 4634 | device &= ~AUDIO_DEVICE_OUT_SPEAKER; |
| 4635 | } |
| 4636 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4637 | if (device) break; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4638 | device = mDefaultOutputDevice->mDeviceType; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4639 | if (device == AUDIO_DEVICE_NONE) { |
| 4640 | ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA"); |
| 4641 | } |
| 4642 | } break; |
| 4643 | |
| 4644 | default: |
| 4645 | ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy); |
| 4646 | break; |
| 4647 | } |
| 4648 | |
| 4649 | ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device); |
| 4650 | return device; |
| 4651 | } |
| 4652 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4653 | void AudioPolicyManager::updateDevicesAndOutputs() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4654 | { |
| 4655 | for (int i = 0; i < NUM_STRATEGIES; i++) { |
| 4656 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
| 4657 | } |
| 4658 | mPreviousOutputs = mOutputs; |
| 4659 | } |
| 4660 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4661 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(sp<AudioOutputDescriptor> outputDesc, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4662 | audio_devices_t prevDevice, |
| 4663 | uint32_t delayMs) |
| 4664 | { |
| 4665 | // mute/unmute strategies using an incompatible device combination |
| 4666 | // if muting, wait for the audio in pcm buffer to be drained before proceeding |
| 4667 | // if unmuting, unmute only after the specified delay |
| 4668 | if (outputDesc->isDuplicated()) { |
| 4669 | return 0; |
| 4670 | } |
| 4671 | |
| 4672 | uint32_t muteWaitMs = 0; |
| 4673 | audio_devices_t device = outputDesc->device(); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4674 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4675 | |
| 4676 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4677 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); |
Eric Laurent | 31551f8 | 2014-10-10 18:21:56 -0700 | [diff] [blame] | 4678 | curDevice = curDevice & outputDesc->mProfile->mSupportedDevices.types(); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4679 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); |
| 4680 | bool doMute = false; |
| 4681 | |
| 4682 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { |
| 4683 | doMute = true; |
| 4684 | outputDesc->mStrategyMutedByDevice[i] = true; |
| 4685 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ |
| 4686 | doMute = true; |
| 4687 | outputDesc->mStrategyMutedByDevice[i] = false; |
| 4688 | } |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4689 | if (doMute) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4690 | for (size_t j = 0; j < mOutputs.size(); j++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4691 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4692 | // skip output if it does not share any device with current output |
| 4693 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) |
| 4694 | == AUDIO_DEVICE_NONE) { |
| 4695 | continue; |
| 4696 | } |
| 4697 | audio_io_handle_t curOutput = mOutputs.keyAt(j); |
| 4698 | ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d", |
| 4699 | mute ? "muting" : "unmuting", i, curDevice, curOutput); |
| 4700 | setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs); |
| 4701 | if (desc->isStrategyActive((routing_strategy)i)) { |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4702 | if (mute) { |
| 4703 | // FIXME: should not need to double latency if volume could be applied |
| 4704 | // immediately by the audioflinger mixer. We must account for the delay |
| 4705 | // between now and the next time the audioflinger thread for this output |
| 4706 | // will process a buffer (which corresponds to one buffer size, |
| 4707 | // usually 1/2 or 1/4 of the latency). |
| 4708 | if (muteWaitMs < desc->latency() * 2) { |
| 4709 | muteWaitMs = desc->latency() * 2; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4710 | } |
| 4711 | } |
| 4712 | } |
| 4713 | } |
| 4714 | } |
| 4715 | } |
| 4716 | |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4717 | // temporary mute output if device selection changes to avoid volume bursts due to |
| 4718 | // different per device volumes |
| 4719 | if (outputDesc->isActive() && (device != prevDevice)) { |
| 4720 | if (muteWaitMs < outputDesc->latency() * 2) { |
| 4721 | muteWaitMs = outputDesc->latency() * 2; |
| 4722 | } |
| 4723 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { |
| 4724 | if (outputDesc->isStrategyActive((routing_strategy)i)) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4725 | setStrategyMute((routing_strategy)i, true, outputDesc->mIoHandle); |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4726 | // do tempMute unmute after twice the mute wait time |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4727 | setStrategyMute((routing_strategy)i, false, outputDesc->mIoHandle, |
Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4728 | muteWaitMs *2, device); |
| 4729 | } |
| 4730 | } |
| 4731 | } |
| 4732 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4733 | // wait for the PCM output buffers to empty before proceeding with the rest of the command |
| 4734 | if (muteWaitMs > delayMs) { |
| 4735 | muteWaitMs -= delayMs; |
| 4736 | usleep(muteWaitMs * 1000); |
| 4737 | return muteWaitMs; |
| 4738 | } |
| 4739 | return 0; |
| 4740 | } |
| 4741 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4742 | uint32_t AudioPolicyManager::setOutputDevice(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4743 | audio_devices_t device, |
| 4744 | bool force, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4745 | int delayMs, |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4746 | audio_patch_handle_t *patchHandle, |
| 4747 | const char* address) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4748 | { |
| 4749 | ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs); |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4750 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4751 | AudioParameter param; |
| 4752 | uint32_t muteWaitMs; |
| 4753 | |
| 4754 | if (outputDesc->isDuplicated()) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4755 | muteWaitMs = setOutputDevice(outputDesc->mOutput1->mIoHandle, device, force, delayMs); |
| 4756 | muteWaitMs += setOutputDevice(outputDesc->mOutput2->mIoHandle, device, force, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4757 | return muteWaitMs; |
| 4758 | } |
| 4759 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current |
| 4760 | // output profile |
| 4761 | if ((device != AUDIO_DEVICE_NONE) && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4762 | ((device & outputDesc->mProfile->mSupportedDevices.types()) == 0)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4763 | return 0; |
| 4764 | } |
| 4765 | |
| 4766 | // filter devices according to output selected |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4767 | device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4768 | |
| 4769 | audio_devices_t prevDevice = outputDesc->mDevice; |
| 4770 | |
| 4771 | ALOGV("setOutputDevice() prevDevice %04x", prevDevice); |
| 4772 | |
| 4773 | if (device != AUDIO_DEVICE_NONE) { |
| 4774 | outputDesc->mDevice = device; |
| 4775 | } |
| 4776 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); |
| 4777 | |
| 4778 | // Do not change the routing if: |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4779 | // the requested device is AUDIO_DEVICE_NONE |
| 4780 | // OR the requested device is the same as current device |
| 4781 | // AND force is not specified |
| 4782 | // AND the output is connected by a valid audio patch. |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4783 | // Doing this check here allows the caller to call setOutputDevice() without conditions |
Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4784 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force && |
| 4785 | outputDesc->mPatchHandle != 0) { |
| 4786 | ALOGV("setOutputDevice() setting same device %04x or null device for output %d", |
| 4787 | device, output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4788 | return muteWaitMs; |
| 4789 | } |
| 4790 | |
| 4791 | ALOGV("setOutputDevice() changing device"); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4792 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4793 | // do the routing |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4794 | if (device == AUDIO_DEVICE_NONE) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4795 | resetOutputDevice(output, delayMs, NULL); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4796 | } else { |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4797 | DeviceVector deviceList = (address == NULL) ? |
| 4798 | mAvailableOutputDevices.getDevicesFromType(device) |
| 4799 | : mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4800 | if (!deviceList.isEmpty()) { |
| 4801 | struct audio_patch patch; |
| 4802 | outputDesc->toAudioPortConfig(&patch.sources[0]); |
| 4803 | patch.num_sources = 1; |
| 4804 | patch.num_sinks = 0; |
| 4805 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { |
| 4806 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4807 | patch.num_sinks++; |
| 4808 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4809 | ssize_t index; |
| 4810 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4811 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4812 | } else { |
| 4813 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4814 | } |
| 4815 | sp< AudioPatch> patchDesc; |
| 4816 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4817 | if (index >= 0) { |
| 4818 | patchDesc = mAudioPatches.valueAt(index); |
| 4819 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4820 | } |
| 4821 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4822 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4823 | &afPatchHandle, |
| 4824 | delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4825 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" |
| 4826 | "num_sources %d num_sinks %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4827 | status, afPatchHandle, patch.num_sources, patch.num_sinks); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4828 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4829 | if (index < 0) { |
| 4830 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 4831 | &patch, mUidCached); |
| 4832 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4833 | } else { |
| 4834 | patchDesc->mPatch = patch; |
| 4835 | } |
| 4836 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4837 | patchDesc->mUid = mUidCached; |
| 4838 | if (patchHandle) { |
| 4839 | *patchHandle = patchDesc->mHandle; |
| 4840 | } |
| 4841 | outputDesc->mPatchHandle = patchDesc->mHandle; |
| 4842 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4843 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4844 | } |
| 4845 | } |
bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 4846 | |
| 4847 | // inform all input as well |
| 4848 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 4849 | const sp<AudioInputDescriptor> inputDescriptor = mInputs.valueAt(i); |
| 4850 | if (!isVirtualInputDevice(inputDescriptor->mDevice)) { |
| 4851 | AudioParameter inputCmd = AudioParameter(); |
| 4852 | ALOGV("%s: inform input %d of device:%d", __func__, |
| 4853 | inputDescriptor->mIoHandle, device); |
| 4854 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); |
| 4855 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, |
| 4856 | inputCmd.toString(), |
| 4857 | delayMs); |
| 4858 | } |
| 4859 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4860 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4861 | |
| 4862 | // update stream volumes according to new device |
| 4863 | applyStreamVolumes(output, device, delayMs); |
| 4864 | |
| 4865 | return muteWaitMs; |
| 4866 | } |
| 4867 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4868 | status_t AudioPolicyManager::resetOutputDevice(audio_io_handle_t output, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4869 | int delayMs, |
| 4870 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4871 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4872 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4873 | ssize_t index; |
| 4874 | if (patchHandle) { |
| 4875 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4876 | } else { |
| 4877 | index = mAudioPatches.indexOfKey(outputDesc->mPatchHandle); |
| 4878 | } |
| 4879 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4880 | return INVALID_OPERATION; |
| 4881 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4882 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4883 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4884 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); |
| 4885 | outputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4886 | removeAudioPatch(patchDesc->mHandle); |
| 4887 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4888 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4889 | return status; |
| 4890 | } |
| 4891 | |
| 4892 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, |
| 4893 | audio_devices_t device, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4894 | bool force, |
| 4895 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4896 | { |
| 4897 | status_t status = NO_ERROR; |
| 4898 | |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4899 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4900 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { |
| 4901 | inputDesc->mDevice = device; |
| 4902 | |
| 4903 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); |
| 4904 | if (!deviceList.isEmpty()) { |
| 4905 | struct audio_patch patch; |
| 4906 | inputDesc->toAudioPortConfig(&patch.sinks[0]); |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4907 | // AUDIO_SOURCE_HOTWORD is for internal use only: |
| 4908 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 4909 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && |
| 4910 | !inputDesc->mIsSoundTrigger) { |
Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 4911 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; |
| 4912 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4913 | patch.num_sinks = 1; |
| 4914 | //only one input device for now |
| 4915 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4916 | patch.num_sources = 1; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4917 | ssize_t index; |
| 4918 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { |
| 4919 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4920 | } else { |
| 4921 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4922 | } |
| 4923 | sp< AudioPatch> patchDesc; |
| 4924 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; |
| 4925 | if (index >= 0) { |
| 4926 | patchDesc = mAudioPatches.valueAt(index); |
| 4927 | afPatchHandle = patchDesc->mAfPatchHandle; |
| 4928 | } |
| 4929 | |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4930 | status_t status = mpClientInterface->createAudioPatch(&patch, |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4931 | &afPatchHandle, |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4932 | 0); |
| 4933 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4934 | status, afPatchHandle); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4935 | if (status == NO_ERROR) { |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4936 | if (index < 0) { |
| 4937 | patchDesc = new AudioPatch((audio_patch_handle_t)nextUniqueId(), |
| 4938 | &patch, mUidCached); |
| 4939 | addAudioPatch(patchDesc->mHandle, patchDesc); |
| 4940 | } else { |
| 4941 | patchDesc->mPatch = patch; |
| 4942 | } |
| 4943 | patchDesc->mAfPatchHandle = afPatchHandle; |
| 4944 | patchDesc->mUid = mUidCached; |
| 4945 | if (patchHandle) { |
| 4946 | *patchHandle = patchDesc->mHandle; |
| 4947 | } |
| 4948 | inputDesc->mPatchHandle = patchDesc->mHandle; |
| 4949 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4950 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4951 | } |
| 4952 | } |
| 4953 | } |
| 4954 | return status; |
| 4955 | } |
| 4956 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4957 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, |
| 4958 | audio_patch_handle_t *patchHandle) |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4959 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4960 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4961 | ssize_t index; |
| 4962 | if (patchHandle) { |
| 4963 | index = mAudioPatches.indexOfKey(*patchHandle); |
| 4964 | } else { |
| 4965 | index = mAudioPatches.indexOfKey(inputDesc->mPatchHandle); |
| 4966 | } |
| 4967 | if (index < 0) { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4968 | return INVALID_OPERATION; |
| 4969 | } |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4970 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); |
| 4971 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4972 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); |
| 4973 | inputDesc->mPatchHandle = 0; |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4974 | removeAudioPatch(patchDesc->mHandle); |
| 4975 | nextAudioPortGeneration(); |
Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 4976 | mpClientInterface->onAudioPatchListUpdate(); |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4977 | return status; |
| 4978 | } |
| 4979 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 4980 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4981 | String8 address, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4982 | uint32_t& samplingRate, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4983 | audio_format_t format, |
Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 4984 | audio_channel_mask_t channelMask, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 4985 | audio_input_flags_t flags) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4986 | { |
| 4987 | // Choose an input profile based on the requested capture parameters: select the first available |
| 4988 | // profile supporting all requested parameters. |
| 4989 | |
| 4990 | for (size_t i = 0; i < mHwModules.size(); i++) |
| 4991 | { |
| 4992 | if (mHwModules[i]->mHandle == 0) { |
| 4993 | continue; |
| 4994 | } |
| 4995 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) |
| 4996 | { |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4997 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4998 | // profile->log(); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4999 | if (profile->isCompatibleProfile(device, address, samplingRate, |
Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5000 | &samplingRate /*updatedSamplingRate*/, |
| 5001 | format, channelMask, (audio_output_flags_t) flags)) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5002 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5003 | return profile; |
| 5004 | } |
| 5005 | } |
| 5006 | } |
| 5007 | return NULL; |
| 5008 | } |
| 5009 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5010 | |
| 5011 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 5012 | AudioMix **policyMix) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5013 | { |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5014 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & |
| 5015 | ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5016 | |
| 5017 | for (size_t i = 0; i < mPolicyMixes.size(); i++) { |
| 5018 | if (mPolicyMixes[i]->mMix.mMixType != MIX_TYPE_RECORDERS) { |
| 5019 | continue; |
| 5020 | } |
| 5021 | for (size_t j = 0; j < mPolicyMixes[i]->mMix.mCriteria.size(); j++) { |
| 5022 | if ((RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule && |
| 5023 | mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource == inputSource) || |
| 5024 | (RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET == mPolicyMixes[i]->mMix.mCriteria[j].mRule && |
| 5025 | mPolicyMixes[i]->mMix.mCriteria[j].mAttr.mSource != inputSource)) { |
| 5026 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 5027 | if (policyMix != NULL) { |
| 5028 | *policyMix = &mPolicyMixes[i]->mMix; |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5029 | } |
| 5030 | return AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 5031 | } |
| 5032 | break; |
| 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | |
Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5037 | return getDeviceForInputSource(inputSource); |
| 5038 | } |
| 5039 | |
| 5040 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) |
| 5041 | { |
| 5042 | uint32_t device = AUDIO_DEVICE_NONE; |
| 5043 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & |
| 5044 | ~AUDIO_DEVICE_BIT_IN; |
| 5045 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5046 | switch (inputSource) { |
| 5047 | case AUDIO_SOURCE_VOICE_UPLINK: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5048 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5049 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 5050 | break; |
| 5051 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 5052 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5053 | |
| 5054 | case AUDIO_SOURCE_DEFAULT: |
| 5055 | case AUDIO_SOURCE_MIC: |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 5056 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) { |
| 5057 | device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP; |
Eric Laurent | dc136ff | 2014-12-16 12:24:18 -0800 | [diff] [blame] | 5058 | } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) && |
| 5059 | (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { |
| 5060 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 5061 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 5062 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 5063 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 5064 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 5065 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 5066 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 5067 | } |
Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 5068 | break; |
| 5069 | |
| 5070 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 5071 | // Allow only use of devices on primary input if in call and HAL does not support routing |
| 5072 | // to voice call path. |
| 5073 | if ((mPhoneState == AUDIO_MODE_IN_CALL) && |
| 5074 | (mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) { |
| 5075 | availableDeviceTypes = availablePrimaryInputDevices() & ~AUDIO_DEVICE_BIT_IN; |
| 5076 | } |
| 5077 | |
| 5078 | switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) { |
| 5079 | case AUDIO_POLICY_FORCE_BT_SCO: |
| 5080 | // if SCO device is requested but no SCO device is available, fall back to default case |
| 5081 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 5082 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 5083 | break; |
| 5084 | } |
| 5085 | // FALL THROUGH |
| 5086 | |
| 5087 | default: // FORCE_NONE |
| 5088 | if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 5089 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
| 5090 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 5091 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
| 5092 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 5093 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 5094 | } |
| 5095 | break; |
| 5096 | |
| 5097 | case AUDIO_POLICY_FORCE_SPEAKER: |
| 5098 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
| 5099 | device = AUDIO_DEVICE_IN_BACK_MIC; |
| 5100 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 5101 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 5102 | } |
| 5103 | break; |
| 5104 | } |
| 5105 | break; |
Mike Lockwood | 41b0e24 | 2014-05-13 15:23:35 -0700 | [diff] [blame] | 5106 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5107 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 5108 | case AUDIO_SOURCE_HOTWORD: |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5109 | if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO && |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5110 | availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5111 | device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5112 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5113 | device = AUDIO_DEVICE_IN_WIRED_HEADSET; |
Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5114 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) { |
| 5115 | device = AUDIO_DEVICE_IN_USB_DEVICE; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5116 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5117 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 5118 | } |
| 5119 | break; |
| 5120 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5121 | if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5122 | device = AUDIO_DEVICE_IN_BACK_MIC; |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5123 | } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5124 | device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 5125 | } |
| 5126 | break; |
| 5127 | case AUDIO_SOURCE_VOICE_DOWNLINK: |
| 5128 | case AUDIO_SOURCE_VOICE_CALL: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5129 | if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5130 | device = AUDIO_DEVICE_IN_VOICE_CALL; |
| 5131 | } |
| 5132 | break; |
| 5133 | case AUDIO_SOURCE_REMOTE_SUBMIX: |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5134 | if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5135 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; |
| 5136 | } |
| 5137 | break; |
Hochi Huang | 327cb70 | 2014-09-21 09:47:31 +0800 | [diff] [blame] | 5138 | case AUDIO_SOURCE_FM_TUNER: |
| 5139 | if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) { |
| 5140 | device = AUDIO_DEVICE_IN_FM_TUNER; |
| 5141 | } |
| 5142 | break; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5143 | default: |
| 5144 | ALOGW("getDeviceForInputSource() invalid input source %d", inputSource); |
| 5145 | break; |
| 5146 | } |
| 5147 | ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device); |
| 5148 | return device; |
| 5149 | } |
| 5150 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5151 | bool AudioPolicyManager::isVirtualInputDevice(audio_devices_t device) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5152 | { |
| 5153 | if ((device & AUDIO_DEVICE_BIT_IN) != 0) { |
| 5154 | device &= ~AUDIO_DEVICE_BIT_IN; |
| 5155 | if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0)) |
| 5156 | return true; |
| 5157 | } |
| 5158 | return false; |
| 5159 | } |
| 5160 | |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5161 | bool AudioPolicyManager::deviceDistinguishesOnAddress(audio_devices_t device) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5162 | return ((device & APM_AUDIO_DEVICE_MATCH_ADDRESS_ALL & ~AUDIO_DEVICE_BIT_IN) != 0); |
Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 5163 | } |
| 5164 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5165 | audio_io_handle_t AudioPolicyManager::getActiveInput(bool ignoreVirtualInputs) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5166 | { |
| 5167 | for (size_t i = 0; i < mInputs.size(); i++) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5168 | const sp<AudioInputDescriptor> input_descriptor = mInputs.valueAt(i); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5169 | if ((input_descriptor->mRefCount > 0) |
| 5170 | && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) { |
| 5171 | return mInputs.keyAt(i); |
| 5172 | } |
| 5173 | } |
| 5174 | return 0; |
| 5175 | } |
| 5176 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5177 | uint32_t AudioPolicyManager::activeInputsCount() const |
| 5178 | { |
| 5179 | uint32_t count = 0; |
| 5180 | for (size_t i = 0; i < mInputs.size(); i++) { |
| 5181 | const sp<AudioInputDescriptor> desc = mInputs.valueAt(i); |
| 5182 | if (desc->mRefCount > 0) { |
Eric Laurent | a34c9ce | 2014-12-19 11:16:32 -0800 | [diff] [blame] | 5183 | count++; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5184 | } |
| 5185 | } |
| 5186 | return count; |
| 5187 | } |
| 5188 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5189 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5190 | void AudioPolicyManager::initializeVolumeCurves() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5191 | { |
| 5192 | for (int i = 0; i < AUDIO_STREAM_CNT; i++) { |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5193 | for (int j = 0; j < ApmGains::DEVICE_CATEGORY_CNT; j++) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5194 | mStreams[i].mVolumeCurve[j] = |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5195 | ApmGains::sVolumeProfiles[i][j]; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | // Check availability of DRC on speaker path: if available, override some of the speaker curves |
| 5200 | if (mSpeakerDrcEnabled) { |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5201 | mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 5202 | ApmGains::sDefaultSystemVolumeCurveDrc; |
| 5203 | mStreams[AUDIO_STREAM_RING].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 5204 | ApmGains::sSpeakerSonificationVolumeCurveDrc; |
| 5205 | mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 5206 | ApmGains::sSpeakerSonificationVolumeCurveDrc; |
| 5207 | mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 5208 | ApmGains::sSpeakerSonificationVolumeCurveDrc; |
| 5209 | mStreams[AUDIO_STREAM_MUSIC].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 5210 | ApmGains::sSpeakerMediaVolumeCurveDrc; |
| 5211 | mStreams[AUDIO_STREAM_ACCESSIBILITY].mVolumeCurve[ApmGains::DEVICE_CATEGORY_SPEAKER] = |
| 5212 | ApmGains::sSpeakerMediaVolumeCurveDrc; |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5213 | } |
| 5214 | } |
| 5215 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5216 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5217 | int index, |
| 5218 | audio_io_handle_t output, |
| 5219 | audio_devices_t device) |
| 5220 | { |
| 5221 | float volume = 1.0; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5222 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5223 | StreamDescriptor &streamDesc = mStreams[stream]; |
| 5224 | |
| 5225 | if (device == AUDIO_DEVICE_NONE) { |
| 5226 | device = outputDesc->device(); |
| 5227 | } |
| 5228 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5229 | volume = ApmGains::volIndexToAmpl(device, streamDesc, index); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5230 | |
| 5231 | // if a headset is connected, apply the following rules to ring tones and notifications |
| 5232 | // to avoid sound level bursts in user's ears: |
| 5233 | // - always attenuate ring tones and notifications volume by 6dB |
| 5234 | // - if music is playing, always limit the volume to current music volume, |
| 5235 | // with a minimum threshold at -36dB so that notification is always perceived. |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5236 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5237 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | |
| 5238 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | |
| 5239 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 5240 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) && |
| 5241 | ((stream_strategy == STRATEGY_SONIFICATION) |
| 5242 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5243 | || (stream == AUDIO_STREAM_SYSTEM) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5244 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5245 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) && |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | streamDesc.mCanBeMuted) { |
| 5247 | volume *= SONIFICATION_HEADSET_VOLUME_FACTOR; |
| 5248 | // when the phone is ringing we must consider that music could have been paused just before |
| 5249 | // by the music application and behave as if music was active if the last music track was |
| 5250 | // just stopped |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5251 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5252 | mLimitRingtoneVolume) { |
| 5253 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5254 | float musicVol = computeVolume(AUDIO_STREAM_MUSIC, |
| 5255 | mStreams[AUDIO_STREAM_MUSIC].getVolumeIndex(musicDevice), |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5256 | output, |
| 5257 | musicDevice); |
| 5258 | float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? |
| 5259 | musicVol : SONIFICATION_HEADSET_VOLUME_MIN; |
| 5260 | if (volume > minVol) { |
| 5261 | volume = minVol; |
| 5262 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol); |
| 5263 | } |
| 5264 | } |
| 5265 | } |
| 5266 | |
| 5267 | return volume; |
| 5268 | } |
| 5269 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5270 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5271 | int index, |
| 5272 | audio_io_handle_t output, |
| 5273 | audio_devices_t device, |
| 5274 | int delayMs, |
| 5275 | bool force) |
| 5276 | { |
| 5277 | |
| 5278 | // do not change actual stream volume if the stream is muted |
| 5279 | if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) { |
| 5280 | ALOGVV("checkAndSetVolume() stream %d muted count %d", |
| 5281 | stream, mOutputs.valueFor(output)->mMuteCount[stream]); |
| 5282 | return NO_ERROR; |
| 5283 | } |
| 5284 | |
| 5285 | // 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] | 5286 | if ((stream == AUDIO_STREAM_VOICE_CALL && |
| 5287 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO) || |
| 5288 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && |
| 5289 | mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] != AUDIO_POLICY_FORCE_BT_SCO)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5290 | 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] | 5291 | stream, mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5292 | return INVALID_OPERATION; |
| 5293 | } |
| 5294 | |
| 5295 | float volume = computeVolume(stream, index, output, device); |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5296 | // unit gain if rerouting to external policy |
| 5297 | if (device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX) { |
| 5298 | ssize_t index = mOutputs.indexOfKey(output); |
| 5299 | if (index >= 0) { |
| 5300 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); |
Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 5301 | if (outputDesc->mPolicyMix != NULL) { |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5302 | ALOGV("max gain when rerouting for output=%d", output); |
| 5303 | volume = 1.0f; |
| 5304 | } |
| 5305 | } |
| 5306 | |
| 5307 | } |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5308 | // We actually change the volume if: |
| 5309 | // - the float value returned by computeVolume() changed |
| 5310 | // - the force flag is set |
| 5311 | if (volume != mOutputs.valueFor(output)->mCurVolume[stream] || |
| 5312 | force) { |
| 5313 | mOutputs.valueFor(output)->mCurVolume[stream] = volume; |
| 5314 | ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs); |
| 5315 | // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is |
| 5316 | // enabled |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5317 | if (stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
| 5318 | mpClientInterface->setStreamVolume(AUDIO_STREAM_VOICE_CALL, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5319 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5320 | mpClientInterface->setStreamVolume(stream, volume, output, delayMs); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5321 | } |
| 5322 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5323 | if (stream == AUDIO_STREAM_VOICE_CALL || |
| 5324 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5325 | float voiceVolume; |
| 5326 | // 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] | 5327 | if (stream == AUDIO_STREAM_VOICE_CALL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5328 | voiceVolume = (float)index/(float)mStreams[stream].mIndexMax; |
| 5329 | } else { |
| 5330 | voiceVolume = 1.0; |
| 5331 | } |
| 5332 | |
| 5333 | if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) { |
| 5334 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); |
| 5335 | mLastVoiceVolume = voiceVolume; |
| 5336 | } |
| 5337 | } |
| 5338 | |
| 5339 | return NO_ERROR; |
| 5340 | } |
| 5341 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5342 | void AudioPolicyManager::applyStreamVolumes(audio_io_handle_t output, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5343 | audio_devices_t device, |
| 5344 | int delayMs, |
| 5345 | bool force) |
| 5346 | { |
| 5347 | ALOGVV("applyStreamVolumes() for output %d and device %x", output, device); |
| 5348 | |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5349 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5350 | if (stream == AUDIO_STREAM_PATCH) { |
| 5351 | continue; |
| 5352 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5353 | checkAndSetVolume((audio_stream_type_t)stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5354 | mStreams[stream].getVolumeIndex(device), |
| 5355 | output, |
| 5356 | device, |
| 5357 | delayMs, |
| 5358 | force); |
| 5359 | } |
| 5360 | } |
| 5361 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5362 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5363 | bool on, |
| 5364 | audio_io_handle_t output, |
| 5365 | int delayMs, |
| 5366 | audio_devices_t device) |
| 5367 | { |
| 5368 | ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output); |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5369 | for (int stream = 0; stream < AUDIO_STREAM_CNT; stream++) { |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5370 | if (stream == AUDIO_STREAM_PATCH) { |
| 5371 | continue; |
| 5372 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5373 | if (getStrategy((audio_stream_type_t)stream) == strategy) { |
| 5374 | setStreamMute((audio_stream_type_t)stream, on, output, delayMs, device); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5375 | } |
| 5376 | } |
| 5377 | } |
| 5378 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5379 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5380 | bool on, |
| 5381 | audio_io_handle_t output, |
| 5382 | int delayMs, |
| 5383 | audio_devices_t device) |
| 5384 | { |
| 5385 | StreamDescriptor &streamDesc = mStreams[stream]; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5386 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5387 | if (device == AUDIO_DEVICE_NONE) { |
| 5388 | device = outputDesc->device(); |
| 5389 | } |
| 5390 | |
| 5391 | ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x", |
| 5392 | stream, on, output, outputDesc->mMuteCount[stream], device); |
| 5393 | |
| 5394 | if (on) { |
| 5395 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5396 | if (streamDesc.mCanBeMuted && |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5397 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || |
| 5398 | (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_NONE))) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5399 | checkAndSetVolume(stream, 0, output, device, delayMs); |
| 5400 | } |
| 5401 | } |
| 5402 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored |
| 5403 | outputDesc->mMuteCount[stream]++; |
| 5404 | } else { |
| 5405 | if (outputDesc->mMuteCount[stream] == 0) { |
| 5406 | ALOGV("setStreamMute() unmuting non muted stream!"); |
| 5407 | return; |
| 5408 | } |
| 5409 | if (--outputDesc->mMuteCount[stream] == 0) { |
| 5410 | checkAndSetVolume(stream, |
| 5411 | streamDesc.getVolumeIndex(device), |
| 5412 | output, |
| 5413 | device, |
| 5414 | delayMs); |
| 5415 | } |
| 5416 | } |
| 5417 | } |
| 5418 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5419 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5420 | bool starting, bool stateChange) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5421 | { |
| 5422 | // if the stream pertains to sonification strategy and we are in call we must |
| 5423 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone |
| 5424 | // in the device used for phone strategy and play the tone if the selected device does not |
| 5425 | // interfere with the device used for phone strategy |
| 5426 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as |
| 5427 | // many times as there are active tracks on the output |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5428 | const routing_strategy stream_strategy = getStrategy(stream); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5429 | if ((stream_strategy == STRATEGY_SONIFICATION) || |
| 5430 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5431 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(mPrimaryOutput); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5432 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", |
| 5433 | stream, starting, outputDesc->mDevice, stateChange); |
| 5434 | if (outputDesc->mRefCount[stream]) { |
| 5435 | int muteCount = 1; |
| 5436 | if (stateChange) { |
| 5437 | muteCount = outputDesc->mRefCount[stream]; |
| 5438 | } |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5439 | if (audio_is_low_visibility(stream)) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5440 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); |
| 5441 | for (int i = 0; i < muteCount; i++) { |
| 5442 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5443 | } |
| 5444 | } else { |
| 5445 | ALOGV("handleIncallSonification() high visibility"); |
| 5446 | if (outputDesc->device() & |
| 5447 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { |
| 5448 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); |
| 5449 | for (int i = 0; i < muteCount; i++) { |
| 5450 | setStreamMute(stream, starting, mPrimaryOutput); |
| 5451 | } |
| 5452 | } |
| 5453 | if (starting) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5454 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, |
| 5455 | AUDIO_STREAM_VOICE_CALL); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5456 | } else { |
| 5457 | mpClientInterface->stopTone(); |
| 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | } |
| 5462 | } |
| 5463 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5464 | bool AudioPolicyManager::isInCall() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5465 | { |
| 5466 | return isStateInCall(mPhoneState); |
| 5467 | } |
| 5468 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5469 | bool AudioPolicyManager::isStateInCall(int state) { |
Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5470 | return ((state == AUDIO_MODE_IN_CALL) || |
| 5471 | (state == AUDIO_MODE_IN_COMMUNICATION)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5472 | } |
| 5473 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5474 | uint32_t AudioPolicyManager::getMaxEffectsCpuLoad() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5475 | { |
| 5476 | return MAX_EFFECTS_CPU_LOAD; |
| 5477 | } |
| 5478 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5479 | uint32_t AudioPolicyManager::getMaxEffectsMemory() |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5480 | { |
| 5481 | return MAX_EFFECTS_MEMORY; |
| 5482 | } |
| 5483 | |
Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5484 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5485 | // --- EffectDescriptor class implementation |
| 5486 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5487 | status_t AudioPolicyManager::EffectDescriptor::dump(int fd) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5488 | { |
| 5489 | const size_t SIZE = 256; |
| 5490 | char buffer[SIZE]; |
| 5491 | String8 result; |
| 5492 | |
| 5493 | snprintf(buffer, SIZE, " I/O: %d\n", mIo); |
| 5494 | result.append(buffer); |
| 5495 | snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy); |
| 5496 | result.append(buffer); |
| 5497 | snprintf(buffer, SIZE, " Session: %d\n", mSession); |
| 5498 | result.append(buffer); |
| 5499 | snprintf(buffer, SIZE, " Name: %s\n", mDesc.name); |
| 5500 | result.append(buffer); |
| 5501 | snprintf(buffer, SIZE, " %s\n", mEnabled ? "Enabled" : "Disabled"); |
| 5502 | result.append(buffer); |
| 5503 | write(fd, result.string(), result.size()); |
| 5504 | |
| 5505 | return NO_ERROR; |
| 5506 | } |
| 5507 | |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5508 | |
| 5509 | // --- audio_policy.conf file parsing |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5510 | // TODO candidate to be moved to ConfigParsingUtils |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5511 | void AudioPolicyManager::loadHwModule(cnode *root) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5512 | { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5513 | status_t status = NAME_NOT_FOUND; |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5514 | cnode *node; |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5515 | sp<HwModule> module = new HwModule(root->name); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5516 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5517 | node = config_find(root, DEVICES_TAG); |
| 5518 | if (node != NULL) { |
| 5519 | node = node->first_child; |
| 5520 | while (node) { |
| 5521 | ALOGV("loadHwModule() loading device %s", node->name); |
| 5522 | status_t tmpStatus = module->loadDevice(node); |
| 5523 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 5524 | status = tmpStatus; |
| 5525 | } |
| 5526 | node = node->next; |
| 5527 | } |
| 5528 | } |
| 5529 | node = config_find(root, OUTPUTS_TAG); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5530 | if (node != NULL) { |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5531 | node = node->first_child; |
| 5532 | while (node) { |
| 5533 | ALOGV("loadHwModule() loading output %s", node->name); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5534 | status_t tmpStatus = module->loadOutput(node); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5535 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 5536 | status = tmpStatus; |
| 5537 | } |
| 5538 | node = node->next; |
| 5539 | } |
| 5540 | } |
| 5541 | node = config_find(root, INPUTS_TAG); |
| 5542 | if (node != NULL) { |
| 5543 | node = node->first_child; |
| 5544 | while (node) { |
| 5545 | ALOGV("loadHwModule() loading input %s", node->name); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5546 | status_t tmpStatus = module->loadInput(node); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5547 | if (status == NAME_NOT_FOUND || status == NO_ERROR) { |
| 5548 | status = tmpStatus; |
| 5549 | } |
| 5550 | node = node->next; |
| 5551 | } |
| 5552 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5553 | loadGlobalConfig(root, module); |
| 5554 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5555 | if (status == NO_ERROR) { |
| 5556 | mHwModules.add(module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5557 | } |
| 5558 | } |
| 5559 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5560 | // TODO candidate to be moved to ConfigParsingUtils |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5561 | void AudioPolicyManager::loadHwModules(cnode *root) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5562 | { |
| 5563 | cnode *node = config_find(root, AUDIO_HW_MODULE_TAG); |
| 5564 | if (node == NULL) { |
| 5565 | return; |
| 5566 | } |
| 5567 | |
| 5568 | node = node->first_child; |
| 5569 | while (node) { |
| 5570 | ALOGV("loadHwModules() loading module %s", node->name); |
| 5571 | loadHwModule(node); |
| 5572 | node = node->next; |
| 5573 | } |
| 5574 | } |
| 5575 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5576 | // TODO candidate to be moved to ConfigParsingUtils |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5577 | void AudioPolicyManager::loadGlobalConfig(cnode *root, const sp<HwModule>& module) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5578 | { |
| 5579 | cnode *node = config_find(root, GLOBAL_CONFIG_TAG); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5580 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5581 | if (node == NULL) { |
| 5582 | return; |
| 5583 | } |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5584 | DeviceVector declaredDevices; |
| 5585 | if (module != NULL) { |
| 5586 | declaredDevices = module->mDeclaredDevices; |
| 5587 | } |
| 5588 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5589 | node = node->first_child; |
| 5590 | while (node) { |
| 5591 | if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5592 | mAvailableOutputDevices.loadDevicesFromName((char *)node->value, |
| 5593 | declaredDevices); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5594 | ALOGV("loadGlobalConfig() Attached Output Devices %08x", |
| 5595 | mAvailableOutputDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5596 | } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) { |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5597 | audio_devices_t device = (audio_devices_t)ConfigParsingUtils::stringToEnum( |
| 5598 | sDeviceNameToEnumTable, |
| 5599 | ARRAY_SIZE(sDeviceNameToEnumTable), |
| 5600 | (char *)node->value); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5601 | if (device != AUDIO_DEVICE_NONE) { |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 5602 | mDefaultOutputDevice = new DeviceDescriptor(String8("default-output"), device); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5603 | } else { |
| 5604 | ALOGW("loadGlobalConfig() default device not specified"); |
| 5605 | } |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5606 | ALOGV("loadGlobalConfig() mDefaultOutputDevice %08x", mDefaultOutputDevice->mDeviceType); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5607 | } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) { |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5608 | mAvailableInputDevices.loadDevicesFromName((char *)node->value, |
| 5609 | declaredDevices); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5610 | ALOGV("loadGlobalConfig() Available InputDevices %08x", mAvailableInputDevices.types()); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5611 | } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) { |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5612 | mSpeakerDrcEnabled = ConfigParsingUtils::stringToBool((char *)node->value); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5613 | ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled); |
Eric Laurent | eb108a4 | 2014-06-06 14:56:52 -0700 | [diff] [blame] | 5614 | } else if (strcmp(AUDIO_HAL_VERSION_TAG, node->name) == 0) { |
| 5615 | uint32_t major, minor; |
| 5616 | sscanf((char *)node->value, "%u.%u", &major, &minor); |
| 5617 | module->mHalVersion = HARDWARE_DEVICE_API_VERSION(major, minor); |
| 5618 | ALOGV("loadGlobalConfig() mHalVersion = %04x major %u minor %u", |
| 5619 | module->mHalVersion, major, minor); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5620 | } |
| 5621 | node = node->next; |
| 5622 | } |
| 5623 | } |
| 5624 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame^] | 5625 | // TODO candidate to be moved to ConfigParsingUtils |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5626 | status_t AudioPolicyManager::loadAudioPolicyConfig(const char *path) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5627 | { |
| 5628 | cnode *root; |
| 5629 | char *data; |
| 5630 | |
| 5631 | data = (char *)load_file(path, NULL); |
| 5632 | if (data == NULL) { |
| 5633 | return -ENODEV; |
| 5634 | } |
| 5635 | root = config_node("", ""); |
| 5636 | config_load(root, data); |
| 5637 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5638 | loadHwModules(root); |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5639 | // legacy audio_policy.conf files have one global_configuration section |
| 5640 | loadGlobalConfig(root, getModuleFromName(AUDIO_HARDWARE_MODULE_ID_PRIMARY)); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5641 | config_free(root); |
| 5642 | free(root); |
| 5643 | free(data); |
| 5644 | |
| 5645 | ALOGI("loadAudioPolicyConfig() loaded %s\n", path); |
| 5646 | |
| 5647 | return NO_ERROR; |
| 5648 | } |
| 5649 | |
Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5650 | void AudioPolicyManager::defaultAudioPolicyConfig(void) |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5651 | { |
Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5652 | sp<HwModule> module; |
Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5653 | sp<IOProfile> profile; |
Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 5654 | sp<DeviceDescriptor> defaultInputDevice = |
| 5655 | new DeviceDescriptor(String8("builtin-mic"), AUDIO_DEVICE_IN_BUILTIN_MIC); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5656 | mAvailableOutputDevices.add(mDefaultOutputDevice); |
| 5657 | mAvailableInputDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5658 | |
| 5659 | module = new HwModule("primary"); |
| 5660 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5661 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SOURCE, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5662 | profile->mSamplingRates.add(44100); |
| 5663 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 5664 | profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5665 | profile->mSupportedDevices.add(mDefaultOutputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5666 | profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY; |
| 5667 | module->mOutputProfiles.add(profile); |
| 5668 | |
Eric Laurent | 1afeecb | 2014-05-14 08:52:28 -0700 | [diff] [blame] | 5669 | profile = new IOProfile(String8("primary"), AUDIO_PORT_ROLE_SINK, module); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5670 | profile->mSamplingRates.add(8000); |
| 5671 | profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT); |
| 5672 | profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO); |
Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 5673 | profile->mSupportedDevices.add(defaultInputDevice); |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5674 | module->mInputProfiles.add(profile); |
| 5675 | |
| 5676 | mHwModules.add(module); |
| 5677 | } |
| 5678 | |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5679 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) |
| 5680 | { |
| 5681 | // flags to stream type mapping |
| 5682 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { |
| 5683 | return AUDIO_STREAM_ENFORCED_AUDIBLE; |
| 5684 | } |
| 5685 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { |
| 5686 | return AUDIO_STREAM_BLUETOOTH_SCO; |
| 5687 | } |
Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5688 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { |
| 5689 | return AUDIO_STREAM_TTS; |
| 5690 | } |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5691 | |
| 5692 | // usage to stream type mapping |
| 5693 | switch (attr->usage) { |
| 5694 | case AUDIO_USAGE_MEDIA: |
| 5695 | case AUDIO_USAGE_GAME: |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5696 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5697 | return AUDIO_STREAM_MUSIC; |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5698 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5699 | if (isStreamActive(AUDIO_STREAM_ALARM)) { |
| 5700 | return AUDIO_STREAM_ALARM; |
| 5701 | } |
| 5702 | if (isStreamActive(AUDIO_STREAM_RING)) { |
| 5703 | return AUDIO_STREAM_RING; |
| 5704 | } |
| 5705 | if (isInCall()) { |
| 5706 | return AUDIO_STREAM_VOICE_CALL; |
| 5707 | } |
Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5708 | return AUDIO_STREAM_ACCESSIBILITY; |
Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5709 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5710 | return AUDIO_STREAM_SYSTEM; |
| 5711 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5712 | return AUDIO_STREAM_VOICE_CALL; |
| 5713 | |
| 5714 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5715 | return AUDIO_STREAM_DTMF; |
| 5716 | |
| 5717 | case AUDIO_USAGE_ALARM: |
| 5718 | return AUDIO_STREAM_ALARM; |
| 5719 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5720 | return AUDIO_STREAM_RING; |
| 5721 | |
| 5722 | case AUDIO_USAGE_NOTIFICATION: |
| 5723 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5724 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5725 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5726 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5727 | return AUDIO_STREAM_NOTIFICATION; |
| 5728 | |
| 5729 | case AUDIO_USAGE_UNKNOWN: |
| 5730 | default: |
| 5731 | return AUDIO_STREAM_MUSIC; |
| 5732 | } |
| 5733 | } |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5734 | |
| 5735 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) { |
| 5736 | // has flags that map to a strategy? |
| 5737 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { |
| 5738 | return true; |
| 5739 | } |
| 5740 | |
| 5741 | // has known usage? |
| 5742 | switch (paa->usage) { |
| 5743 | case AUDIO_USAGE_UNKNOWN: |
| 5744 | case AUDIO_USAGE_MEDIA: |
| 5745 | case AUDIO_USAGE_VOICE_COMMUNICATION: |
| 5746 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: |
| 5747 | case AUDIO_USAGE_ALARM: |
| 5748 | case AUDIO_USAGE_NOTIFICATION: |
| 5749 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: |
| 5750 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: |
| 5751 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: |
| 5752 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: |
| 5753 | case AUDIO_USAGE_NOTIFICATION_EVENT: |
| 5754 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: |
| 5755 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 5756 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: |
| 5757 | case AUDIO_USAGE_GAME: |
Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5758 | case AUDIO_USAGE_VIRTUAL_SOURCE: |
Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5759 | break; |
| 5760 | default: |
| 5761 | return false; |
| 5762 | } |
| 5763 | return true; |
| 5764 | } |
| 5765 | |
Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5766 | }; // namespace android |