| 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 | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [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 |  | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 27 | #define AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH 128 | 
|  | 28 | #define AUDIO_POLICY_XML_CONFIG_FILE_NAME "audio_policy_configuration.xml" | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 29 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 30 | #include <inttypes.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 31 | #include <math.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 32 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 33 | #include <AudioPolicyManagerInterface.h> | 
|  | 34 | #include <AudioPolicyEngineInstance.h> | 
| Mathias Agopian | 05d19b0 | 2017-02-28 16:28:19 -0800 | [diff] [blame] | 35 | #include <cutils/atomic.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 36 | #include <cutils/properties.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 37 | #include <utils/Log.h> | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 38 | #include <media/AudioParameter.h> | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 39 | #include <media/AudioPolicyHelper.h> | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 40 | #include <soundtrigger/SoundTrigger.h> | 
| Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 41 | #include <system/audio.h> | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 42 | #include <audio_policy_conf.h> | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 43 | #include "AudioPolicyManager.h" | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 44 | #ifndef USE_XML_AUDIO_POLICY_CONF | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 45 | #include <ConfigParsingUtils.h> | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 46 | #include <StreamDescriptor.h> | 
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 47 | #endif | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 48 | #include <Serializer.h> | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 49 | #include "TypeConverter.h" | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 50 | #include <policy.h> | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 51 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 52 | namespace android { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 53 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 54 | //FIXME: workaround for truncated touch sounds | 
|  | 55 | // to be removed when the problem is handled by system UI | 
|  | 56 | #define TOUCH_SOUND_FIXED_DELAY_MS 100 | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 57 | // ---------------------------------------------------------------------------- | 
|  | 58 | // AudioPolicyInterface implementation | 
|  | 59 | // ---------------------------------------------------------------------------- | 
|  | 60 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 61 | status_t AudioPolicyManager::setDeviceConnectionState(audio_devices_t device, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 62 | audio_policy_dev_state_t state, | 
|  | 63 | const char *device_address, | 
|  | 64 | const char *device_name) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 65 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 66 | return setDeviceConnectionStateInt(device, state, device_address, device_name); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 69 | void AudioPolicyManager::broadcastDeviceConnectionState(audio_devices_t device, | 
|  | 70 | audio_policy_dev_state_t state, | 
|  | 71 | const String8 &device_address) | 
|  | 72 | { | 
|  | 73 | AudioParameter param(device_address); | 
|  | 74 | const String8 key(state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE ? | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 75 | AudioParameter::keyStreamConnect : AudioParameter::keyStreamDisconnect); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 76 | param.addInt(key, device); | 
|  | 77 | mpClientInterface->setParameters(AUDIO_IO_HANDLE_NONE, param.toString()); | 
|  | 78 | } | 
|  | 79 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 80 | status_t AudioPolicyManager::setDeviceConnectionStateInt(audio_devices_t device, | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 81 | audio_policy_dev_state_t state, | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 82 | const char *device_address, | 
|  | 83 | const char *device_name) | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 84 | { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 85 | ALOGV("setDeviceConnectionStateInt() device: 0x%X, state %d, address %s name %s", | 
|  | 86 | -            device, state, device_address, device_name); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | // connect/disconnect only 1 device at a time | 
|  | 89 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 90 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 91 | sp<DeviceDescriptor> devDesc = | 
|  | 92 | mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 93 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 94 | // handle output devices | 
|  | 95 | if (audio_is_output_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 96 | SortedVector <audio_io_handle_t> outputs; | 
|  | 97 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 98 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 99 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 100 | // save a copy of the opened output descriptors before any output is opened or closed | 
|  | 101 | // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies() | 
|  | 102 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 103 | switch (state) | 
|  | 104 | { | 
|  | 105 | // handle output device connection | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 106 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 107 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 108 | ALOGW("setDeviceConnectionState() device already connected: %x", device); | 
|  | 109 | return INVALID_OPERATION; | 
|  | 110 | } | 
|  | 111 | ALOGV("setDeviceConnectionState() connecting device %x", device); | 
|  | 112 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 113 | // register new device as available | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 114 | index = mAvailableOutputDevices.add(devDesc); | 
|  | 115 | if (index >= 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 116 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | cf817a2 | 2014-08-04 20:36:31 -0700 | [diff] [blame] | 117 | if (module == 0) { | 
|  | 118 | ALOGD("setDeviceConnectionState() could not find HW module for device %08x", | 
|  | 119 | device); | 
|  | 120 | mAvailableOutputDevices.remove(devDesc); | 
|  | 121 | return INVALID_OPERATION; | 
|  | 122 | } | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 123 | mAvailableOutputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 124 | } else { | 
|  | 125 | return NO_MEMORY; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 128 | // Before checking outputs, broadcast connect event to allow HAL to retrieve dynamic | 
|  | 129 | // parameters on newly connected devices (instead of opening the outputs...) | 
|  | 130 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
|  | 131 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 132 | if (checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress) != NO_ERROR) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 133 | mAvailableOutputDevices.remove(devDesc); | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 134 |  | 
|  | 135 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 136 | devDesc->mAddress); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 137 | return INVALID_OPERATION; | 
|  | 138 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 139 | // Propagate device availability to Engine | 
|  | 140 | mEngine->setDeviceConnectionState(devDesc, state); | 
|  | 141 |  | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 142 | // outputs should never be empty here | 
|  | 143 | ALOG_ASSERT(outputs.size() != 0, "setDeviceConnectionState():" | 
|  | 144 | "checkOutputsForDevice() returned no outputs but status OK"); | 
|  | 145 | ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs", | 
|  | 146 | outputs.size()); | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 147 |  | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 148 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 149 | // handle output device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 150 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 151 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 152 | ALOGW("setDeviceConnectionState() device not connected: %x", device); | 
|  | 153 | return INVALID_OPERATION; | 
|  | 154 | } | 
|  | 155 |  | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 156 | ALOGV("setDeviceConnectionState() disconnecting output device %x", device); | 
|  | 157 |  | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 158 | // Send Disconnect to HALs | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 159 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 160 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 161 | // remove device from available output devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 162 | mAvailableOutputDevices.remove(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 163 |  | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 164 | checkOutputsForDevice(devDesc, state, outputs, devDesc->mAddress); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 165 |  | 
|  | 166 | // Propagate device availability to Engine | 
|  | 167 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 168 | } break; | 
|  | 169 |  | 
|  | 170 | default: | 
|  | 171 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 172 | return BAD_VALUE; | 
|  | 173 | } | 
|  | 174 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 175 | // checkA2dpSuspend must run before checkOutputForAllStrategies so that A2DP | 
|  | 176 | // output is suspended before any tracks are moved to it | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 177 | checkA2dpSuspend(); | 
|  | 178 | checkOutputForAllStrategies(); | 
|  | 179 | // outputs must be closed after checkOutputForAllStrategies() is executed | 
|  | 180 | if (!outputs.isEmpty()) { | 
|  | 181 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 182 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 183 | // close unused outputs after device disconnection or direct outputs that have been | 
|  | 184 | // opened by checkOutputsForDevice() to query dynamic parameters | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 185 | if ((state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 186 | (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) && | 
|  | 187 | (desc->mDirectOpenCount == 0))) { | 
|  | 188 | closeOutput(outputs[i]); | 
|  | 189 | } | 
|  | 190 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 191 | // check again after closing A2DP output to reset mA2dpSuspended if needed | 
|  | 192 | checkA2dpSuspend(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
|  | 195 | updateDevicesAndOutputs(); | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 196 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 197 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 198 | updateCallRouting(newDevice); | 
|  | 199 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 201 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 202 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (desc != mPrimaryOutput)) { | 
|  | 203 | audio_devices_t newDevice = getNewOutputDevice(desc, true /*fromCache*/); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 204 | // do not force device change on duplicated output because if device is 0, it will | 
|  | 205 | // also force a device 0 for the two outputs it is duplicated to which may override | 
|  | 206 | // a valid device selection on those outputs. | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 207 | bool force = !desc->isDuplicated() | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 208 | && (!device_distinguishes_on_address(device) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 209 | // always force when disconnecting (a non-duplicated device) | 
|  | 210 | || (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 211 | setOutputDevice(desc, newDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 212 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 215 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 216 | cleanUpForDevice(devDesc); | 
|  | 217 | } | 
|  | 218 |  | 
| Eric Laurent | 72aa32f | 2014-05-30 18:51:48 -0700 | [diff] [blame] | 219 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | b71e58b | 2014-05-29 16:08:11 -0700 | [diff] [blame] | 220 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 221 | }  // end if is output device | 
|  | 222 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 223 | // handle input devices | 
|  | 224 | if (audio_is_input_device(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 225 | SortedVector <audio_io_handle_t> inputs; | 
|  | 226 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 227 | ssize_t index = mAvailableInputDevices.indexOf(devDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 228 | switch (state) | 
|  | 229 | { | 
|  | 230 | // handle input device connection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 231 | case AUDIO_POLICY_DEVICE_STATE_AVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 232 | if (index >= 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 233 | ALOGW("setDeviceConnectionState() device already connected: %d", device); | 
|  | 234 | return INVALID_OPERATION; | 
|  | 235 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 236 | sp<HwModule> module = mHwModules.getModuleForDevice(device); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 237 | if (module == NULL) { | 
|  | 238 | ALOGW("setDeviceConnectionState(): could not find HW module for device %08x", | 
|  | 239 | device); | 
|  | 240 | return INVALID_OPERATION; | 
|  | 241 | } | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 242 |  | 
|  | 243 | // Before checking intputs, broadcast connect event to allow HAL to retrieve dynamic | 
|  | 244 | // parameters on newly connected devices (instead of opening the inputs...) | 
|  | 245 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
|  | 246 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 247 | if (checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress) != NO_ERROR) { | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 248 | broadcastDeviceConnectionState(device, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 249 | devDesc->mAddress); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 250 | return INVALID_OPERATION; | 
|  | 251 | } | 
|  | 252 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 253 | index = mAvailableInputDevices.add(devDesc); | 
|  | 254 | if (index >= 0) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 255 | mAvailableInputDevices[index]->attach(module); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 256 | } else { | 
|  | 257 | return NO_MEMORY; | 
|  | 258 | } | 
| Eric Laurent | 3ae5f31 | 2015-02-03 17:12:08 -0800 | [diff] [blame] | 259 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 260 | // Propagate device availability to Engine | 
|  | 261 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 262 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 263 |  | 
|  | 264 | // handle input device disconnection | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 265 | case AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE: { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 266 | if (index < 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 267 | ALOGW("setDeviceConnectionState() device not connected: %d", device); | 
|  | 268 | return INVALID_OPERATION; | 
|  | 269 | } | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 270 |  | 
|  | 271 | ALOGV("setDeviceConnectionState() disconnecting input device %x", device); | 
|  | 272 |  | 
|  | 273 | // Set Disconnect to HALs | 
| François Gaffie | 44481e7 | 2016-04-20 07:49:57 +0200 | [diff] [blame] | 274 | broadcastDeviceConnectionState(device, state, devDesc->mAddress); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 275 |  | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 276 | checkInputsForDevice(devDesc, state, inputs, devDesc->mAddress); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 277 | mAvailableInputDevices.remove(devDesc); | 
| Paul McLean | 5c477aa | 2014-08-20 16:47:57 -0700 | [diff] [blame] | 278 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 279 | // Propagate device availability to Engine | 
|  | 280 | mEngine->setDeviceConnectionState(devDesc, state); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 281 | } break; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 282 |  | 
|  | 283 | default: | 
|  | 284 | ALOGE("setDeviceConnectionState() invalid state: %x", state); | 
|  | 285 | return BAD_VALUE; | 
|  | 286 | } | 
|  | 287 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 288 | closeAllInputs(); | 
| Eric Laurent | 5f5fca5 | 2016-08-04 11:48:57 -0700 | [diff] [blame] | 289 | // As the input device list can impact the output device selection, update | 
|  | 290 | // getDeviceForStrategy() cache | 
|  | 291 | updateDevicesAndOutputs(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 292 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 293 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 294 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 295 | updateCallRouting(newDevice); | 
|  | 296 | } | 
|  | 297 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 298 | if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) { | 
|  | 299 | cleanUpForDevice(devDesc); | 
|  | 300 | } | 
|  | 301 |  | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 302 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 303 | return NO_ERROR; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 304 | } // end if is input device | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 305 |  | 
|  | 306 | ALOGW("setDeviceConnectionState() invalid device: %x", device); | 
|  | 307 | return BAD_VALUE; | 
|  | 308 | } | 
|  | 309 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 310 | audio_policy_dev_state_t AudioPolicyManager::getDeviceConnectionState(audio_devices_t device, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 311 | const char *device_address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 312 | { | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 313 | sp<DeviceDescriptor> devDesc = | 
|  | 314 | mHwModules.getDeviceDescriptor(device, device_address, "", | 
|  | 315 | (strlen(device_address) != 0)/*matchAddress*/); | 
|  | 316 |  | 
|  | 317 | if (devDesc == 0) { | 
|  | 318 | ALOGW("getDeviceConnectionState() undeclared device, type %08x, address: %s", | 
|  | 319 | device, device_address); | 
|  | 320 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
|  | 321 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 322 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 323 | DeviceVector *deviceVector; | 
|  | 324 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 325 | if (audio_is_output_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 326 | deviceVector = &mAvailableOutputDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 327 | } else if (audio_is_input_device(device)) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 328 | deviceVector = &mAvailableInputDevices; | 
|  | 329 | } else { | 
|  | 330 | ALOGW("getDeviceConnectionState() invalid device type %08x", device); | 
|  | 331 | return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 332 | } | 
| Eric Laurent | 634b714 | 2016-04-20 13:48:02 -0700 | [diff] [blame] | 333 |  | 
|  | 334 | return (deviceVector->getDevice(device, String8(device_address)) != 0) ? | 
|  | 335 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE : AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE; | 
| Eric Laurent | a1d525f | 2015-01-29 13:36:45 -0800 | [diff] [blame] | 336 | } | 
|  | 337 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 338 | status_t AudioPolicyManager::handleDeviceConfigChange(audio_devices_t device, | 
|  | 339 | const char *device_address, | 
|  | 340 | const char *device_name) | 
|  | 341 | { | 
|  | 342 | status_t status; | 
|  | 343 |  | 
|  | 344 | ALOGV("handleDeviceConfigChange(() device: 0x%X, address %s name %s", | 
|  | 345 | device, device_address, device_name); | 
|  | 346 |  | 
| Pavlin Radoslavov | c694ff4 | 2017-01-09 23:27:29 -0800 | [diff] [blame] | 347 | // connect/disconnect only 1 device at a time | 
|  | 348 | if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE; | 
|  | 349 |  | 
| Pavlin Radoslavov | f862bc6 | 2016-12-26 18:57:22 -0800 | [diff] [blame] | 350 | // Check if the device is currently connected | 
|  | 351 | sp<DeviceDescriptor> devDesc = | 
|  | 352 | mHwModules.getDeviceDescriptor(device, device_address, device_name); | 
|  | 353 | ssize_t index = mAvailableOutputDevices.indexOf(devDesc); | 
|  | 354 | if (index < 0) { | 
|  | 355 | // Nothing to do: device is not connected | 
|  | 356 | return NO_ERROR; | 
|  | 357 | } | 
|  | 358 |  | 
|  | 359 | // Toggle the device state: UNAVAILABLE -> AVAILABLE | 
|  | 360 | // This will force reading again the device configuration | 
|  | 361 | status = setDeviceConnectionState(device, | 
|  | 362 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 363 | device_address, device_name); | 
|  | 364 | if (status != NO_ERROR) { | 
|  | 365 | ALOGW("handleDeviceConfigChange() error disabling connection state: %d", | 
|  | 366 | status); | 
|  | 367 | return status; | 
|  | 368 | } | 
|  | 369 |  | 
|  | 370 | status = setDeviceConnectionState(device, | 
|  | 371 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 372 | device_address, device_name); | 
|  | 373 | if (status != NO_ERROR) { | 
|  | 374 | ALOGW("handleDeviceConfigChange() error enabling connection state: %d", | 
|  | 375 | status); | 
|  | 376 | return status; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | return NO_ERROR; | 
|  | 380 | } | 
|  | 381 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 382 | uint32_t AudioPolicyManager::updateCallRouting(audio_devices_t rxDevice, uint32_t delayMs) | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 383 | { | 
|  | 384 | bool createTxPatch = false; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 385 | status_t status; | 
|  | 386 | audio_patch_handle_t afPatchHandle; | 
|  | 387 | DeviceVector deviceList; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 388 | uint32_t muteWaitMs = 0; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 389 |  | 
| Andy Hung | a76c7de | 2016-12-13 19:14:31 -0800 | [diff] [blame] | 390 | if(!hasPrimaryOutput() || mPrimaryOutput->device() == AUDIO_DEVICE_OUT_STUB) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 391 | return muteWaitMs; | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 392 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 393 | audio_devices_t txDevice = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 394 | ALOGV("updateCallRouting device rxDevice %08x txDevice %08x", rxDevice, txDevice); | 
|  | 395 |  | 
|  | 396 | // release existing RX patch if any | 
|  | 397 | if (mCallRxPatch != 0) { | 
|  | 398 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 399 | mCallRxPatch.clear(); | 
|  | 400 | } | 
|  | 401 | // release TX patch if any | 
|  | 402 | if (mCallTxPatch != 0) { | 
|  | 403 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 404 | mCallTxPatch.clear(); | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 | // If the RX device is on the primary HW module, then use legacy routing method for voice calls | 
|  | 408 | // via setOutputDevice() on primary output. | 
|  | 409 | // Otherwise, create two audio patches for TX and RX path. | 
|  | 410 | if (availablePrimaryOutputDevices() & rxDevice) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 411 | muteWaitMs = setOutputDevice(mPrimaryOutput, rxDevice, true, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 412 | // If the TX device is also on the primary HW module, setOutputDevice() will take care | 
|  | 413 | // of it due to legacy implementation. If not, create a patch. | 
|  | 414 | if ((availablePrimaryInputDevices() & txDevice & ~AUDIO_DEVICE_BIT_IN) | 
|  | 415 | == AUDIO_DEVICE_NONE) { | 
|  | 416 | createTxPatch = true; | 
|  | 417 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 418 | } else { // create RX path audio patch | 
|  | 419 | struct audio_patch patch; | 
|  | 420 |  | 
|  | 421 | patch.num_sources = 1; | 
|  | 422 | patch.num_sinks = 1; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 423 | deviceList = mAvailableOutputDevices.getDevicesFromType(rxDevice); | 
|  | 424 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 425 | "updateCallRouting() selected device not in output device list"); | 
|  | 426 | sp<DeviceDescriptor> rxSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 427 | deviceList = mAvailableInputDevices.getDevicesFromType(AUDIO_DEVICE_IN_TELEPHONY_RX); | 
|  | 428 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 429 | "updateCallRouting() no telephony RX device"); | 
|  | 430 | sp<DeviceDescriptor> rxSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 431 |  | 
|  | 432 | rxSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 433 | rxSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 434 |  | 
|  | 435 | // request to reuse existing output stream if one is already opened to reach the RX device | 
|  | 436 | SortedVector<audio_io_handle_t> outputs = | 
|  | 437 | getOutputsForDevice(rxDevice, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 438 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 439 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 440 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 441 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 442 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 443 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 444 | "updateCallRouting() RX device output is duplicated"); | 
|  | 445 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 446 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 447 | patch.num_sources = 2; | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 451 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 452 | ALOGW_IF(status != NO_ERROR, "updateCallRouting() error %d creating RX audio patch", | 
|  | 453 | status); | 
|  | 454 | if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 455 | mCallRxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 456 | mCallRxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 457 | mCallRxPatch->mUid = mUidCached; | 
|  | 458 | } | 
|  | 459 | createTxPatch = true; | 
|  | 460 | } | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 461 | if (createTxPatch) { // create TX path audio patch | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 462 | struct audio_patch patch; | 
| Eric Laurent | 8ae7312 | 2016-04-12 10:13:29 -0700 | [diff] [blame] | 463 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 464 | patch.num_sources = 1; | 
|  | 465 | patch.num_sinks = 1; | 
|  | 466 | deviceList = mAvailableInputDevices.getDevicesFromType(txDevice); | 
|  | 467 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 468 | "updateCallRouting() selected device not in input device list"); | 
|  | 469 | sp<DeviceDescriptor> txSourceDeviceDesc = deviceList.itemAt(0); | 
|  | 470 | txSourceDeviceDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 471 | deviceList = mAvailableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX); | 
|  | 472 | ALOG_ASSERT(!deviceList.isEmpty(), | 
|  | 473 | "updateCallRouting() no telephony TX device"); | 
|  | 474 | sp<DeviceDescriptor> txSinkDeviceDesc = deviceList.itemAt(0); | 
|  | 475 | txSinkDeviceDesc->toAudioPortConfig(&patch.sinks[0]); | 
|  | 476 |  | 
|  | 477 | SortedVector<audio_io_handle_t> outputs = | 
|  | 478 | getOutputsForDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX, mOutputs); | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 479 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 480 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 481 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 482 | // request to reuse existing output stream if one is already opened to reach the TX | 
|  | 483 | // path output device | 
|  | 484 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 485 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 486 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 487 | "updateCallRouting() RX device output is duplicated"); | 
|  | 488 | outputDesc->toAudioPortConfig(&patch.sources[1]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 489 | patch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 490 | patch.num_sources = 2; | 
|  | 491 | } | 
|  | 492 |  | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 493 | // terminate active capture if on the same HW module as the call TX source device | 
|  | 494 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 495 | // call TX device but this information is not in the audio patch and logic here must be | 
|  | 496 | // symmetric to the one in startInput() | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 497 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); | 
|  | 498 | for (size_t i = 0; i < activeInputs.size(); i++) { | 
|  | 499 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; | 
|  | 500 | if (activeDesc->hasSameHwModuleAs(txSourceDeviceDesc)) { | 
|  | 501 | AudioSessionCollection activeSessions = | 
|  | 502 | activeDesc->getAudioSessions(true /*activeOnly*/); | 
|  | 503 | for (size_t j = 0; j < activeSessions.size(); j++) { | 
|  | 504 | audio_session_t activeSession = activeSessions.keyAt(j); | 
|  | 505 | stopInput(activeDesc->mIoHandle, activeSession); | 
|  | 506 | releaseInput(activeDesc->mIoHandle, activeSession); | 
|  | 507 | } | 
| Eric Laurent | c0a889f | 2015-10-14 14:36:34 -0700 | [diff] [blame] | 508 | } | 
|  | 509 | } | 
|  | 510 |  | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 511 | afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 512 | status = mpClientInterface->createAudioPatch(&patch, &afPatchHandle, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 513 | ALOGW_IF(status != NO_ERROR, "setPhoneState() error %d creating TX audio patch", | 
|  | 514 | status); | 
|  | 515 | if (status == NO_ERROR) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 516 | mCallTxPatch = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 517 | mCallTxPatch->mAfPatchHandle = afPatchHandle; | 
|  | 518 | mCallTxPatch->mUid = mUidCached; | 
|  | 519 | } | 
|  | 520 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 521 |  | 
|  | 522 | return muteWaitMs; | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 523 | } | 
|  | 524 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 525 | void AudioPolicyManager::setPhoneState(audio_mode_t state) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 526 | { | 
|  | 527 | ALOGV("setPhoneState() state %d", state); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 528 | // store previous phone state for management of sonification strategy below | 
|  | 529 | int oldState = mEngine->getPhoneState(); | 
|  | 530 |  | 
|  | 531 | if (mEngine->setPhoneState(state) != NO_ERROR) { | 
|  | 532 | ALOGW("setPhoneState() invalid or same state %d", state); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 533 | return; | 
|  | 534 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 535 | /// Opens: can these line be executed after the switch of volume curves??? | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 536 | // if leaving call state, handle special case of active streams | 
|  | 537 | // pertaining to sonification strategy see handleIncallSonification() | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 538 | if (isStateInCall(oldState)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 539 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 540 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 541 | handleIncallSonification((audio_stream_type_t)stream, false, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 542 | } | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 543 |  | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 544 | // force reevaluating accessibility routing when call stops | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 545 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 546 | } | 
|  | 547 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 548 | /** | 
|  | 549 | * Switching to or from incall state or switching between telephony and VoIP lead to force | 
|  | 550 | * routing command. | 
|  | 551 | */ | 
|  | 552 | bool force = ((is_state_in_call(oldState) != is_state_in_call(state)) | 
|  | 553 | || (is_state_in_call(state) && (state != oldState))); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 554 |  | 
|  | 555 | // check for device and output changes triggered by new phone state | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 556 | checkA2dpSuspend(); | 
|  | 557 | checkOutputForAllStrategies(); | 
|  | 558 | updateDevicesAndOutputs(); | 
|  | 559 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 560 | int delayMs = 0; | 
|  | 561 | if (isStateInCall(state)) { | 
|  | 562 | nsecs_t sysTime = systemTime(); | 
|  | 563 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 564 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 565 | // mute media and sonification strategies and delay device switch by the largest | 
|  | 566 | // latency of any output where either strategy is active. | 
|  | 567 | // This avoid sending the ring tone or music tail into the earpiece or headset. | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 568 | if ((isStrategyActive(desc, STRATEGY_MEDIA, | 
|  | 569 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 570 | sysTime) || | 
|  | 571 | isStrategyActive(desc, STRATEGY_SONIFICATION, | 
|  | 572 | SONIFICATION_HEADSET_MUSIC_DELAY, | 
|  | 573 | sysTime)) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 574 | (delayMs < (int)desc->latency()*2)) { | 
|  | 575 | delayMs = desc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 576 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 577 | setStrategyMute(STRATEGY_MEDIA, true, desc); | 
|  | 578 | setStrategyMute(STRATEGY_MEDIA, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 579 | getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/)); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 580 | setStrategyMute(STRATEGY_SONIFICATION, true, desc); | 
|  | 581 | setStrategyMute(STRATEGY_SONIFICATION, false, desc, MUTE_TIME_MS, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 582 | getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/)); | 
|  | 583 | } | 
|  | 584 | } | 
|  | 585 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 586 | if (hasPrimaryOutput()) { | 
|  | 587 | // Note that despite the fact that getNewOutputDevice() is called on the primary output, | 
|  | 588 | // the device returned is not necessarily reachable via this output | 
|  | 589 | audio_devices_t rxDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/); | 
|  | 590 | // force routing command to audio hardware when ending call | 
|  | 591 | // even if no device change is needed | 
|  | 592 | if (isStateInCall(oldState) && rxDevice == AUDIO_DEVICE_NONE) { | 
|  | 593 | rxDevice = mPrimaryOutput->device(); | 
|  | 594 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 595 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 596 | if (state == AUDIO_MODE_IN_CALL) { | 
|  | 597 | updateCallRouting(rxDevice, delayMs); | 
|  | 598 | } else if (oldState == AUDIO_MODE_IN_CALL) { | 
|  | 599 | if (mCallRxPatch != 0) { | 
|  | 600 | mpClientInterface->releaseAudioPatch(mCallRxPatch->mAfPatchHandle, 0); | 
|  | 601 | mCallRxPatch.clear(); | 
|  | 602 | } | 
|  | 603 | if (mCallTxPatch != 0) { | 
|  | 604 | mpClientInterface->releaseAudioPatch(mCallTxPatch->mAfPatchHandle, 0); | 
|  | 605 | mCallTxPatch.clear(); | 
|  | 606 | } | 
|  | 607 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
|  | 608 | } else { | 
|  | 609 | setOutputDevice(mPrimaryOutput, rxDevice, force, 0); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 610 | } | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 611 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 612 | // if entering in call state, handle special case of active streams | 
|  | 613 | // pertaining to sonification strategy see handleIncallSonification() | 
|  | 614 | if (isStateInCall(state)) { | 
|  | 615 | ALOGV("setPhoneState() in call state management: new state is %d", state); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 616 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 617 | handleIncallSonification((audio_stream_type_t)stream, true, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 618 | } | 
| Eric Laurent | 63dea1d | 2015-07-02 17:10:28 -0700 | [diff] [blame] | 619 |  | 
|  | 620 | // force reevaluating accessibility routing when call starts | 
|  | 621 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 622 | } | 
|  | 623 |  | 
|  | 624 | // 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] | 625 | if (state == AUDIO_MODE_RINGTONE && | 
|  | 626 | isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 627 | mLimitRingtoneVolume = true; | 
|  | 628 | } else { | 
|  | 629 | mLimitRingtoneVolume = false; | 
|  | 630 | } | 
|  | 631 | } | 
|  | 632 |  | 
| Jean-Michel Trivi | 887a9ed | 2015-03-31 18:02:24 -0700 | [diff] [blame] | 633 | audio_mode_t AudioPolicyManager::getPhoneState() { | 
|  | 634 | return mEngine->getPhoneState(); | 
|  | 635 | } | 
|  | 636 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 637 | void AudioPolicyManager::setForceUse(audio_policy_force_use_t usage, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 638 | audio_policy_forced_cfg_t config) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 639 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 640 | ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mEngine->getPhoneState()); | 
| Eric Laurent | 8dc87a6 | 2017-05-16 19:00:40 -0700 | [diff] [blame^] | 641 | if (config == mEngine->getForceUse(usage)) { | 
|  | 642 | return; | 
|  | 643 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 644 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 645 | if (mEngine->setForceUse(usage, config) != NO_ERROR) { | 
|  | 646 | ALOGW("setForceUse() could not set force cfg %d for usage %d", config, usage); | 
|  | 647 | return; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 648 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 649 | bool forceVolumeReeval = (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) || | 
|  | 650 | (usage == AUDIO_POLICY_FORCE_FOR_DOCK) || | 
|  | 651 | (usage == AUDIO_POLICY_FORCE_FOR_SYSTEM); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 652 |  | 
|  | 653 | // check for device and output changes triggered by new force usage | 
|  | 654 | checkA2dpSuspend(); | 
|  | 655 | checkOutputForAllStrategies(); | 
|  | 656 | updateDevicesAndOutputs(); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 657 |  | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 658 | //FIXME: workaround for truncated touch sounds | 
|  | 659 | // to be removed when the problem is handled by system UI | 
|  | 660 | uint32_t delayMs = 0; | 
|  | 661 | uint32_t waitMs = 0; | 
|  | 662 | if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) { | 
|  | 663 | delayMs = TOUCH_SOUND_FIXED_DELAY_MS; | 
|  | 664 | } | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 665 | if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) { | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 666 | audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/); | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 667 | waitMs = updateCallRouting(newDevice, delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 668 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 669 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 670 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i); | 
|  | 671 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/); | 
|  | 672 | if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 673 | waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE), | 
|  | 674 | delayMs); | 
| Eric Laurent | c2730ba | 2014-07-20 15:47:07 -0700 | [diff] [blame] | 675 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 676 | if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 677 | applyStreamVolumes(outputDesc, newDevice, waitMs, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 678 | } | 
|  | 679 | } | 
|  | 680 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 681 | Vector<sp <AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); | 
|  | 682 | for (size_t i = 0; i < activeInputs.size(); i++) { | 
|  | 683 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; | 
|  | 684 | audio_devices_t newDevice = getNewInputDevice(activeDesc); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 685 | // Force new input selection if the new device can not be reached via current input | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 686 | if (activeDesc->mProfile->getSupportedDevices().types() & | 
|  | 687 | (newDevice & ~AUDIO_DEVICE_BIT_IN)) { | 
|  | 688 | setInputDevice(activeDesc->mIoHandle, newDevice); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 689 | } else { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 690 | closeInput(activeDesc->mIoHandle); | 
| Eric Laurent | c171c7c | 2015-09-25 12:21:06 -0700 | [diff] [blame] | 691 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 692 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 693 | } | 
|  | 694 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 695 | void AudioPolicyManager::setSystemProperty(const char* property, const char* value) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 696 | { | 
|  | 697 | ALOGV("setSystemProperty() property %s, value %s", property, value); | 
|  | 698 | } | 
|  | 699 |  | 
|  | 700 | // Find a direct output profile compatible with the parameters passed, even if the input flags do | 
|  | 701 | // not explicitly request a direct output | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 702 | sp<IOProfile> AudioPolicyManager::getProfileForDirectOutput( | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 703 | audio_devices_t device, | 
|  | 704 | uint32_t samplingRate, | 
|  | 705 | audio_format_t format, | 
|  | 706 | audio_channel_mask_t channelMask, | 
|  | 707 | audio_output_flags_t flags) | 
|  | 708 | { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 709 | // only retain flags that will drive the direct output profile selection | 
|  | 710 | // if explicitly requested | 
|  | 711 | static const uint32_t kRelevantFlags = | 
|  | 712 | (AUDIO_OUTPUT_FLAG_HW_AV_SYNC | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
|  | 713 | flags = | 
|  | 714 | (audio_output_flags_t)((flags & kRelevantFlags) | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 715 |  | 
|  | 716 | sp<IOProfile> profile; | 
|  | 717 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 718 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
|  | 719 | if (mHwModules[i]->mHandle == 0) { | 
|  | 720 | continue; | 
|  | 721 | } | 
|  | 722 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 723 | sp<IOProfile> curProfile = mHwModules[i]->mOutputProfiles[j]; | 
|  | 724 | if (!curProfile->isCompatibleProfile(device, String8(""), | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 725 | samplingRate, NULL /*updatedSamplingRate*/, | 
|  | 726 | format, NULL /*updatedFormat*/, | 
|  | 727 | channelMask, NULL /*updatedChannelMask*/, | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 728 | flags)) { | 
|  | 729 | continue; | 
|  | 730 | } | 
|  | 731 | // reject profiles not corresponding to a device currently available | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 732 | if ((mAvailableOutputDevices.types() & curProfile->getSupportedDevicesType()) == 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 733 | continue; | 
|  | 734 | } | 
|  | 735 | // if several profiles are compatible, give priority to one with offload capability | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 736 | if (profile != 0 && ((curProfile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0)) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 737 | continue; | 
|  | 738 | } | 
|  | 739 | profile = curProfile; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 740 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 741 | break; | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 742 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 743 | } | 
|  | 744 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 745 | return profile; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 746 | } | 
|  | 747 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 748 | audio_io_handle_t AudioPolicyManager::getOutput(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 749 | uint32_t samplingRate, | 
|  | 750 | audio_format_t format, | 
|  | 751 | audio_channel_mask_t channelMask, | 
|  | 752 | audio_output_flags_t flags, | 
|  | 753 | const audio_offload_info_t *offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 754 | { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 755 | routing_strategy strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 756 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 757 | ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x", | 
|  | 758 | device, stream, samplingRate, format, channelMask, flags); | 
|  | 759 |  | 
| Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 760 | return getOutputForDevice(device, AUDIO_SESSION_ALLOCATE, uid_t{0} /*Invalid uid*/, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 761 | stream, samplingRate,format, channelMask, | 
|  | 762 | flags, offloadInfo); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 763 | } | 
|  | 764 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 765 | status_t AudioPolicyManager::getOutputForAttr(const audio_attributes_t *attr, | 
|  | 766 | audio_io_handle_t *output, | 
|  | 767 | audio_session_t session, | 
|  | 768 | audio_stream_type_t *stream, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 769 | uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 770 | const audio_config_t *config, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 771 | audio_output_flags_t flags, | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 772 | audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 773 | audio_port_handle_t *portId) | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 774 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 775 | audio_attributes_t attributes; | 
|  | 776 | if (attr != NULL) { | 
|  | 777 | if (!isValidAttributes(attr)) { | 
|  | 778 | ALOGE("getOutputForAttr() invalid attributes: usage=%d content=%d flags=0x%x tags=[%s]", | 
|  | 779 | attr->usage, attr->content_type, attr->flags, | 
|  | 780 | attr->tags); | 
|  | 781 | return BAD_VALUE; | 
|  | 782 | } | 
|  | 783 | attributes = *attr; | 
|  | 784 | } else { | 
|  | 785 | if (*stream < AUDIO_STREAM_MIN || *stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
|  | 786 | ALOGE("getOutputForAttr():  invalid stream type"); | 
|  | 787 | return BAD_VALUE; | 
|  | 788 | } | 
|  | 789 | stream_type_to_audio_attributes(*stream, &attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 790 | } | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 791 |  | 
|  | 792 | // TODO: check for existing client for this port ID | 
|  | 793 | if (*portId == AUDIO_PORT_HANDLE_NONE) { | 
|  | 794 | *portId = AudioPort::getNextUniqueId(); | 
|  | 795 | } | 
|  | 796 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 797 | sp<SwAudioOutputDescriptor> desc; | 
| Jean-Michel Trivi | e8deced | 2016-02-11 12:50:39 -0800 | [diff] [blame] | 798 | if (mPolicyMixes.getOutputForAttr(attributes, uid, desc) == NO_ERROR) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 799 | ALOG_ASSERT(desc != 0, "Invalid desc returned by getOutputForAttr"); | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 800 | if (!audio_has_proportional_frames(config->format)) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 801 | return BAD_VALUE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 802 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 803 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 804 | *output = desc->mIoHandle; | 
|  | 805 | ALOGV("getOutputForAttr() returns output %d", *output); | 
|  | 806 | return NO_ERROR; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 807 | } | 
|  | 808 | if (attributes.usage == AUDIO_USAGE_VIRTUAL_SOURCE) { | 
|  | 809 | ALOGW("getOutputForAttr() no policy mix found for usage AUDIO_USAGE_VIRTUAL_SOURCE"); | 
|  | 810 | return BAD_VALUE; | 
|  | 811 | } | 
|  | 812 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 813 | ALOGV("getOutputForAttr() usage=%d, content=%d, tag=%s flags=%08x" | 
|  | 814 | " session %d selectedDeviceId %d", | 
|  | 815 | attributes.usage, attributes.content_type, attributes.tags, attributes.flags, | 
|  | 816 | session, selectedDeviceId); | 
|  | 817 |  | 
|  | 818 | *stream = streamTypefromAttributesInt(&attributes); | 
|  | 819 |  | 
|  | 820 | // Explicit routing? | 
|  | 821 | sp<DeviceDescriptor> deviceDesc; | 
|  | 822 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 823 | if (mAvailableOutputDevices[i]->getId() == selectedDeviceId) { | 
|  | 824 | deviceDesc = mAvailableOutputDevices[i]; | 
|  | 825 | break; | 
|  | 826 | } | 
|  | 827 | } | 
|  | 828 | mOutputRoutes.addRoute(session, *stream, SessionRoute::SOURCE_TYPE_NA, deviceDesc, uid); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 829 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 830 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&attributes); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 831 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 832 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 833 | if ((attributes.flags & AUDIO_FLAG_HW_AV_SYNC) != 0) { | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 834 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_HW_AV_SYNC); | 
|  | 835 | } | 
|  | 836 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 837 | ALOGV("getOutputForAttr() device 0x%x, samplingRate %d, format %x, channelMask %x, flags %x", | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 838 | device, config->sample_rate, config->format, config->channel_mask, flags); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 839 |  | 
| Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 840 | *output = getOutputForDevice(device, session, uid, *stream, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 841 | config->sample_rate, config->format, config->channel_mask, | 
|  | 842 | flags, &config->offload_info); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 843 | if (*output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 844 | mOutputRoutes.removeRoute(session); | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 845 | return INVALID_OPERATION; | 
|  | 846 | } | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 847 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 848 | return NO_ERROR; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 849 | } | 
|  | 850 |  | 
|  | 851 | audio_io_handle_t AudioPolicyManager::getOutputForDevice( | 
|  | 852 | audio_devices_t device, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 853 | audio_session_t session __unused, | 
| Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 854 | uid_t clientUid, | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 855 | audio_stream_type_t stream, | 
|  | 856 | uint32_t samplingRate, | 
|  | 857 | audio_format_t format, | 
|  | 858 | audio_channel_mask_t channelMask, | 
|  | 859 | audio_output_flags_t flags, | 
|  | 860 | const audio_offload_info_t *offloadInfo) | 
|  | 861 | { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 862 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 863 | status_t status; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 864 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 865 | #ifdef AUDIO_POLICY_TEST | 
|  | 866 | if (mCurOutput != 0) { | 
|  | 867 | ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d", | 
|  | 868 | mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput); | 
|  | 869 |  | 
|  | 870 | if (mTestOutputs[mCurOutput] == 0) { | 
|  | 871 | ALOGV("getOutput() opening test output"); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 872 | sp<AudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(NULL, | 
|  | 873 | mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 874 | outputDesc->mDevice = mTestDevice; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 875 | outputDesc->mLatency = mTestLatencyMs; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 876 | outputDesc->mFlags = | 
|  | 877 | (audio_output_flags_t)(mDirectOutput ? AUDIO_OUTPUT_FLAG_DIRECT : 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 878 | outputDesc->mRefCount[stream] = 0; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 879 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 880 | config.sample_rate = mTestSamplingRate; | 
|  | 881 | config.channel_mask = mTestChannels; | 
|  | 882 | config.format = mTestFormat; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 883 | if (offloadInfo != NULL) { | 
|  | 884 | config.offload_info = *offloadInfo; | 
|  | 885 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 886 | status = mpClientInterface->openOutput(0, | 
|  | 887 | &mTestOutputs[mCurOutput], | 
|  | 888 | &config, | 
|  | 889 | &outputDesc->mDevice, | 
|  | 890 | String8(""), | 
|  | 891 | &outputDesc->mLatency, | 
|  | 892 | outputDesc->mFlags); | 
|  | 893 | if (status == NO_ERROR) { | 
|  | 894 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 895 | outputDesc->mFormat = config.format; | 
|  | 896 | outputDesc->mChannelMask = config.channel_mask; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 897 | AudioParameter outputCmd = AudioParameter(); | 
|  | 898 | outputCmd.addInt(String8("set_id"),mCurOutput); | 
|  | 899 | mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString()); | 
|  | 900 | addOutput(mTestOutputs[mCurOutput], outputDesc); | 
|  | 901 | } | 
|  | 902 | } | 
|  | 903 | return mTestOutputs[mCurOutput]; | 
|  | 904 | } | 
|  | 905 | #endif //AUDIO_POLICY_TEST | 
|  | 906 |  | 
|  | 907 | // open a direct output if required by specified parameters | 
|  | 908 | //force direct flag if offload flag is set: offloading implies a direct output stream | 
|  | 909 | // and all common behaviors are driven by checking only the direct flag | 
|  | 910 | // this should normally be set appropriately in the policy configuration file | 
|  | 911 | if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 912 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 913 | } | 
| Eric Laurent | 93c3d41 | 2014-08-01 14:48:35 -0700 | [diff] [blame] | 914 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 915 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 916 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 917 | // only allow deep buffering for music stream type | 
|  | 918 | if (stream != AUDIO_STREAM_MUSIC) { | 
|  | 919 | flags = (audio_output_flags_t)(flags &~AUDIO_OUTPUT_FLAG_DEEP_BUFFER); | 
| Ravi Kumar Alamanda | 439e4ed | 2015-04-03 12:13:21 -0700 | [diff] [blame] | 920 | } else if (/* stream == AUDIO_STREAM_MUSIC && */ | 
|  | 921 | flags == AUDIO_OUTPUT_FLAG_NONE && | 
|  | 922 | property_get_bool("audio.deep_buffer.media", false /* default_value */)) { | 
|  | 923 | // use DEEP_BUFFER as default output for music stream type | 
|  | 924 | flags = (audio_output_flags_t)AUDIO_OUTPUT_FLAG_DEEP_BUFFER; | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 925 | } | 
| Ravi Kumar Alamanda | c36a889 | 2015-04-24 16:35:49 -0700 | [diff] [blame] | 926 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 927 | flags = AUDIO_OUTPUT_FLAG_TTS; | 
|  | 928 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 929 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 930 | sp<IOProfile> profile; | 
|  | 931 |  | 
|  | 932 | // 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] | 933 | // and not explicitly requested | 
|  | 934 | if (((flags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 935 | audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX && | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 936 | audio_channel_count_from_out_mask(channelMask) <= 2) { | 
|  | 937 | goto non_direct_output; | 
|  | 938 | } | 
|  | 939 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 940 | // Do not allow offloading if one non offloadable effect is enabled or MasterMono is enabled. | 
|  | 941 | // This prevents creating an offloaded track and tearing it down immediately after start | 
|  | 942 | // when audioflinger detects there is an active non offloadable effect. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 943 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 944 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 945 | // in the background. | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 946 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 947 | if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) || | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 948 | !(mEffects.isNonOffloadableEffectEnabled() || mMasterMono)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 949 | profile = getProfileForDirectOutput(device, | 
|  | 950 | samplingRate, | 
|  | 951 | format, | 
|  | 952 | channelMask, | 
|  | 953 | (audio_output_flags_t)flags); | 
|  | 954 | } | 
|  | 955 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 956 | if (profile != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 957 | sp<SwAudioOutputDescriptor> outputDesc = NULL; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 958 |  | 
|  | 959 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 960 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 961 | if (!desc->isDuplicated() && (profile == desc->mProfile)) { | 
|  | 962 | outputDesc = desc; | 
| Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 963 | // reuse direct output if currently open by the same client | 
|  | 964 | // and configured with same parameters | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 965 | if ((samplingRate == outputDesc->mSamplingRate) && | 
| Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 966 | audio_formats_match(format, outputDesc->mFormat) && | 
|  | 967 | (channelMask == outputDesc->mChannelMask)) { | 
|  | 968 | if (clientUid == outputDesc->mDirectClientUid) { | 
|  | 969 | outputDesc->mDirectOpenCount++; | 
|  | 970 | ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i)); | 
|  | 971 | return mOutputs.keyAt(i); | 
|  | 972 | } else { | 
|  | 973 | ALOGV("getOutput() do not reuse direct output because current client (%ld) " | 
|  | 974 | "is not the same as requesting client (%ld)", | 
|  | 975 | (long)outputDesc->mDirectClientUid, (long)clientUid); | 
|  | 976 | goto non_direct_output; | 
|  | 977 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 978 | } | 
|  | 979 | } | 
|  | 980 | } | 
|  | 981 | // close direct output if currently open and configured with different parameters | 
|  | 982 | if (outputDesc != NULL) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 983 | closeOutput(outputDesc->mIoHandle); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 984 | } | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 985 |  | 
|  | 986 | // if the selected profile is offloaded and no offload info was specified, | 
|  | 987 | // create a default one | 
|  | 988 | audio_offload_info_t defaultOffloadInfo = AUDIO_INFO_INITIALIZER; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 989 | if ((profile->getFlags() & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && !offloadInfo) { | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 990 | flags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
|  | 991 | defaultOffloadInfo.sample_rate = samplingRate; | 
|  | 992 | defaultOffloadInfo.channel_mask = channelMask; | 
|  | 993 | defaultOffloadInfo.format = format; | 
|  | 994 | defaultOffloadInfo.stream_type = stream; | 
|  | 995 | defaultOffloadInfo.bit_rate = 0; | 
|  | 996 | defaultOffloadInfo.duration_us = -1; | 
|  | 997 | defaultOffloadInfo.has_video = true; // conservative | 
|  | 998 | defaultOffloadInfo.is_streaming = true; // likely | 
|  | 999 | offloadInfo = &defaultOffloadInfo; | 
|  | 1000 | } | 
|  | 1001 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1002 | outputDesc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1003 | outputDesc->mDevice = device; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1004 | outputDesc->mLatency = 0; | 
| Eric Laurent | 861a628 | 2015-05-18 15:40:16 -0700 | [diff] [blame] | 1005 | outputDesc->mFlags = (audio_output_flags_t)(outputDesc->mFlags | flags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1006 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 1007 | config.sample_rate = samplingRate; | 
|  | 1008 | config.channel_mask = channelMask; | 
|  | 1009 | config.format = format; | 
| Phil Burk | 77cce80 | 2014-08-04 16:18:15 -0700 | [diff] [blame] | 1010 | if (offloadInfo != NULL) { | 
|  | 1011 | config.offload_info = *offloadInfo; | 
|  | 1012 | } | 
| Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1013 | DeviceVector outputDevices = mAvailableOutputDevices.getDevicesFromType(device); | 
|  | 1014 | String8 address = outputDevices.size() > 0 ? outputDevices.itemAt(0)->mAddress | 
|  | 1015 | : String8(""); | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1016 | status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1017 | &output, | 
|  | 1018 | &config, | 
|  | 1019 | &outputDesc->mDevice, | 
| Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1020 | address, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1021 | &outputDesc->mLatency, | 
|  | 1022 | outputDesc->mFlags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1023 |  | 
|  | 1024 | // only accept an output with the requested parameters | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1025 | if (status != NO_ERROR || | 
|  | 1026 | (samplingRate != 0 && samplingRate != config.sample_rate) || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1027 | (format != AUDIO_FORMAT_DEFAULT && !audio_formats_match(format, config.format)) || | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1028 | (channelMask != 0 && channelMask != config.channel_mask)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1029 | ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d," | 
|  | 1030 | "format %d %d, channelMask %04x %04x", output, samplingRate, | 
|  | 1031 | outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask, | 
|  | 1032 | outputDesc->mChannelMask); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1033 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1034 | mpClientInterface->closeOutput(output); | 
|  | 1035 | } | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1036 | // fall back to mixer output if possible when the direct output could not be open | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1037 | if (audio_is_linear_pcm(format) && samplingRate <= SAMPLE_RATE_HZ_MAX) { | 
| Eric Laurent | a82797f | 2015-01-30 11:49:43 -0800 | [diff] [blame] | 1038 | goto non_direct_output; | 
|  | 1039 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1040 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1041 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1042 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 1043 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 1044 | outputDesc->mFormat = config.format; | 
|  | 1045 | outputDesc->mRefCount[stream] = 0; | 
|  | 1046 | outputDesc->mStopTime[stream] = 0; | 
|  | 1047 | outputDesc->mDirectOpenCount = 1; | 
| Kevin Rocard | 551061a | 2017-02-06 15:59:29 -0800 | [diff] [blame] | 1048 | outputDesc->mDirectClientUid = clientUid; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1049 | addOutput(output, outputDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1050 | mPreviousOutputs = mOutputs; | 
|  | 1051 | ALOGV("getOutput() returns new direct output %d", output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1052 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1053 | return output; | 
|  | 1054 | } | 
|  | 1055 |  | 
| Eric Laurent | b732cf5 | 2014-09-24 19:08:21 -0700 | [diff] [blame] | 1056 | non_direct_output: | 
| Eric Laurent | 14cbfca | 2016-03-17 09:42:16 -0700 | [diff] [blame] | 1057 |  | 
|  | 1058 | // A request for HW A/V sync cannot fallback to a mixed output because time | 
|  | 1059 | // stamps are embedded in audio data | 
|  | 1060 | if ((flags & AUDIO_OUTPUT_FLAG_HW_AV_SYNC) != 0) { | 
|  | 1061 | return AUDIO_IO_HANDLE_NONE; | 
|  | 1062 | } | 
|  | 1063 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1064 | // ignoring channel mask due to downmix capability in mixer | 
|  | 1065 |  | 
|  | 1066 | // open a non direct output | 
|  | 1067 |  | 
|  | 1068 | // for non direct outputs, only PCM is supported | 
|  | 1069 | if (audio_is_linear_pcm(format)) { | 
|  | 1070 | // get which output is suitable for the specified stream. The actual | 
|  | 1071 | // routing change will happen when startOutput() will be called | 
|  | 1072 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 1073 |  | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1074 | // at this stage we should ignore the DIRECT flag as no direct output could be found earlier | 
|  | 1075 | flags = (audio_output_flags_t)(flags & ~AUDIO_OUTPUT_FLAG_DIRECT); | 
|  | 1076 | output = selectOutput(outputs, flags, format); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1077 | } | 
|  | 1078 | ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d," | 
|  | 1079 | "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags); | 
|  | 1080 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1081 | ALOGV("  getOutputForDevice() returns output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1082 |  | 
|  | 1083 | return output; | 
|  | 1084 | } | 
|  | 1085 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1086 | 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] | 1087 | audio_output_flags_t flags, | 
|  | 1088 | audio_format_t format) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1089 | { | 
|  | 1090 | // select one output among several that provide a path to a particular device or set of | 
|  | 1091 | // devices (the list was previously build by getOutputsForDevice()). | 
|  | 1092 | // The priority is as follows: | 
|  | 1093 | // 1: the output with the highest number of requested policy flags | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1094 | // 2: the output with the bit depth the closest to the requested one | 
|  | 1095 | // 3: the primary output | 
|  | 1096 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1097 |  | 
|  | 1098 | if (outputs.size() == 0) { | 
|  | 1099 | return 0; | 
|  | 1100 | } | 
|  | 1101 | if (outputs.size() == 1) { | 
|  | 1102 | return outputs[0]; | 
|  | 1103 | } | 
|  | 1104 |  | 
|  | 1105 | int maxCommonFlags = 0; | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1106 | audio_io_handle_t outputForFlags = 0; | 
|  | 1107 | audio_io_handle_t outputForPrimary = 0; | 
|  | 1108 | audio_io_handle_t outputForFormat = 0; | 
|  | 1109 | audio_format_t bestFormat = AUDIO_FORMAT_INVALID; | 
|  | 1110 | audio_format_t bestFormatForFlags = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1111 |  | 
|  | 1112 | for (size_t i = 0; i < outputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1113 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1114 | if (!outputDesc->isDuplicated()) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1115 | // if a valid format is specified, skip output if not compatible | 
|  | 1116 | if (format != AUDIO_FORMAT_INVALID) { | 
|  | 1117 | if (outputDesc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1118 | if (!audio_formats_match(format, outputDesc->mFormat)) { | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1119 | continue; | 
|  | 1120 | } | 
|  | 1121 | } else if (!audio_is_linear_pcm(format)) { | 
|  | 1122 | continue; | 
|  | 1123 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1124 | if (AudioPort::isBetterFormatMatch( | 
|  | 1125 | outputDesc->mFormat, bestFormat, format)) { | 
|  | 1126 | outputForFormat = outputs[i]; | 
|  | 1127 | bestFormat = outputDesc->mFormat; | 
|  | 1128 | } | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 1129 | } | 
|  | 1130 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1131 | int commonFlags = popcount(outputDesc->mProfile->getFlags() & flags); | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1132 | if (commonFlags >= maxCommonFlags) { | 
|  | 1133 | if (commonFlags == maxCommonFlags) { | 
|  | 1134 | if (AudioPort::isBetterFormatMatch( | 
|  | 1135 | outputDesc->mFormat, bestFormatForFlags, format)) { | 
|  | 1136 | outputForFlags = outputs[i]; | 
|  | 1137 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1138 | } | 
|  | 1139 | } else { | 
|  | 1140 | outputForFlags = outputs[i]; | 
|  | 1141 | maxCommonFlags = commonFlags; | 
|  | 1142 | bestFormatForFlags = outputDesc->mFormat; | 
|  | 1143 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1144 | ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags); | 
|  | 1145 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 1146 | if (outputDesc->mProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1147 | outputForPrimary = outputs[i]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1148 | } | 
|  | 1149 | } | 
|  | 1150 | } | 
|  | 1151 |  | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1152 | if (outputForFlags != 0) { | 
|  | 1153 | return outputForFlags; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1154 | } | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1155 | if (outputForFormat != 0) { | 
|  | 1156 | return outputForFormat; | 
|  | 1157 | } | 
|  | 1158 | if (outputForPrimary != 0) { | 
|  | 1159 | return outputForPrimary; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1160 | } | 
|  | 1161 |  | 
|  | 1162 | return outputs[0]; | 
|  | 1163 | } | 
|  | 1164 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1165 | status_t AudioPolicyManager::startOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1166 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1167 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1168 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1169 | ALOGV("startOutput() output %d, stream %d, session %d", | 
|  | 1170 | output, stream, session); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1171 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1172 | if (index < 0) { | 
|  | 1173 | ALOGW("startOutput() unknown output %d", output); | 
|  | 1174 | return BAD_VALUE; | 
|  | 1175 | } | 
|  | 1176 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1177 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
|  | 1178 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1179 | // Routing? | 
|  | 1180 | mOutputRoutes.incRouteActivity(session); | 
|  | 1181 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1182 | audio_devices_t newDevice; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1183 | AudioMix *policyMix = NULL; | 
|  | 1184 | const char *address = NULL; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1185 | if (outputDesc->mPolicyMix != NULL) { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1186 | policyMix = outputDesc->mPolicyMix; | 
|  | 1187 | address = policyMix->mDeviceAddress.string(); | 
|  | 1188 | if ((policyMix->mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 1189 | newDevice = policyMix->mDeviceType; | 
|  | 1190 | } else { | 
|  | 1191 | newDevice = AUDIO_DEVICE_OUT_REMOTE_SUBMIX; | 
|  | 1192 | } | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1193 | } else if (mOutputRoutes.hasRouteChanged(session)) { | 
|  | 1194 | newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1195 | checkStrategyRoute(getStrategy(stream), output); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1196 | } else { | 
|  | 1197 | newDevice = AUDIO_DEVICE_NONE; | 
|  | 1198 | } | 
|  | 1199 |  | 
|  | 1200 | uint32_t delayMs = 0; | 
|  | 1201 |  | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1202 | status_t status = startSource(outputDesc, stream, newDevice, address, &delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1203 |  | 
|  | 1204 | if (status != NO_ERROR) { | 
|  | 1205 | mOutputRoutes.decRouteActivity(session); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1206 | return status; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1207 | } | 
|  | 1208 | // Automatically enable the remote submix input when output is started on a re routing mix | 
|  | 1209 | // of type MIX_TYPE_RECORDERS | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1210 | if (audio_is_remote_submix_device(newDevice) && policyMix != NULL && | 
|  | 1211 | policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1212 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1213 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1214 | address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1215 | "remote-submix"); | 
|  | 1216 | } | 
|  | 1217 |  | 
|  | 1218 | if (delayMs != 0) { | 
|  | 1219 | usleep(delayMs * 1000); | 
|  | 1220 | } | 
|  | 1221 |  | 
|  | 1222 | return status; | 
|  | 1223 | } | 
|  | 1224 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1225 | status_t AudioPolicyManager::startSource(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1226 | audio_stream_type_t stream, | 
|  | 1227 | audio_devices_t device, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 1228 | const char *address, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1229 | uint32_t *delayMs) | 
|  | 1230 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1231 | // cannot start playback of STREAM_TTS if any other output is being used | 
|  | 1232 | uint32_t beaconMuteLatency = 0; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1233 |  | 
|  | 1234 | *delayMs = 0; | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1235 | if (stream == AUDIO_STREAM_TTS) { | 
|  | 1236 | ALOGV("\t found BEACON stream"); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 1237 | if (!mTtsOutputAvailable && mOutputs.isAnyOutputActive(AUDIO_STREAM_TTS /*streamToIgnore*/)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1238 | return INVALID_OPERATION; | 
|  | 1239 | } else { | 
|  | 1240 | beaconMuteLatency = handleEventForBeacon(STARTING_BEACON); | 
|  | 1241 | } | 
|  | 1242 | } else { | 
|  | 1243 | // some playback other than beacon starts | 
|  | 1244 | beaconMuteLatency = handleEventForBeacon(STARTING_OUTPUT); | 
|  | 1245 | } | 
|  | 1246 |  | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1247 | // force device change if the output is inactive and no audio patch is already present. | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1248 | // check active before incrementing usage count | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1249 | bool force = !outputDesc->isActive() && | 
|  | 1250 | (outputDesc->getPatchHandle() == AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 1251 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1252 | // increment usage count for this stream on the requested output: | 
|  | 1253 | // NOTE that the usage count is the same for duplicated output and hardware output which is | 
|  | 1254 | // necessary for a correct control of hardware output routing by startOutput() and stopOutput() | 
|  | 1255 | outputDesc->changeRefCount(stream, 1); | 
|  | 1256 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1257 | if (stream == AUDIO_STREAM_MUSIC) { | 
|  | 1258 | selectOutputForMusicEffects(); | 
|  | 1259 | } | 
|  | 1260 |  | 
| Eric Laurent | 493404d | 2015-04-21 15:07:36 -0700 | [diff] [blame] | 1261 | if (outputDesc->mRefCount[stream] == 1 || device != AUDIO_DEVICE_NONE) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1262 | // starting an output being rerouted? | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1263 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 1264 | device = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1265 | } | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1266 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1267 | routing_strategy strategy = getStrategy(stream); | 
|  | 1268 | bool shouldWait = (strategy == STRATEGY_SONIFICATION) || | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1269 | (strategy == STRATEGY_SONIFICATION_RESPECTFUL) || | 
|  | 1270 | (beaconMuteLatency > 0); | 
|  | 1271 | uint32_t waitMs = beaconMuteLatency; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1272 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1273 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1274 | if (desc != outputDesc) { | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1275 | // force a device change if any other output is: | 
|  | 1276 | // - managed by the same hw module | 
|  | 1277 | // - has a current device selection that differs from selected device. | 
|  | 1278 | // - supports currently selected device | 
|  | 1279 | // - has an active audio patch | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1280 | // In this case, the audio HAL must receive the new device selection so that it can | 
|  | 1281 | // change the device currently selected by the other active output. | 
|  | 1282 | if (outputDesc->sharesHwModuleWith(desc) && | 
| Eric Laurent | 77305a6 | 2016-07-25 16:39:22 -0700 | [diff] [blame] | 1283 | desc->device() != device && | 
|  | 1284 | desc->supportedDevices() & device && | 
|  | 1285 | desc->getPatchHandle() != AUDIO_PATCH_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1286 | force = true; | 
|  | 1287 | } | 
|  | 1288 | // 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] | 1289 | // a notification so that audio focus effect can propagate, or that a mute/unmute | 
|  | 1290 | // event occurred for beacon | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1291 | uint32_t latency = desc->latency(); | 
|  | 1292 | if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) { | 
|  | 1293 | waitMs = latency; | 
|  | 1294 | } | 
|  | 1295 | } | 
|  | 1296 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1297 | uint32_t muteWaitMs = setOutputDevice(outputDesc, device, force, 0, NULL, address); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1298 |  | 
|  | 1299 | // handle special case for sonification while in call | 
|  | 1300 | if (isInCall()) { | 
|  | 1301 | handleIncallSonification(stream, true, false); | 
|  | 1302 | } | 
|  | 1303 |  | 
|  | 1304 | // apply volume rules for current stream and device if necessary | 
|  | 1305 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 1306 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1307 | outputDesc, | 
|  | 1308 | device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1309 |  | 
|  | 1310 | // update the outputs if starting an output with a stream that can affect notification | 
|  | 1311 | // routing | 
|  | 1312 | handleNotificationRoutingForStream(stream); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1313 |  | 
| Eric Laurent | 2cbe89a | 2014-12-19 11:49:08 -0800 | [diff] [blame] | 1314 | // force reevaluating accessibility routing when ringtone or alarm starts | 
|  | 1315 | if (strategy == STRATEGY_SONIFICATION) { | 
|  | 1316 | mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY); | 
|  | 1317 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1318 |  | 
|  | 1319 | if (waitMs > muteWaitMs) { | 
|  | 1320 | *delayMs = waitMs - muteWaitMs; | 
|  | 1321 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1322 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 1323 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1324 | return NO_ERROR; | 
|  | 1325 | } | 
|  | 1326 |  | 
|  | 1327 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 1328 | status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1329 | audio_stream_type_t stream, | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1330 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1331 | { | 
|  | 1332 | ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session); | 
|  | 1333 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1334 | if (index < 0) { | 
|  | 1335 | ALOGW("stopOutput() unknown output %d", output); | 
|  | 1336 | return BAD_VALUE; | 
|  | 1337 | } | 
|  | 1338 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1339 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1340 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1341 | if (outputDesc->mRefCount[stream] == 1) { | 
|  | 1342 | // Automatically disable the remote submix input when output is stopped on a | 
|  | 1343 | // re routing mix of type MIX_TYPE_RECORDERS | 
|  | 1344 | if (audio_is_remote_submix_device(outputDesc->mDevice) && | 
|  | 1345 | outputDesc->mPolicyMix != NULL && | 
|  | 1346 | outputDesc->mPolicyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1347 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 1348 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1349 | outputDesc->mPolicyMix->mDeviceAddress, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1350 | "remote-submix"); | 
|  | 1351 | } | 
|  | 1352 | } | 
|  | 1353 |  | 
|  | 1354 | // Routing? | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1355 | bool forceDeviceUpdate = false; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1356 | if (outputDesc->mRefCount[stream] > 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1357 | int activityCount = mOutputRoutes.decRouteActivity(session); | 
|  | 1358 | forceDeviceUpdate = (mOutputRoutes.hasRoute(session) && (activityCount == 0)); | 
|  | 1359 |  | 
|  | 1360 | if (forceDeviceUpdate) { | 
|  | 1361 | checkStrategyRoute(getStrategy(stream), AUDIO_IO_HANDLE_NONE); | 
|  | 1362 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1363 | } | 
|  | 1364 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1365 | return stopSource(outputDesc, stream, forceDeviceUpdate); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1366 | } | 
|  | 1367 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 1368 | status_t AudioPolicyManager::stopSource(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1369 | audio_stream_type_t stream, | 
|  | 1370 | bool forceDeviceUpdate) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1371 | { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 1372 | // always handle stream stop, check which stream type is stopping | 
|  | 1373 | handleEventForBeacon(stream == AUDIO_STREAM_TTS ? STOPPING_BEACON : STOPPING_OUTPUT); | 
|  | 1374 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1375 | // handle special case for sonification while in call | 
|  | 1376 | if (isInCall()) { | 
|  | 1377 | handleIncallSonification(stream, false, false); | 
|  | 1378 | } | 
|  | 1379 |  | 
|  | 1380 | if (outputDesc->mRefCount[stream] > 0) { | 
|  | 1381 | // decrement usage count of this stream on the output | 
|  | 1382 | outputDesc->changeRefCount(stream, -1); | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1383 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1384 | // store time at which the stream was stopped - see isStreamActive() | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1385 | if (outputDesc->mRefCount[stream] == 0 || forceDeviceUpdate) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1386 | outputDesc->mStopTime[stream] = systemTime(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1387 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1388 | // delay the device switch by twice the latency because stopOutput() is executed when | 
|  | 1389 | // the track stop() command is received and at that time the audio track buffer can | 
|  | 1390 | // still contain data that needs to be drained. The latency only covers the audio HAL | 
|  | 1391 | // and kernel buffers. Also the latency does not always include additional delay in the | 
|  | 1392 | // audio path (audio DSP, CODEC ...) | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1393 | setOutputDevice(outputDesc, newDevice, false, outputDesc->latency()*2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1394 |  | 
|  | 1395 | // force restoring the device selection on other active outputs if it differs from the | 
|  | 1396 | // one being selected for this output | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1397 | uint32_t delayMs = outputDesc->latency()*2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1398 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1399 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1400 | if (desc != outputDesc && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1401 | desc->isActive() && | 
|  | 1402 | outputDesc->sharesHwModuleWith(desc) && | 
|  | 1403 | (newDevice != desc->device())) { | 
| Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1404 | audio_devices_t newDevice2 = getNewOutputDevice(desc, false /*fromCache*/); | 
|  | 1405 | bool force = desc->device() != newDevice2; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1406 | setOutputDevice(desc, | 
| Haynes Mathew George | 11c499a | 2016-08-26 12:08:25 -0700 | [diff] [blame] | 1407 | newDevice2, | 
|  | 1408 | force, | 
| Eric Laurent | 57de36c | 2016-09-28 16:59:11 -0700 | [diff] [blame] | 1409 | delayMs); | 
|  | 1410 | // re-apply device specific volume if not done by setOutputDevice() | 
|  | 1411 | if (!force) { | 
|  | 1412 | applyStreamVolumes(desc, newDevice2, delayMs); | 
|  | 1413 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1414 | } | 
|  | 1415 | } | 
|  | 1416 | // update the outputs if stopping one with a stream that can affect notification routing | 
|  | 1417 | handleNotificationRoutingForStream(stream); | 
|  | 1418 | } | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 1419 | if (stream == AUDIO_STREAM_MUSIC) { | 
|  | 1420 | selectOutputForMusicEffects(); | 
|  | 1421 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1422 | return NO_ERROR; | 
|  | 1423 | } else { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1424 | ALOGW("stopOutput() refcount is already 0"); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1425 | return INVALID_OPERATION; | 
|  | 1426 | } | 
|  | 1427 | } | 
|  | 1428 |  | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 1429 | void AudioPolicyManager::releaseOutput(audio_io_handle_t output, | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1430 | audio_stream_type_t stream __unused, | 
|  | 1431 | audio_session_t session __unused) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1432 | { | 
|  | 1433 | ALOGV("releaseOutput() %d", output); | 
|  | 1434 | ssize_t index = mOutputs.indexOfKey(output); | 
|  | 1435 | if (index < 0) { | 
|  | 1436 | ALOGW("releaseOutput() releasing unknown output %d", output); | 
|  | 1437 | return; | 
|  | 1438 | } | 
|  | 1439 |  | 
|  | 1440 | #ifdef AUDIO_POLICY_TEST | 
|  | 1441 | int testIndex = testOutputIndex(output); | 
|  | 1442 | if (testIndex != 0) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1443 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1444 | if (outputDesc->isActive()) { | 
|  | 1445 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1446 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1447 | mTestOutputs[testIndex] = 0; | 
|  | 1448 | } | 
|  | 1449 | return; | 
|  | 1450 | } | 
|  | 1451 | #endif //AUDIO_POLICY_TEST | 
|  | 1452 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 1453 | // Routing | 
|  | 1454 | mOutputRoutes.removeRoute(session); | 
|  | 1455 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 1456 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(index); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 1457 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1458 | if (desc->mDirectOpenCount <= 0) { | 
|  | 1459 | ALOGW("releaseOutput() invalid open count %d for output %d", | 
|  | 1460 | desc->mDirectOpenCount, output); | 
|  | 1461 | return; | 
|  | 1462 | } | 
|  | 1463 | if (--desc->mDirectOpenCount == 0) { | 
|  | 1464 | closeOutput(output); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1465 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1466 | } | 
|  | 1467 | } | 
|  | 1468 | } | 
|  | 1469 |  | 
|  | 1470 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1471 | status_t AudioPolicyManager::getInputForAttr(const audio_attributes_t *attr, | 
|  | 1472 | audio_io_handle_t *input, | 
|  | 1473 | audio_session_t session, | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1474 | uid_t uid, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1475 | const audio_config_base_t *config, | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1476 | audio_input_flags_t flags, | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1477 | audio_port_handle_t selectedDeviceId, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1478 | input_type_t *inputType, | 
|  | 1479 | audio_port_handle_t *portId) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1480 | { | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1481 | ALOGV("getInputForAttr() source %d, samplingRate %d, format %d, channelMask %x," | 
|  | 1482 | "session %d, flags %#x", | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1483 | attr->source, config->sample_rate, config->format, config->channel_mask, session, flags); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1484 |  | 
| Eric Laurent | caf7f48 | 2014-11-25 17:50:47 -0800 | [diff] [blame] | 1485 | *input = AUDIO_IO_HANDLE_NONE; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1486 | *inputType = API_INPUT_INVALID; | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1487 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1488 | audio_devices_t device; | 
|  | 1489 | // handle legacy remote submix case where the address was not always specified | 
|  | 1490 | String8 address = String8(""); | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1491 | audio_source_t inputSource = attr->source; | 
|  | 1492 | audio_source_t halInputSource; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1493 | AudioMix *policyMix = NULL; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1494 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1495 | if (inputSource == AUDIO_SOURCE_DEFAULT) { | 
|  | 1496 | inputSource = AUDIO_SOURCE_MIC; | 
|  | 1497 | } | 
|  | 1498 | halInputSource = inputSource; | 
|  | 1499 |  | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1500 | // TODO: check for existing client for this port ID | 
|  | 1501 | if (*portId == AUDIO_PORT_HANDLE_NONE) { | 
|  | 1502 | *portId = AudioPort::getNextUniqueId(); | 
|  | 1503 | } | 
|  | 1504 |  | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1505 | // Explicit routing? | 
|  | 1506 | sp<DeviceDescriptor> deviceDesc; | 
|  | 1507 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 1508 | if (mAvailableInputDevices[i]->getId() == selectedDeviceId) { | 
|  | 1509 | deviceDesc = mAvailableInputDevices[i]; | 
|  | 1510 | break; | 
|  | 1511 | } | 
|  | 1512 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1513 | mInputRoutes.addRoute(session, SessionRoute::STREAM_TYPE_NA, inputSource, deviceDesc, uid); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1514 |  | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1515 | if (inputSource == AUDIO_SOURCE_REMOTE_SUBMIX && | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1516 | strncmp(attr->tags, "addr=", strlen("addr=")) == 0) { | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 1517 | status_t ret = mPolicyMixes.getInputMixForAttr(*attr, &policyMix); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 1518 | if (ret != NO_ERROR) { | 
|  | 1519 | return ret; | 
|  | 1520 | } | 
|  | 1521 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1522 | device = AUDIO_DEVICE_IN_REMOTE_SUBMIX; | 
|  | 1523 | address = String8(attr->tags + strlen("addr=")); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1524 | } else { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1525 | device = getDeviceAndMixForInputSource(inputSource, &policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1526 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c447ded | 2015-01-06 08:47:05 -0800 | [diff] [blame] | 1527 | ALOGW("getInputForAttr() could not find device for source %d", inputSource); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1528 | return BAD_VALUE; | 
|  | 1529 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1530 | if (policyMix != NULL) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 1531 | address = policyMix->mDeviceAddress; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1532 | if (policyMix->mMixType == MIX_TYPE_RECORDERS) { | 
|  | 1533 | // there is an external policy, but this input is attached to a mix of recorders, | 
|  | 1534 | // meaning it receives audio injected into the framework, so the recorder doesn't | 
|  | 1535 | // know about it and is therefore considered "legacy" | 
|  | 1536 | *inputType = API_INPUT_LEGACY; | 
|  | 1537 | } else { | 
|  | 1538 | // recording a mix of players defined by an external policy, we're rerouting for | 
|  | 1539 | // an external policy | 
|  | 1540 | *inputType = API_INPUT_MIX_EXT_POLICY_REROUTE; | 
|  | 1541 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1542 | } else if (audio_is_remote_submix_device(device)) { | 
|  | 1543 | address = String8("0"); | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1544 | *inputType = API_INPUT_MIX_CAPTURE; | 
| Eric Laurent | 82db269 | 2015-08-07 13:59:42 -0700 | [diff] [blame] | 1545 | } else if (device == AUDIO_DEVICE_IN_TELEPHONY_RX) { | 
|  | 1546 | *inputType = API_INPUT_TELEPHONY_RX; | 
| Jean-Michel Trivi | 97bb33f | 2014-12-12 16:23:43 -0800 | [diff] [blame] | 1547 | } else { | 
|  | 1548 | *inputType = API_INPUT_LEGACY; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1549 | } | 
| Ravi Kumar Alamanda | b367f5b | 2015-08-25 08:21:37 -0700 | [diff] [blame] | 1550 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1551 | } | 
|  | 1552 |  | 
|  | 1553 | *input = getInputForDevice(device, address, session, uid, inputSource, | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1554 | config->sample_rate, config->format, config->channel_mask, flags, | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1555 | policyMix); | 
|  | 1556 | if (*input == AUDIO_IO_HANDLE_NONE) { | 
|  | 1557 | mInputRoutes.removeRoute(session); | 
|  | 1558 | return INVALID_OPERATION; | 
|  | 1559 | } | 
| Eric Laurent | 20b9ef0 | 2016-12-05 11:03:16 -0800 | [diff] [blame] | 1560 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1561 | ALOGV("getInputForAttr() returns input type = %d", *inputType); | 
|  | 1562 | return NO_ERROR; | 
|  | 1563 | } | 
|  | 1564 |  | 
|  | 1565 |  | 
|  | 1566 | audio_io_handle_t AudioPolicyManager::getInputForDevice(audio_devices_t device, | 
|  | 1567 | String8 address, | 
|  | 1568 | audio_session_t session, | 
|  | 1569 | uid_t uid, | 
|  | 1570 | audio_source_t inputSource, | 
|  | 1571 | uint32_t samplingRate, | 
|  | 1572 | audio_format_t format, | 
|  | 1573 | audio_channel_mask_t channelMask, | 
|  | 1574 | audio_input_flags_t flags, | 
|  | 1575 | AudioMix *policyMix) | 
|  | 1576 | { | 
|  | 1577 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
|  | 1578 | audio_source_t halInputSource = inputSource; | 
|  | 1579 | bool isSoundTrigger = false; | 
|  | 1580 |  | 
|  | 1581 | if (inputSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1582 | ssize_t index = mSoundTriggerSessions.indexOfKey(session); | 
|  | 1583 | if (index >= 0) { | 
|  | 1584 | input = mSoundTriggerSessions.valueFor(session); | 
|  | 1585 | isSoundTrigger = true; | 
|  | 1586 | flags = (audio_input_flags_t)(flags | AUDIO_INPUT_FLAG_HW_HOTWORD); | 
|  | 1587 | ALOGV("SoundTrigger capture on session %d input %d", session, input); | 
|  | 1588 | } else { | 
|  | 1589 | halInputSource = AUDIO_SOURCE_VOICE_RECOGNITION; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1590 | } | 
|  | 1591 | } | 
|  | 1592 |  | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1593 | // find a compatible input profile (not necessarily identical in parameters) | 
|  | 1594 | sp<IOProfile> profile; | 
|  | 1595 | // samplingRate and flags may be updated by getInputProfile | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1596 | uint32_t profileSamplingRate = (samplingRate == 0) ? SAMPLE_RATE_HZ_DEFAULT : samplingRate; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1597 | audio_format_t profileFormat = format; | 
|  | 1598 | audio_channel_mask_t profileChannelMask = channelMask; | 
|  | 1599 | audio_input_flags_t profileFlags = flags; | 
|  | 1600 | for (;;) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 1601 | profile = getInputProfile(device, address, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1602 | profileSamplingRate, profileFormat, profileChannelMask, | 
|  | 1603 | profileFlags); | 
|  | 1604 | if (profile != 0) { | 
|  | 1605 | break; // success | 
| Eric Laurent | 0506778 | 2016-06-01 18:27:28 -0700 | [diff] [blame] | 1606 | } else if (profileFlags & AUDIO_INPUT_FLAG_RAW) { | 
|  | 1607 | profileFlags = (audio_input_flags_t) (profileFlags & ~AUDIO_INPUT_FLAG_RAW); // retry | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1608 | } else if (profileFlags != AUDIO_INPUT_FLAG_NONE) { | 
|  | 1609 | profileFlags = AUDIO_INPUT_FLAG_NONE; // retry | 
|  | 1610 | } else { // fail | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1611 | ALOGW("getInputForDevice() could not find profile for device 0x%X," | 
|  | 1612 | "samplingRate %u, format %#x, channelMask 0x%X, flags %#x", | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1613 | device, samplingRate, format, channelMask, flags); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1614 | return input; | 
| Eric Laurent | 5dbe471 | 2014-09-19 19:04:57 -0700 | [diff] [blame] | 1615 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1616 | } | 
| Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 1617 | // Pick input sampling rate if not specified by client | 
|  | 1618 | if (samplingRate == 0) { | 
|  | 1619 | samplingRate = profileSamplingRate; | 
|  | 1620 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1621 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1622 | if (profile->getModuleHandle() == 0) { | 
|  | 1623 | ALOGE("getInputForAttr(): HW module %s not opened", profile->getModuleName()); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1624 | return input; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1625 | } | 
|  | 1626 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1627 | sp<AudioSession> audioSession = new AudioSession(session, | 
|  | 1628 | inputSource, | 
|  | 1629 | format, | 
|  | 1630 | samplingRate, | 
|  | 1631 | channelMask, | 
|  | 1632 | flags, | 
|  | 1633 | uid, | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 1634 | isSoundTrigger, | 
|  | 1635 | policyMix, mpClientInterface); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1636 |  | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1637 | // FIXME: disable concurrent capture until UI is ready | 
|  | 1638 | #if 0 | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1639 | // reuse an open input if possible | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1640 | sp<AudioInputDescriptor> reusedInputDesc; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1641 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 1642 | sp<AudioInputDescriptor> desc = mInputs.valueAt(i); | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1643 | // reuse input if: | 
|  | 1644 | // - it shares the same profile | 
|  | 1645 | //      AND | 
|  | 1646 | // - it is not a reroute submix input | 
|  | 1647 | //      AND | 
|  | 1648 | // - it is: not used for sound trigger | 
|  | 1649 | //                OR | 
|  | 1650 | //          used for sound trigger and all clients use the same session ID | 
|  | 1651 | // | 
|  | 1652 | if ((profile == desc->mProfile) && | 
|  | 1653 | (isSoundTrigger == desc->isSoundTrigger()) && | 
|  | 1654 | !is_virtual_input_device(device)) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1655 |  | 
|  | 1656 | sp<AudioSession> as = desc->getAudioSession(session); | 
|  | 1657 | if (as != 0) { | 
|  | 1658 | // do not allow unmatching properties on same session | 
|  | 1659 | if (as->matches(audioSession)) { | 
|  | 1660 | as->changeOpenCount(1); | 
|  | 1661 | } else { | 
|  | 1662 | ALOGW("getInputForDevice() record with different attributes" | 
|  | 1663 | " exists for session %d", session); | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1664 | continue; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1665 | } | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1666 | } else if (isSoundTrigger) { | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1667 | continue; | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1668 | } | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1669 |  | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1670 | // Reuse the already opened input stream on this profile if: | 
|  | 1671 | // - the new capture source is background OR | 
|  | 1672 | // - the path requested configurations match OR | 
|  | 1673 | // - the new source priority is less than the highest source priority on this input | 
|  | 1674 | // If the input stream cannot be reused, close it before opening a new stream | 
|  | 1675 | // on the same profile for the new client so that the requested path configuration | 
|  | 1676 | // can be selected. | 
|  | 1677 | if (!isConcurrentSource(inputSource) && | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1678 | ((desc->mSamplingRate != samplingRate || | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1679 | desc->mChannelMask != channelMask || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1680 | !audio_formats_match(desc->mFormat, format)) && | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1681 | (source_priority(desc->getHighestPrioritySource(false /*activeOnly*/)) < | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1682 | source_priority(inputSource)))) { | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1683 | reusedInputDesc = desc; | 
|  | 1684 | continue; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1685 | } else { | 
|  | 1686 | desc->addAudioSession(session, audioSession); | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1687 | ALOGV("%s: reusing input %d", __FUNCTION__, mInputs.keyAt(i)); | 
|  | 1688 | return mInputs.keyAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1689 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1690 | } | 
|  | 1691 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1692 |  | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1693 | if (reusedInputDesc != 0) { | 
|  | 1694 | AudioSessionCollection sessions = reusedInputDesc->getAudioSessions(false /*activeOnly*/); | 
|  | 1695 | for (size_t j = 0; j < sessions.size(); j++) { | 
|  | 1696 | audio_session_t currentSession = sessions.keyAt(j); | 
|  | 1697 | stopInput(reusedInputDesc->mIoHandle, currentSession); | 
|  | 1698 | releaseInput(reusedInputDesc->mIoHandle, currentSession); | 
|  | 1699 | } | 
|  | 1700 | } | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1701 | #endif | 
| Eric Laurent | 555530a | 2017-02-07 18:17:24 -0800 | [diff] [blame] | 1702 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1703 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1704 | config.sample_rate = profileSamplingRate; | 
|  | 1705 | config.channel_mask = profileChannelMask; | 
|  | 1706 | config.format = profileFormat; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 1707 |  | 
| Eric Laurent | e301410 | 2017-05-03 11:15:43 -0700 | [diff] [blame] | 1708 | if (address == "") { | 
|  | 1709 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(device); | 
|  | 1710 | //   the inputs vector must be of size 1, but we don't want to crash here | 
|  | 1711 | address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress : String8(""); | 
|  | 1712 | } | 
|  | 1713 |  | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 1714 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1715 | &input, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1716 | &config, | 
|  | 1717 | &device, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 1718 | address, | 
| Eric Laurent | 1c9c2cc | 2014-08-28 19:37:25 -0700 | [diff] [blame] | 1719 | halInputSource, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1720 | profileFlags); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1721 |  | 
|  | 1722 | // only accept input with the exact requested set of parameters | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1723 | if (status != NO_ERROR || input == AUDIO_IO_HANDLE_NONE || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1724 | (profileSamplingRate != config.sample_rate) || | 
| Eric Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 1725 | !audio_formats_match(profileFormat, config.format) || | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1726 | (profileChannelMask != config.channel_mask)) { | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1727 | ALOGW("getInputForAttr() failed opening input: samplingRate %d" | 
|  | 1728 | ", format %d, channelMask %x", | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1729 | samplingRate, format, channelMask); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1730 | if (input != AUDIO_IO_HANDLE_NONE) { | 
|  | 1731 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1732 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1733 | return AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1734 | } | 
|  | 1735 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1736 | sp<AudioInputDescriptor> inputDesc = new AudioInputDescriptor(profile); | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 1737 | inputDesc->mSamplingRate = profileSamplingRate; | 
|  | 1738 | inputDesc->mFormat = profileFormat; | 
|  | 1739 | inputDesc->mChannelMask = profileChannelMask; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 1740 | inputDesc->mDevice = device; | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1741 | inputDesc->mPolicyMix = policyMix; | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1742 | inputDesc->addAudioSession(session, audioSession); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1743 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1744 | addInput(input, inputDesc); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 1745 | mpClientInterface->onAudioPortListUpdate(); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1746 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1747 | return input; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1748 | } | 
|  | 1749 |  | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1750 | //static | 
|  | 1751 | bool AudioPolicyManager::isConcurrentSource(audio_source_t source) | 
|  | 1752 | { | 
|  | 1753 | return (source == AUDIO_SOURCE_HOTWORD) || | 
|  | 1754 | (source == AUDIO_SOURCE_VOICE_RECOGNITION) || | 
|  | 1755 | (source == AUDIO_SOURCE_FM_TUNER); | 
|  | 1756 | } | 
|  | 1757 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1758 | bool AudioPolicyManager::isConcurentCaptureAllowed(const sp<AudioInputDescriptor>& inputDesc, | 
|  | 1759 | const sp<AudioSession>& audioSession) | 
|  | 1760 | { | 
|  | 1761 | // Do not allow capture if an active voice call is using a software patch and | 
|  | 1762 | // the call TX source device is on the same HW module. | 
|  | 1763 | // FIXME: would be better to refine to only inputs whose profile connects to the | 
|  | 1764 | // call TX device but this information is not in the audio patch | 
|  | 1765 | if (mCallTxPatch != 0 && | 
|  | 1766 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
|  | 1767 | return false; | 
|  | 1768 | } | 
|  | 1769 |  | 
|  | 1770 | // starting concurrent capture is enabled if: | 
|  | 1771 | // 1) capturing for re-routing | 
|  | 1772 | // 2) capturing for HOTWORD source | 
|  | 1773 | // 3) capturing for FM TUNER source | 
|  | 1774 | // 3) All other active captures are either for re-routing or HOTWORD | 
|  | 1775 |  | 
|  | 1776 | if (is_virtual_input_device(inputDesc->mDevice) || | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1777 | isConcurrentSource(audioSession->inputSource())) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1778 | return true; | 
|  | 1779 | } | 
|  | 1780 |  | 
|  | 1781 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); | 
|  | 1782 | for (size_t i = 0; i <  activeInputs.size(); i++) { | 
|  | 1783 | sp<AudioInputDescriptor> activeInput = activeInputs[i]; | 
| Eric Laurent | bb94809 | 2017-01-23 18:33:30 -0800 | [diff] [blame] | 1784 | if (!isConcurrentSource(activeInput->inputSource(true)) && | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1785 | !is_virtual_input_device(activeInput->mDevice)) { | 
|  | 1786 | return false; | 
|  | 1787 | } | 
|  | 1788 | } | 
|  | 1789 |  | 
|  | 1790 | return true; | 
|  | 1791 | } | 
|  | 1792 |  | 
|  | 1793 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1794 | status_t AudioPolicyManager::startInput(audio_io_handle_t input, | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1795 | audio_session_t session, | 
|  | 1796 | concurrency_type__mask_t *concurrency) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1797 | { | 
|  | 1798 | ALOGV("startInput() input %d", input); | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1799 | *concurrency = API_INPUT_CONCURRENCY_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1800 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1801 | if (index < 0) { | 
|  | 1802 | ALOGW("startInput() unknown input %d", input); | 
|  | 1803 | return BAD_VALUE; | 
|  | 1804 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1805 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1806 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1807 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
|  | 1808 | if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1809 | ALOGW("startInput() unknown session %d on input %d", session, input); | 
|  | 1810 | return BAD_VALUE; | 
|  | 1811 | } | 
|  | 1812 |  | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1813 | // FIXME: disable concurrent capture until UI is ready | 
|  | 1814 | #if 0 | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1815 | if (!isConcurentCaptureAllowed(inputDesc, audioSession)) { | 
|  | 1816 | ALOGW("startInput(%d) failed: other input already started", input); | 
|  | 1817 | return INVALID_OPERATION; | 
|  | 1818 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1819 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1820 | if (isInCall()) { | 
|  | 1821 | *concurrency |= API_INPUT_CONCURRENCY_CALL; | 
|  | 1822 | } | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1823 | if (mInputs.activeInputsCountOnDevices() != 0) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1824 | *concurrency |= API_INPUT_CONCURRENCY_CAPTURE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1825 | } | 
| Eric Laurent | 74708e7 | 2017-04-07 17:13:42 -0700 | [diff] [blame] | 1826 | #else | 
|  | 1827 | if (!is_virtual_input_device(inputDesc->mDevice)) { | 
|  | 1828 | if (mCallTxPatch != 0 && | 
|  | 1829 | inputDesc->getModuleHandle() == mCallTxPatch->mPatch.sources[0].ext.device.hw_module) { | 
|  | 1830 | ALOGW("startInput(%d) failed: call in progress", input); | 
|  | 1831 | return INVALID_OPERATION; | 
|  | 1832 | } | 
|  | 1833 |  | 
|  | 1834 | Vector< sp<AudioInputDescriptor> > activeInputs = mInputs.getActiveInputs(); | 
|  | 1835 | for (size_t i = 0; i < activeInputs.size(); i++) { | 
|  | 1836 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; | 
|  | 1837 |  | 
|  | 1838 | if (is_virtual_input_device(activeDesc->mDevice)) { | 
|  | 1839 | continue; | 
|  | 1840 | } | 
|  | 1841 |  | 
|  | 1842 | audio_source_t activeSource = activeDesc->inputSource(true); | 
|  | 1843 | if (audioSession->inputSource() == AUDIO_SOURCE_HOTWORD) { | 
|  | 1844 | if (activeSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1845 | if (activeDesc->hasPreemptedSession(session)) { | 
|  | 1846 | ALOGW("startInput(%d) failed for HOTWORD: " | 
|  | 1847 | "other input %d already started for HOTWORD", | 
|  | 1848 | input, activeDesc->mIoHandle); | 
|  | 1849 | return INVALID_OPERATION; | 
|  | 1850 | } | 
|  | 1851 | } else { | 
|  | 1852 | ALOGV("startInput(%d) failed for HOTWORD: other input %d already started", | 
|  | 1853 | input, activeDesc->mIoHandle); | 
|  | 1854 | return INVALID_OPERATION; | 
|  | 1855 | } | 
|  | 1856 | } else { | 
|  | 1857 | if (activeSource != AUDIO_SOURCE_HOTWORD) { | 
|  | 1858 | ALOGW("startInput(%d) failed: other input %d already started", | 
|  | 1859 | input, activeDesc->mIoHandle); | 
|  | 1860 | return INVALID_OPERATION; | 
|  | 1861 | } | 
|  | 1862 | } | 
|  | 1863 | } | 
|  | 1864 |  | 
|  | 1865 | // if capture is allowed, preempt currently active HOTWORD captures | 
|  | 1866 | for (size_t i = 0; i < activeInputs.size(); i++) { | 
|  | 1867 | sp<AudioInputDescriptor> activeDesc = activeInputs[i]; | 
|  | 1868 |  | 
|  | 1869 | if (is_virtual_input_device(activeDesc->mDevice)) { | 
|  | 1870 | continue; | 
|  | 1871 | } | 
|  | 1872 |  | 
|  | 1873 | audio_source_t activeSource = activeDesc->inputSource(true); | 
|  | 1874 | if (activeSource == AUDIO_SOURCE_HOTWORD) { | 
|  | 1875 | AudioSessionCollection activeSessions = | 
|  | 1876 | activeDesc->getAudioSessions(true /*activeOnly*/); | 
|  | 1877 | audio_session_t activeSession = activeSessions.keyAt(0); | 
|  | 1878 | audio_io_handle_t activeHandle = activeDesc->mIoHandle; | 
|  | 1879 | SortedVector<audio_session_t> sessions = activeDesc->getPreemptedSessions(); | 
|  | 1880 | sessions.add(activeSession); | 
|  | 1881 | inputDesc->setPreemptedSessions(sessions); | 
|  | 1882 | stopInput(activeHandle, activeSession); | 
|  | 1883 | releaseInput(activeHandle, activeSession); | 
|  | 1884 | ALOGV("startInput(%d) for HOTWORD preempting HOTWORD input %d", | 
|  | 1885 | input, activeDesc->mIoHandle); | 
|  | 1886 | } | 
|  | 1887 | } | 
|  | 1888 | } | 
|  | 1889 | #endif | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1890 |  | 
| Eric Laurent | 313d1e7 | 2016-01-29 09:56:57 -0800 | [diff] [blame] | 1891 | // increment activity count before calling getNewInputDevice() below as only active sessions | 
|  | 1892 | // are considered for device selection | 
|  | 1893 | audioSession->changeActiveCount(1); | 
|  | 1894 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 1895 | // Routing? | 
|  | 1896 | mInputRoutes.incRouteActivity(session); | 
|  | 1897 |  | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1898 | if (audioSession->activeCount() == 1 || mInputRoutes.hasRouteChanged(session)) { | 
| Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1899 | // indicate active capture to sound trigger service if starting capture from a mic on | 
|  | 1900 | // primary HW module | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1901 | audio_devices_t device = getNewInputDevice(inputDesc); | 
| Eric Laurent | 271a93e | 2016-09-29 14:47:06 -0700 | [diff] [blame] | 1902 | setInputDevice(input, device, true /* force */); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1903 |  | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1904 | if (inputDesc->getAudioSessionCount(true/*activeOnly*/) == 1) { | 
|  | 1905 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1906 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1907 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
|  | 1908 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
|  | 1909 | MIX_STATE_MIXING); | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1910 | } | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1911 |  | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1912 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); | 
|  | 1913 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && | 
| Eric Laurent | 05b345e | 2016-11-18 12:36:27 -0800 | [diff] [blame] | 1914 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 1) { | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1915 | SoundTrigger::setCaptureState(true); | 
|  | 1916 | } | 
|  | 1917 |  | 
|  | 1918 | // automatically enable the remote submix output when input is started if not | 
|  | 1919 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1920 | // For remote submix (a virtual device), we open only one input per capture request. | 
|  | 1921 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1922 | String8 address = String8(""); | 
|  | 1923 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1924 | address = String8("0"); | 
|  | 1925 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
|  | 1926 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
|  | 1927 | } | 
|  | 1928 | if (address != "") { | 
|  | 1929 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1930 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 1931 | address, "remote-submix"); | 
|  | 1932 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 1933 | } | 
| Glenn Kasten | 74a8e25 | 2014-07-24 14:09:55 -0700 | [diff] [blame] | 1934 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1935 | } | 
|  | 1936 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1937 | ALOGV("AudioPolicyManager::startInput() input source = %d", audioSession->inputSource()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1938 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1939 | return NO_ERROR; | 
|  | 1940 | } | 
|  | 1941 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1942 | status_t AudioPolicyManager::stopInput(audio_io_handle_t input, | 
|  | 1943 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1944 | { | 
|  | 1945 | ALOGV("stopInput() input %d", input); | 
|  | 1946 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 1947 | if (index < 0) { | 
|  | 1948 | ALOGW("stopInput() unknown input %d", input); | 
|  | 1949 | return BAD_VALUE; | 
|  | 1950 | } | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 1951 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1952 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1953 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 1954 | if (index < 0) { | 
|  | 1955 | ALOGW("stopInput() unknown session %d on input %d", session, input); | 
|  | 1956 | return BAD_VALUE; | 
|  | 1957 | } | 
|  | 1958 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1959 | if (audioSession->activeCount() == 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 1960 | ALOGW("stopInput() input %d already stopped", input); | 
|  | 1961 | return INVALID_OPERATION; | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 1962 | } | 
|  | 1963 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 1964 | audioSession->changeActiveCount(-1); | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 1965 |  | 
|  | 1966 | // Routing? | 
|  | 1967 | mInputRoutes.decRouteActivity(session); | 
|  | 1968 |  | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1969 | if (audioSession->activeCount() == 0) { | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1970 |  | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1971 | if (inputDesc->isActive()) { | 
|  | 1972 | setInputDevice(input, getNewInputDevice(inputDesc), false /* force */); | 
|  | 1973 | } else { | 
|  | 1974 | // if input maps to a dynamic policy with an activity listener, notify of state change | 
|  | 1975 | if ((inputDesc->mPolicyMix != NULL) | 
|  | 1976 | && ((inputDesc->mPolicyMix->mCbFlags & AudioMix::kCbFlagNotifyActivity) != 0)) { | 
|  | 1977 | mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress, | 
|  | 1978 | MIX_STATE_IDLE); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1979 | } | 
| Jean-Michel Trivi | eb6421d | 2016-03-17 12:32:52 -0700 | [diff] [blame] | 1980 |  | 
|  | 1981 | // automatically disable the remote submix output when input is stopped if not | 
|  | 1982 | // used by a policy mix of type MIX_TYPE_RECORDERS | 
|  | 1983 | if (audio_is_remote_submix_device(inputDesc->mDevice)) { | 
|  | 1984 | String8 address = String8(""); | 
|  | 1985 | if (inputDesc->mPolicyMix == NULL) { | 
|  | 1986 | address = String8("0"); | 
|  | 1987 | } else if (inputDesc->mPolicyMix->mMixType == MIX_TYPE_PLAYERS) { | 
|  | 1988 | address = inputDesc->mPolicyMix->mDeviceAddress; | 
|  | 1989 | } | 
|  | 1990 | if (address != "") { | 
|  | 1991 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 1992 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 1993 | address, "remote-submix"); | 
|  | 1994 | } | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1995 | } | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1996 |  | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 1997 | audio_devices_t device = inputDesc->mDevice; | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 1998 | resetInputDevice(input); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 1999 |  | 
| Eric Laurent | f7c5010 | 2016-09-30 15:19:43 -0700 | [diff] [blame] | 2000 | // indicate inactive capture to sound trigger service if stopping capture from a mic on | 
|  | 2001 | // primary HW module | 
|  | 2002 | audio_devices_t primaryInputDevices = availablePrimaryInputDevices(); | 
|  | 2003 | if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) && | 
|  | 2004 | mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2005 | SoundTrigger::setCaptureState(false); | 
|  | 2006 | } | 
|  | 2007 | inputDesc->clearPreemptedSessions(); | 
| Eric Laurent | 84332aa | 2016-01-28 22:19:18 +0000 | [diff] [blame] | 2008 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2009 | } | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2010 | return NO_ERROR; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2011 | } | 
|  | 2012 |  | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2013 | void AudioPolicyManager::releaseInput(audio_io_handle_t input, | 
|  | 2014 | audio_session_t session) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2015 | { | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 2016 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2017 | ALOGV("releaseInput() %d", input); | 
|  | 2018 | ssize_t index = mInputs.indexOfKey(input); | 
|  | 2019 | if (index < 0) { | 
|  | 2020 | ALOGW("releaseInput() releasing unknown input %d", input); | 
|  | 2021 | return; | 
|  | 2022 | } | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 2023 |  | 
|  | 2024 | // Routing | 
|  | 2025 | mInputRoutes.removeRoute(session); | 
|  | 2026 |  | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2027 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(index); | 
|  | 2028 | ALOG_ASSERT(inputDesc != 0); | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2029 |  | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2030 | sp<AudioSession> audioSession = inputDesc->getAudioSession(session); | 
| vivek mehta | 587b8df | 2017-01-31 14:58:44 -0800 | [diff] [blame] | 2031 | if (audioSession == 0) { | 
| Eric Laurent | 4dc6806 | 2014-07-28 17:26:49 -0700 | [diff] [blame] | 2032 | ALOGW("releaseInput() unknown session %d on input %d", session, input); | 
|  | 2033 | return; | 
|  | 2034 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2035 |  | 
|  | 2036 | if (audioSession->openCount() == 0) { | 
|  | 2037 | ALOGW("releaseInput() invalid open count %d on session %d", | 
|  | 2038 | audioSession->openCount(), session); | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2039 | return; | 
|  | 2040 | } | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2041 |  | 
|  | 2042 | if (audioSession->changeOpenCount(-1) == 0) { | 
|  | 2043 | inputDesc->removeAudioSession(session); | 
|  | 2044 | } | 
|  | 2045 |  | 
| Jean-Michel Trivi | 65bfe91 | 2015-12-04 15:56:47 -0800 | [diff] [blame] | 2046 | if (inputDesc->getOpenRefCount() > 0) { | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2047 | ALOGV("releaseInput() exit > 0"); | 
|  | 2048 | return; | 
|  | 2049 | } | 
|  | 2050 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2051 | closeInput(input); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2052 | mpClientInterface->onAudioPortListUpdate(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2053 | ALOGV("releaseInput() exit"); | 
|  | 2054 | } | 
|  | 2055 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2056 | void AudioPolicyManager::closeAllInputs() { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2057 | bool patchRemoved = false; | 
|  | 2058 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2059 | for(size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2060 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(input_index); | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 2061 | ssize_t patch_index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2062 | if (patch_index >= 0) { | 
|  | 2063 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(patch_index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 2064 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2065 | mAudioPatches.removeItemsAt(patch_index); | 
|  | 2066 | patchRemoved = true; | 
|  | 2067 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2068 | mpClientInterface->closeInput(mInputs.keyAt(input_index)); | 
|  | 2069 | } | 
|  | 2070 | mInputs.clear(); | 
| Chris Thornton | 52785f3 | 2016-02-09 17:28:49 -0800 | [diff] [blame] | 2071 | SoundTrigger::setCaptureState(false); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2072 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 2073 |  | 
|  | 2074 | if (patchRemoved) { | 
|  | 2075 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 2076 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2077 | } | 
|  | 2078 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2079 | void AudioPolicyManager::initStreamVolume(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2080 | int indexMin, | 
|  | 2081 | int indexMax) | 
|  | 2082 | { | 
|  | 2083 | ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2084 | mVolumeCurves->initStreamVolume(stream, indexMin, indexMax); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2085 |  | 
|  | 2086 | // initialize other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2087 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 2088 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2089 | continue; | 
|  | 2090 | } | 
|  | 2091 | mVolumeCurves->initStreamVolume((audio_stream_type_t)curStream, indexMin, indexMax); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2092 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2093 | } | 
|  | 2094 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2095 | status_t AudioPolicyManager::setStreamVolumeIndex(audio_stream_type_t stream, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2096 | int index, | 
|  | 2097 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2098 | { | 
|  | 2099 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2100 | if ((index < mVolumeCurves->getVolumeIndexMin(stream)) || | 
|  | 2101 | (index > mVolumeCurves->getVolumeIndexMax(stream))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2102 | return BAD_VALUE; | 
|  | 2103 | } | 
|  | 2104 | if (!audio_is_output_device(device)) { | 
|  | 2105 | return BAD_VALUE; | 
|  | 2106 | } | 
|  | 2107 |  | 
|  | 2108 | // Force max volume if stream cannot be muted | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2109 | if (!mVolumeCurves->canBeMuted(stream)) index = mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2110 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2111 | ALOGV("setStreamVolumeIndex() stream %d, device %08x, index %d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2112 | stream, device, index); | 
|  | 2113 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2114 | // update other private stream volumes which follow this one | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2115 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 2116 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2117 | continue; | 
|  | 2118 | } | 
|  | 2119 | mVolumeCurves->addCurrentVolumeIndex((audio_stream_type_t)curStream, device, index); | 
|  | 2120 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2121 |  | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2122 | // update volume on all outputs and streams matching the following: | 
|  | 2123 | // - The requested stream (or a stream matching for volume control) is active on the output | 
|  | 2124 | // - The device (or devices) selected by the strategy corresponding to this stream includes | 
|  | 2125 | // the requested device | 
|  | 2126 | // - For non default requested device, currently selected device on the output is either the | 
|  | 2127 | // requested device or one of the devices selected by the strategy | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2128 | // - For default requested device (AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME), apply volume only if | 
|  | 2129 | // no specific device volume value exists for currently selected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2130 | status_t status = NO_ERROR; | 
|  | 2131 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2132 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 2133 | audio_devices_t curDevice = Volume::getDeviceForVolume(desc->device()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2134 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 2135 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2136 | continue; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2137 | } | 
| Eric Laurent | d3926fe | 2016-04-15 18:10:07 -0700 | [diff] [blame] | 2138 | if (!(desc->isStreamActive((audio_stream_type_t)curStream) || | 
|  | 2139 | (isInCall() && (curStream == AUDIO_STREAM_VOICE_CALL)))) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2140 | continue; | 
|  | 2141 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2142 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2143 | audio_devices_t curStreamDevice = getDeviceForStrategy(curStrategy, false /*fromCache*/); | 
|  | 2144 | if ((device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) && | 
|  | 2145 | ((curStreamDevice & device) == 0)) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2146 | continue; | 
|  | 2147 | } | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2148 | bool applyVolume; | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2149 | if (device != AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2150 | curStreamDevice |= device; | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2151 | applyVolume = (curDevice & curStreamDevice) != 0; | 
|  | 2152 | } else { | 
|  | 2153 | applyVolume = !mVolumeCurves->hasVolumeIndexForDevice( | 
|  | 2154 | stream, Volume::getDeviceForVolume(curStreamDevice)); | 
| Eric Laurent | 1fd372e | 2016-04-06 14:23:53 -0700 | [diff] [blame] | 2155 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2156 |  | 
| Eric Laurent | e76f29c | 2016-09-14 17:17:53 -0700 | [diff] [blame] | 2157 | if (applyVolume) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2158 | //FIXME: workaround for truncated touch sounds | 
|  | 2159 | // delayed volume change for system stream to be removed when the problem is | 
|  | 2160 | // handled by system UI | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2161 | status_t volStatus = | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 2162 | checkAndSetVolume((audio_stream_type_t)curStream, index, desc, curDevice, | 
|  | 2163 | (stream == AUDIO_STREAM_SYSTEM) ? TOUCH_SOUND_FIXED_DELAY_MS : 0); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2164 | if (volStatus != NO_ERROR) { | 
|  | 2165 | status = volStatus; | 
|  | 2166 | } | 
|  | 2167 | } | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 2168 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2169 | } | 
|  | 2170 | return status; | 
|  | 2171 | } | 
|  | 2172 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2173 | status_t AudioPolicyManager::getStreamVolumeIndex(audio_stream_type_t stream, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2174 | int *index, | 
|  | 2175 | audio_devices_t device) | 
|  | 2176 | { | 
|  | 2177 | if (index == NULL) { | 
|  | 2178 | return BAD_VALUE; | 
|  | 2179 | } | 
|  | 2180 | if (!audio_is_output_device(device)) { | 
|  | 2181 | return BAD_VALUE; | 
|  | 2182 | } | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2183 | // if device is AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME, return volume for device corresponding to | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2184 | // the strategy the stream belongs to. | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2185 | if (device == AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2186 | device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/); | 
|  | 2187 | } | 
| François Gaffie | dfd7409 | 2015-03-19 12:10:59 +0100 | [diff] [blame] | 2188 | device = Volume::getDeviceForVolume(device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2189 |  | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2190 | *index =  mVolumeCurves->getVolumeIndex(stream, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2191 | ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index); | 
|  | 2192 | return NO_ERROR; | 
|  | 2193 | } | 
|  | 2194 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2195 | audio_io_handle_t AudioPolicyManager::selectOutputForMusicEffects() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2196 | { | 
|  | 2197 | // select one output among several suitable for global effects. | 
|  | 2198 | // The priority is as follows: | 
|  | 2199 | // 1: An offloaded output. If the effect ends up not being offloadable, | 
|  | 2200 | //    AudioFlinger will invalidate the track and the offloaded output | 
|  | 2201 | //    will be closed causing the effect to be moved to a PCM output. | 
|  | 2202 | // 2: A deep buffer output | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2203 | // 3: The primary output | 
|  | 2204 | // 4: the first output in the list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2205 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2206 | routing_strategy strategy = getStrategy(AUDIO_STREAM_MUSIC); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2207 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2208 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2209 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2210 | if (outputs.size() == 0) { | 
|  | 2211 | return AUDIO_IO_HANDLE_NONE; | 
|  | 2212 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2213 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2214 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
|  | 2215 | bool activeOnly = true; | 
|  | 2216 |  | 
|  | 2217 | while (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 2218 | audio_io_handle_t outputOffloaded = AUDIO_IO_HANDLE_NONE; | 
|  | 2219 | audio_io_handle_t outputDeepBuffer = AUDIO_IO_HANDLE_NONE; | 
|  | 2220 | audio_io_handle_t outputPrimary = AUDIO_IO_HANDLE_NONE; | 
|  | 2221 |  | 
|  | 2222 | for (size_t i = 0; i < outputs.size(); i++) { | 
|  | 2223 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(outputs[i]); | 
|  | 2224 | if (activeOnly && !desc->isStreamActive(AUDIO_STREAM_MUSIC)) { | 
|  | 2225 | continue; | 
|  | 2226 | } | 
|  | 2227 | ALOGV("selectOutputForMusicEffects activeOnly %d outputs[%zu] flags 0x%08x", | 
|  | 2228 | activeOnly, i, desc->mFlags); | 
|  | 2229 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) { | 
|  | 2230 | outputOffloaded = outputs[i]; | 
|  | 2231 | } | 
|  | 2232 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) { | 
|  | 2233 | outputDeepBuffer = outputs[i]; | 
|  | 2234 | } | 
|  | 2235 | if ((desc->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) != 0) { | 
|  | 2236 | outputPrimary = outputs[i]; | 
|  | 2237 | } | 
|  | 2238 | } | 
|  | 2239 | if (outputOffloaded != AUDIO_IO_HANDLE_NONE) { | 
|  | 2240 | output = outputOffloaded; | 
|  | 2241 | } else if (outputDeepBuffer != AUDIO_IO_HANDLE_NONE) { | 
|  | 2242 | output = outputDeepBuffer; | 
|  | 2243 | } else if (outputPrimary != AUDIO_IO_HANDLE_NONE) { | 
|  | 2244 | output = outputPrimary; | 
|  | 2245 | } else { | 
|  | 2246 | output = outputs[0]; | 
|  | 2247 | } | 
|  | 2248 | activeOnly = false; | 
|  | 2249 | } | 
|  | 2250 |  | 
|  | 2251 | if (output != mMusicEffectOutput) { | 
|  | 2252 | mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mMusicEffectOutput, output); | 
|  | 2253 | mMusicEffectOutput = output; | 
|  | 2254 | } | 
|  | 2255 |  | 
|  | 2256 | ALOGV("selectOutputForMusicEffects selected output %d", output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2257 | return output; | 
|  | 2258 | } | 
|  | 2259 |  | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 2260 | audio_io_handle_t AudioPolicyManager::getOutputForEffect(const effect_descriptor_t *desc __unused) | 
|  | 2261 | { | 
|  | 2262 | return selectOutputForMusicEffects(); | 
|  | 2263 | } | 
|  | 2264 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2265 | status_t AudioPolicyManager::registerEffect(const effect_descriptor_t *desc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2266 | audio_io_handle_t io, | 
|  | 2267 | uint32_t strategy, | 
|  | 2268 | int session, | 
|  | 2269 | int id) | 
|  | 2270 | { | 
|  | 2271 | ssize_t index = mOutputs.indexOfKey(io); | 
|  | 2272 | if (index < 0) { | 
|  | 2273 | index = mInputs.indexOfKey(io); | 
|  | 2274 | if (index < 0) { | 
|  | 2275 | ALOGW("registerEffect() unknown io %d", io); | 
|  | 2276 | return INVALID_OPERATION; | 
|  | 2277 | } | 
|  | 2278 | } | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2279 | return mEffects.registerEffect(desc, io, strategy, session, id); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2280 | } | 
|  | 2281 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2282 | bool AudioPolicyManager::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 2283 | { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2284 | bool active = false; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 2285 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT && !active; curStream++) { | 
|  | 2286 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2287 | continue; | 
|  | 2288 | } | 
|  | 2289 | active = mOutputs.isStreamActive((audio_stream_type_t)curStream, inPastMs); | 
|  | 2290 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 2291 | return active; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2292 | } | 
|  | 2293 |  | 
|  | 2294 | bool AudioPolicyManager::isStreamActiveRemotely(audio_stream_type_t stream, uint32_t inPastMs) const | 
|  | 2295 | { | 
|  | 2296 | return mOutputs.isStreamActiveRemotely(stream, inPastMs); | 
|  | 2297 | } | 
|  | 2298 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2299 | bool AudioPolicyManager::isSourceActive(audio_source_t source) const | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2300 | { | 
|  | 2301 | for (size_t i = 0; i < mInputs.size(); i++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 2302 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 2303 | if (inputDescriptor->isSourceActive(source)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2304 | return true; | 
|  | 2305 | } | 
|  | 2306 | } | 
|  | 2307 | return false; | 
|  | 2308 | } | 
|  | 2309 |  | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2310 | // Register a list of custom mixes with their attributes and format. | 
|  | 2311 | // When a mix is registered, corresponding input and output profiles are | 
|  | 2312 | // added to the remote submix hw module. The profile contains only the | 
|  | 2313 | // parameters (sampling rate, format...) specified by the mix. | 
|  | 2314 | // The corresponding input remote submix device is also connected. | 
|  | 2315 | // | 
|  | 2316 | // When a remote submix device is connected, the address is checked to select the | 
|  | 2317 | // appropriate profile and the corresponding input or output stream is opened. | 
|  | 2318 | // | 
|  | 2319 | // When capture starts, getInputForAttr() will: | 
|  | 2320 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2321 | //  - 2 if none found, getDeviceForInputSource() will: | 
|  | 2322 | //     - 2.1 look for a mix matching the attributes source | 
|  | 2323 | //     - 2.2 if none found, default to device selection by policy rules | 
|  | 2324 | // At this time, the corresponding output remote submix device is also connected | 
|  | 2325 | // and active playback use cases can be transferred to this mix if needed when reconnecting | 
|  | 2326 | // after AudioTracks are invalidated | 
|  | 2327 | // | 
|  | 2328 | // When playback starts, getOutputForAttr() will: | 
|  | 2329 | //  - 1 look for a mix matching the address passed in attribtutes tags if any | 
|  | 2330 | //  - 2 if none found, look for a mix matching the attributes usage | 
|  | 2331 | //  - 3 if none found, default to device and output selection by policy rules. | 
|  | 2332 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 2333 | status_t AudioPolicyManager::registerPolicyMixes(const Vector<AudioMix>& mixes) | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2334 | { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2335 | ALOGV("registerPolicyMixes() %zu mix(es)", mixes.size()); | 
|  | 2336 | status_t res = NO_ERROR; | 
|  | 2337 |  | 
|  | 2338 | sp<HwModule> rSubmixModule; | 
|  | 2339 | // examine each mix's route type | 
|  | 2340 | for (size_t i = 0; i < mixes.size(); i++) { | 
|  | 2341 | // we only support MIX_ROUTE_FLAG_LOOP_BACK or MIX_ROUTE_FLAG_RENDER, not the combination | 
|  | 2342 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_ALL) == MIX_ROUTE_FLAG_ALL) { | 
|  | 2343 | res = INVALID_OPERATION; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2344 | break; | 
|  | 2345 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2346 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
|  | 2347 | // Loop back through "remote submix" | 
|  | 2348 | if (rSubmixModule == 0) { | 
|  | 2349 | for (size_t j = 0; i < mHwModules.size(); j++) { | 
|  | 2350 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 | 
|  | 2351 | && mHwModules[j]->mHandle != 0) { | 
|  | 2352 | rSubmixModule = mHwModules[j]; | 
|  | 2353 | break; | 
|  | 2354 | } | 
|  | 2355 | } | 
|  | 2356 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2357 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2358 | ALOGV("registerPolicyMixes() mix %zu of %zu is LOOP_BACK", i, mixes.size()); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2359 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2360 | if (rSubmixModule == 0) { | 
|  | 2361 | ALOGE(" Unable to find audio module for submix, aborting mix %zu registration", i); | 
|  | 2362 | res = INVALID_OPERATION; | 
|  | 2363 | break; | 
|  | 2364 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2365 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2366 | String8 address = mixes[i].mDeviceAddress; | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2367 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2368 | if (mPolicyMixes.registerMix(address, mixes[i], 0 /*output desc*/) != NO_ERROR) { | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2369 | ALOGE(" Error registering mix %zu for address %s", i, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2370 | res = INVALID_OPERATION; | 
|  | 2371 | break; | 
|  | 2372 | } | 
|  | 2373 | audio_config_t outputConfig = mixes[i].mFormat; | 
|  | 2374 | audio_config_t inputConfig = mixes[i].mFormat; | 
|  | 2375 | // NOTE: audio flinger mixer does not support mono output: configure remote submix HAL in | 
|  | 2376 | // stereo and let audio flinger do the channel conversion if needed. | 
|  | 2377 | outputConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO; | 
|  | 2378 | inputConfig.channel_mask = AUDIO_CHANNEL_IN_STEREO; | 
|  | 2379 | rSubmixModule->addOutputProfile(address, &outputConfig, | 
|  | 2380 | AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address); | 
|  | 2381 | rSubmixModule->addInputProfile(address, &inputConfig, | 
|  | 2382 | AUDIO_DEVICE_IN_REMOTE_SUBMIX, address); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2383 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2384 | if (mixes[i].mMixType == MIX_TYPE_PLAYERS) { | 
|  | 2385 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2386 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2387 | address.string(), "remote-submix"); | 
|  | 2388 | } else { | 
|  | 2389 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2390 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE, | 
|  | 2391 | address.string(), "remote-submix"); | 
|  | 2392 | } | 
|  | 2393 | } else if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2394 | String8 address = mixes[i].mDeviceAddress; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2395 | audio_devices_t device = mixes[i].mDeviceType; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2396 | ALOGV(" registerPolicyMixes() mix %zu of %zu is RENDER, dev=0x%X addr=%s", | 
|  | 2397 | i, mixes.size(), device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2398 |  | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2399 | bool foundOutput = false; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2400 | for (size_t j = 0 ; j < mOutputs.size() ; j++) { | 
|  | 2401 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
|  | 2402 | sp<AudioPatch> patch = mAudioPatches.valueFor(desc->getPatchHandle()); | 
|  | 2403 | if ((patch != 0) && (patch->mPatch.num_sinks != 0) | 
|  | 2404 | && (patch->mPatch.sinks[0].type == AUDIO_PORT_TYPE_DEVICE) | 
|  | 2405 | && (patch->mPatch.sinks[0].ext.device.type == device) | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2406 | && (strncmp(patch->mPatch.sinks[0].ext.device.address, address.string(), | 
|  | 2407 | AUDIO_DEVICE_MAX_ADDRESS_LEN) == 0)) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2408 | if (mPolicyMixes.registerMix(address, mixes[i], desc) != NO_ERROR) { | 
|  | 2409 | res = INVALID_OPERATION; | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2410 | } else { | 
|  | 2411 | foundOutput = true; | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2412 | } | 
|  | 2413 | break; | 
|  | 2414 | } | 
|  | 2415 | } | 
|  | 2416 |  | 
|  | 2417 | if (res != NO_ERROR) { | 
|  | 2418 | ALOGE(" Error registering mix %zu for device 0x%X addr %s", | 
| Jean-Michel Trivi | 5ac8cd4 | 2016-03-24 16:35:36 -0700 | [diff] [blame] | 2419 | i, device, address.string()); | 
|  | 2420 | res = INVALID_OPERATION; | 
|  | 2421 | break; | 
|  | 2422 | } else if (!foundOutput) { | 
|  | 2423 | ALOGE(" Output not found for mix %zu for device 0x%X addr %s", | 
|  | 2424 | i, device, address.string()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2425 | res = INVALID_OPERATION; | 
|  | 2426 | break; | 
|  | 2427 | } | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 2428 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2429 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2430 | if (res != NO_ERROR) { | 
|  | 2431 | unregisterPolicyMixes(mixes); | 
|  | 2432 | } | 
|  | 2433 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2434 | } | 
|  | 2435 |  | 
|  | 2436 | status_t AudioPolicyManager::unregisterPolicyMixes(Vector<AudioMix> mixes) | 
|  | 2437 | { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2438 | ALOGV("unregisterPolicyMixes() num mixes %zu", mixes.size()); | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2439 | status_t res = NO_ERROR; | 
|  | 2440 | sp<HwModule> rSubmixModule; | 
|  | 2441 | // examine each mix's route type | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2442 | for (size_t i = 0; i < mixes.size(); i++) { | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2443 | if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_LOOP_BACK) == MIX_ROUTE_FLAG_LOOP_BACK) { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 2444 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2445 | if (rSubmixModule == 0) { | 
|  | 2446 | for (size_t j = 0; i < mHwModules.size(); j++) { | 
|  | 2447 | if (strcmp(AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX, mHwModules[j]->mName) == 0 | 
|  | 2448 | && mHwModules[j]->mHandle != 0) { | 
|  | 2449 | rSubmixModule = mHwModules[j]; | 
|  | 2450 | break; | 
|  | 2451 | } | 
|  | 2452 | } | 
|  | 2453 | } | 
|  | 2454 | if (rSubmixModule == 0) { | 
|  | 2455 | res = INVALID_OPERATION; | 
|  | 2456 | continue; | 
|  | 2457 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2458 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2459 | String8 address = mixes[i].mDeviceAddress; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2460 |  | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2461 | if (mPolicyMixes.unregisterMix(address) != NO_ERROR) { | 
|  | 2462 | res = INVALID_OPERATION; | 
|  | 2463 | continue; | 
|  | 2464 | } | 
|  | 2465 |  | 
|  | 2466 | if (getDeviceConnectionState(AUDIO_DEVICE_IN_REMOTE_SUBMIX, address.string()) == | 
|  | 2467 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2468 | setDeviceConnectionStateInt(AUDIO_DEVICE_IN_REMOTE_SUBMIX, | 
|  | 2469 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2470 | address.string(), "remote-submix"); | 
|  | 2471 | } | 
|  | 2472 | if (getDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, address.string()) == | 
|  | 2473 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE)  { | 
|  | 2474 | setDeviceConnectionStateInt(AUDIO_DEVICE_OUT_REMOTE_SUBMIX, | 
|  | 2475 | AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE, | 
|  | 2476 | address.string(), "remote-submix"); | 
|  | 2477 | } | 
|  | 2478 | rSubmixModule->removeOutputProfile(address); | 
|  | 2479 | rSubmixModule->removeInputProfile(address); | 
|  | 2480 |  | 
|  | 2481 | } if ((mixes[i].mRouteFlags & MIX_ROUTE_FLAG_RENDER) == MIX_ROUTE_FLAG_RENDER) { | 
|  | 2482 | if (mPolicyMixes.unregisterMix(mixes[i].mDeviceAddress) != NO_ERROR) { | 
|  | 2483 | res = INVALID_OPERATION; | 
|  | 2484 | continue; | 
|  | 2485 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2486 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2487 | } | 
| Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 2488 | return res; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2489 | } | 
|  | 2490 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2491 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2492 | status_t AudioPolicyManager::dump(int fd) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2493 | { | 
|  | 2494 | const size_t SIZE = 256; | 
|  | 2495 | char buffer[SIZE]; | 
|  | 2496 | String8 result; | 
|  | 2497 |  | 
|  | 2498 | snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this); | 
|  | 2499 | result.append(buffer); | 
|  | 2500 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 2501 | snprintf(buffer, SIZE, " Primary Output: %d\n", | 
|  | 2502 | hasPrimaryOutput() ? mPrimaryOutput->mIoHandle : AUDIO_IO_HANDLE_NONE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2503 | result.append(buffer); | 
| Mikhail Naganov | 0d6a033 | 2016-04-19 17:12:38 -0700 | [diff] [blame] | 2504 | std::string stateLiteral; | 
|  | 2505 | AudioModeConverter::toString(mEngine->getPhoneState(), stateLiteral); | 
|  | 2506 | snprintf(buffer, SIZE, " Phone state: %s\n", stateLiteral.c_str()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2507 | result.append(buffer); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 2508 | snprintf(buffer, SIZE, " Force use for communications %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2509 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2510 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2511 | snprintf(buffer, SIZE, " Force use for media %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2512 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2513 | snprintf(buffer, SIZE, " Force use for record %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2514 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2515 | snprintf(buffer, SIZE, " Force use for dock %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2516 | result.append(buffer); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2517 | snprintf(buffer, SIZE, " Force use for system %d\n", mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2518 | result.append(buffer); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2519 | snprintf(buffer, SIZE, " Force use for hdmi system audio %d\n", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2520 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO)); | 
| Jungshik Jang | 7b24ee3 | 2014-07-15 19:38:42 +0900 | [diff] [blame] | 2521 | result.append(buffer); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 2522 | snprintf(buffer, SIZE, " Force use for encoded surround output %d\n", | 
|  | 2523 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND)); | 
|  | 2524 | result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2525 | snprintf(buffer, SIZE, " TTS output %s\n", mTtsOutputAvailable ? "available" : "not available"); | 
|  | 2526 | result.append(buffer); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2527 | snprintf(buffer, SIZE, " Master mono: %s\n", mMasterMono ? "on" : "off"); | 
|  | 2528 | result.append(buffer); | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 2529 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 2530 | write(fd, result.string(), result.size()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2531 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 2532 | mAvailableOutputDevices.dump(fd, String8("Available output")); | 
|  | 2533 | mAvailableInputDevices.dump(fd, String8("Available input")); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2534 | mHwModules.dump(fd); | 
|  | 2535 | mOutputs.dump(fd); | 
|  | 2536 | mInputs.dump(fd); | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 2537 | mVolumeCurves->dump(fd); | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2538 | mEffects.dump(fd); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2539 | mAudioPatches.dump(fd); | 
| Mikhail Naganov | 44344b0 | 2016-12-13 11:21:02 -0800 | [diff] [blame] | 2540 | mPolicyMixes.dump(fd); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2541 |  | 
|  | 2542 | return NO_ERROR; | 
|  | 2543 | } | 
|  | 2544 |  | 
|  | 2545 | // This function checks for the parameters which can be offloaded. | 
|  | 2546 | // This can be enhanced depending on the capability of the DSP and policy | 
|  | 2547 | // of the system. | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 2548 | bool AudioPolicyManager::isOffloadSupported(const audio_offload_info_t& offloadInfo) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2549 | { | 
|  | 2550 | ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d," | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 2551 | " BitRate=%u, duration=%" PRId64 " us, has_video=%d", | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2552 | offloadInfo.sample_rate, offloadInfo.channel_mask, | 
|  | 2553 | offloadInfo.format, | 
|  | 2554 | offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us, | 
|  | 2555 | offloadInfo.has_video); | 
|  | 2556 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 2557 | if (mMasterMono) { | 
|  | 2558 | return false; // no offloading if mono is set. | 
|  | 2559 | } | 
|  | 2560 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2561 | // Check if offload has been disabled | 
|  | 2562 | char propValue[PROPERTY_VALUE_MAX]; | 
|  | 2563 | if (property_get("audio.offload.disable", propValue, "0")) { | 
|  | 2564 | if (atoi(propValue) != 0) { | 
|  | 2565 | ALOGV("offload disabled by audio.offload.disable=%s", propValue ); | 
|  | 2566 | return false; | 
|  | 2567 | } | 
|  | 2568 | } | 
|  | 2569 |  | 
|  | 2570 | // Check if stream type is music, then only allow offload as of now. | 
|  | 2571 | if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC) | 
|  | 2572 | { | 
|  | 2573 | ALOGV("isOffloadSupported: stream_type != MUSIC, returning false"); | 
|  | 2574 | return false; | 
|  | 2575 | } | 
|  | 2576 |  | 
|  | 2577 | //TODO: enable audio offloading with video when ready | 
| Andy Hung | 08945c4 | 2015-05-31 21:36:46 -0700 | [diff] [blame] | 2578 | const bool allowOffloadWithVideo = | 
|  | 2579 | property_get_bool("audio.offload.video", false /* default_value */); | 
|  | 2580 | if (offloadInfo.has_video && !allowOffloadWithVideo) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2581 | ALOGV("isOffloadSupported: has_video == true, returning false"); | 
|  | 2582 | return false; | 
|  | 2583 | } | 
|  | 2584 |  | 
|  | 2585 | //If duration is less than minimum value defined in property, return false | 
|  | 2586 | if (property_get("audio.offload.min.duration.secs", propValue, NULL)) { | 
|  | 2587 | if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) { | 
|  | 2588 | ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue); | 
|  | 2589 | return false; | 
|  | 2590 | } | 
|  | 2591 | } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) { | 
|  | 2592 | ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS); | 
|  | 2593 | return false; | 
|  | 2594 | } | 
|  | 2595 |  | 
|  | 2596 | // Do not allow offloading if one non offloadable effect is enabled. This prevents from | 
|  | 2597 | // creating an offloaded track and tearing it down immediately after start when audioflinger | 
|  | 2598 | // detects there is an active non offloadable effect. | 
|  | 2599 | // FIXME: We should check the audio session here but we do not have it in this context. | 
|  | 2600 | // This may prevent offloading in rare situations where effects are left active by apps | 
|  | 2601 | // in the background. | 
| François Gaffie | 45ed3b0 | 2015-03-19 10:35:14 +0100 | [diff] [blame] | 2602 | if (mEffects.isNonOffloadableEffectEnabled()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2603 | return false; | 
|  | 2604 | } | 
|  | 2605 |  | 
|  | 2606 | // See if there is a profile to support this. | 
|  | 2607 | // AUDIO_DEVICE_NONE | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2608 | sp<IOProfile> profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2609 | offloadInfo.sample_rate, | 
|  | 2610 | offloadInfo.format, | 
|  | 2611 | offloadInfo.channel_mask, | 
|  | 2612 | AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 2613 | ALOGV("isOffloadSupported() profile %sfound", profile != 0 ? "" : "NOT "); | 
|  | 2614 | return (profile != 0); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 2615 | } | 
|  | 2616 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2617 | status_t AudioPolicyManager::listAudioPorts(audio_port_role_t role, | 
|  | 2618 | audio_port_type_t type, | 
|  | 2619 | unsigned int *num_ports, | 
|  | 2620 | struct audio_port *ports, | 
|  | 2621 | unsigned int *generation) | 
|  | 2622 | { | 
|  | 2623 | if (num_ports == NULL || (*num_ports != 0 && ports == NULL) || | 
|  | 2624 | generation == NULL) { | 
|  | 2625 | return BAD_VALUE; | 
|  | 2626 | } | 
|  | 2627 | ALOGV("listAudioPorts() role %d type %d num_ports %d ports %p", role, type, *num_ports, ports); | 
|  | 2628 | if (ports == NULL) { | 
|  | 2629 | *num_ports = 0; | 
|  | 2630 | } | 
|  | 2631 |  | 
|  | 2632 | size_t portsWritten = 0; | 
|  | 2633 | size_t portsMax = *num_ports; | 
|  | 2634 | *num_ports = 0; | 
|  | 2635 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2636 | // do not report devices with type AUDIO_DEVICE_IN_STUB or AUDIO_DEVICE_OUT_STUB | 
|  | 2637 | // as they are used by stub HALs by convention | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2638 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2639 | for (size_t i = 0; i < mAvailableOutputDevices.size(); i++) { | 
|  | 2640 | if (mAvailableOutputDevices[i]->type() == AUDIO_DEVICE_OUT_STUB) { | 
|  | 2641 | continue; | 
|  | 2642 | } | 
|  | 2643 | if (portsWritten < portsMax) { | 
|  | 2644 | mAvailableOutputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2645 | } | 
|  | 2646 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2647 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2648 | } | 
|  | 2649 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 2650 | for (size_t i = 0; i < mAvailableInputDevices.size(); i++) { | 
|  | 2651 | if (mAvailableInputDevices[i]->type() == AUDIO_DEVICE_IN_STUB) { | 
|  | 2652 | continue; | 
|  | 2653 | } | 
|  | 2654 | if (portsWritten < portsMax) { | 
|  | 2655 | mAvailableInputDevices[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2656 | } | 
|  | 2657 | (*num_ports)++; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2658 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2659 | } | 
|  | 2660 | } | 
|  | 2661 | if (type == AUDIO_PORT_TYPE_NONE || type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2662 | if (role == AUDIO_PORT_ROLE_SINK || role == AUDIO_PORT_ROLE_NONE) { | 
|  | 2663 | for (size_t i = 0; i < mInputs.size() && portsWritten < portsMax; i++) { | 
|  | 2664 | mInputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2665 | } | 
|  | 2666 | *num_ports += mInputs.size(); | 
|  | 2667 | } | 
|  | 2668 | if (role == AUDIO_PORT_ROLE_SOURCE || role == AUDIO_PORT_ROLE_NONE) { | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2669 | size_t numOutputs = 0; | 
|  | 2670 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 2671 | if (!mOutputs[i]->isDuplicated()) { | 
|  | 2672 | numOutputs++; | 
|  | 2673 | if (portsWritten < portsMax) { | 
|  | 2674 | mOutputs[i]->toAudioPort(&ports[portsWritten++]); | 
|  | 2675 | } | 
|  | 2676 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2677 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2678 | *num_ports += numOutputs; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2679 | } | 
|  | 2680 | } | 
|  | 2681 | *generation = curAudioPortGeneration(); | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 2682 | ALOGV("listAudioPorts() got %zu ports needed %d", portsWritten, *num_ports); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2683 | return NO_ERROR; | 
|  | 2684 | } | 
|  | 2685 |  | 
|  | 2686 | status_t AudioPolicyManager::getAudioPort(struct audio_port *port __unused) | 
|  | 2687 | { | 
|  | 2688 | return NO_ERROR; | 
|  | 2689 | } | 
|  | 2690 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2691 | status_t AudioPolicyManager::createAudioPatch(const struct audio_patch *patch, | 
|  | 2692 | audio_patch_handle_t *handle, | 
|  | 2693 | uid_t uid) | 
|  | 2694 | { | 
|  | 2695 | ALOGV("createAudioPatch()"); | 
|  | 2696 |  | 
|  | 2697 | if (handle == NULL || patch == NULL) { | 
|  | 2698 | return BAD_VALUE; | 
|  | 2699 | } | 
|  | 2700 | ALOGV("createAudioPatch() num sources %d num sinks %d", patch->num_sources, patch->num_sinks); | 
|  | 2701 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2702 | if (patch->num_sources == 0 || patch->num_sources > AUDIO_PATCH_PORTS_MAX || | 
|  | 2703 | patch->num_sinks == 0 || patch->num_sinks > AUDIO_PATCH_PORTS_MAX) { | 
|  | 2704 | return BAD_VALUE; | 
|  | 2705 | } | 
|  | 2706 | // only one source per audio patch supported for now | 
|  | 2707 | if (patch->num_sources > 1) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2708 | return INVALID_OPERATION; | 
|  | 2709 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2710 |  | 
|  | 2711 | if (patch->sources[0].role != AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2712 | return INVALID_OPERATION; | 
|  | 2713 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2714 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2715 | if (patch->sinks[i].role != AUDIO_PORT_ROLE_SINK) { | 
|  | 2716 | return INVALID_OPERATION; | 
|  | 2717 | } | 
|  | 2718 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2719 |  | 
|  | 2720 | sp<AudioPatch> patchDesc; | 
|  | 2721 | ssize_t index = mAudioPatches.indexOfKey(*handle); | 
|  | 2722 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2723 | ALOGV("createAudioPatch source id %d role %d type %d", patch->sources[0].id, | 
|  | 2724 | patch->sources[0].role, | 
|  | 2725 | patch->sources[0].type); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2726 | #if LOG_NDEBUG == 0 | 
|  | 2727 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 2728 | ALOGV("createAudioPatch sink %zu: id %d role %d type %d", i, patch->sinks[i].id, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2729 | patch->sinks[i].role, | 
|  | 2730 | patch->sinks[i].type); | 
|  | 2731 | } | 
|  | 2732 | #endif | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2733 |  | 
|  | 2734 | if (index >= 0) { | 
|  | 2735 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2736 | ALOGV("createAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2737 | mUidCached, patchDesc->mUid, uid); | 
|  | 2738 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2739 | return INVALID_OPERATION; | 
|  | 2740 | } | 
|  | 2741 | } else { | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 2742 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2743 | } | 
|  | 2744 |  | 
|  | 2745 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2746 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2747 | if (outputDesc == NULL) { | 
|  | 2748 | ALOGV("createAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2749 | return BAD_VALUE; | 
|  | 2750 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 2751 | ALOG_ASSERT(!outputDesc->isDuplicated(),"duplicated output %d in source in ports", | 
|  | 2752 | outputDesc->mIoHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2753 | if (patchDesc != 0) { | 
|  | 2754 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
|  | 2755 | ALOGV("createAudioPatch() source id differs for patch current id %d new id %d", | 
|  | 2756 | patchDesc->mPatch.sources[0].id, patch->sources[0].id); | 
|  | 2757 | return BAD_VALUE; | 
|  | 2758 | } | 
|  | 2759 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2760 | DeviceVector devices; | 
|  | 2761 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2762 | // Only support mix to devices connection | 
|  | 2763 | // TODO add support for mix to mix connection | 
|  | 2764 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2765 | ALOGV("createAudioPatch() source mix but sink is not a device"); | 
|  | 2766 | return INVALID_OPERATION; | 
|  | 2767 | } | 
|  | 2768 | sp<DeviceDescriptor> devDesc = | 
|  | 2769 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2770 | if (devDesc == 0) { | 
|  | 2771 | ALOGV("createAudioPatch() out device not found for id %d", patch->sinks[i].id); | 
|  | 2772 | return BAD_VALUE; | 
|  | 2773 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2774 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2775 | if (!outputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2776 | devDesc->mAddress, | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2777 | patch->sources[0].sample_rate, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2778 | NULL,  // updatedSamplingRate | 
|  | 2779 | patch->sources[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2780 | NULL,  // updatedFormat | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2781 | patch->sources[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2782 | NULL,  // updatedChannelMask | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2783 | AUDIO_OUTPUT_FLAG_NONE /*FIXME*/)) { | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2784 | ALOGV("createAudioPatch() profile not supported for device %08x", | 
|  | 2785 | devDesc->type()); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2786 | return INVALID_OPERATION; | 
|  | 2787 | } | 
|  | 2788 | devices.add(devDesc); | 
|  | 2789 | } | 
|  | 2790 | if (devices.size() == 0) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2791 | return INVALID_OPERATION; | 
|  | 2792 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2793 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2794 | // TODO: reconfigure output format and channels here | 
|  | 2795 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2796 | devices.types(), outputDesc->mIoHandle); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2797 | setOutputDevice(outputDesc, devices.types(), true, 0, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2798 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2799 | if (index >= 0) { | 
|  | 2800 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2801 | ALOGW("createAudioPatch() setOutputDevice() did not reuse the patch provided"); | 
|  | 2802 | } | 
|  | 2803 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2804 | patchDesc->mUid = uid; | 
|  | 2805 | ALOGV("createAudioPatch() success"); | 
|  | 2806 | } else { | 
|  | 2807 | ALOGW("createAudioPatch() setOutputDevice() failed to create a patch"); | 
|  | 2808 | return INVALID_OPERATION; | 
|  | 2809 | } | 
|  | 2810 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2811 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
|  | 2812 | // input device to input mix connection | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2813 | // only one sink supported when connecting an input device to a mix | 
|  | 2814 | if (patch->num_sinks > 1) { | 
|  | 2815 | return INVALID_OPERATION; | 
|  | 2816 | } | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2817 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2818 | if (inputDesc == NULL) { | 
|  | 2819 | return BAD_VALUE; | 
|  | 2820 | } | 
|  | 2821 | if (patchDesc != 0) { | 
|  | 2822 | if (patchDesc->mPatch.sinks[0].id != patch->sinks[0].id) { | 
|  | 2823 | return BAD_VALUE; | 
|  | 2824 | } | 
|  | 2825 | } | 
|  | 2826 | sp<DeviceDescriptor> devDesc = | 
|  | 2827 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
|  | 2828 | if (devDesc == 0) { | 
|  | 2829 | return BAD_VALUE; | 
|  | 2830 | } | 
|  | 2831 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2832 | if (!inputDesc->mProfile->isCompatibleProfile(devDesc->type(), | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2833 | devDesc->mAddress, | 
|  | 2834 | patch->sinks[0].sample_rate, | 
|  | 2835 | NULL, /*updatedSampleRate*/ | 
|  | 2836 | patch->sinks[0].format, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2837 | NULL, /*updatedFormat*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2838 | patch->sinks[0].channel_mask, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 2839 | NULL, /*updatedChannelMask*/ | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 2840 | // FIXME for the parameter type, | 
|  | 2841 | // and the NONE | 
|  | 2842 | (audio_output_flags_t) | 
| Glenn Kasten | 6a8ab05 | 2014-07-24 14:08:35 -0700 | [diff] [blame] | 2843 | AUDIO_INPUT_FLAG_NONE)) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2844 | return INVALID_OPERATION; | 
|  | 2845 | } | 
|  | 2846 | // TODO: reconfigure output format and channels here | 
|  | 2847 | ALOGV("createAudioPatch() setting device %08x on output %d", | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2848 | devDesc->type(), inputDesc->mIoHandle); | 
|  | 2849 | setInputDevice(inputDesc->mIoHandle, devDesc->type(), true, handle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2850 | index = mAudioPatches.indexOfKey(*handle); | 
|  | 2851 | if (index >= 0) { | 
|  | 2852 | if (patchDesc != 0 && patchDesc != mAudioPatches.valueAt(index)) { | 
|  | 2853 | ALOGW("createAudioPatch() setInputDevice() did not reuse the patch provided"); | 
|  | 2854 | } | 
|  | 2855 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 2856 | patchDesc->mUid = uid; | 
|  | 2857 | ALOGV("createAudioPatch() success"); | 
|  | 2858 | } else { | 
|  | 2859 | ALOGW("createAudioPatch() setInputDevice() failed to create a patch"); | 
|  | 2860 | return INVALID_OPERATION; | 
|  | 2861 | } | 
|  | 2862 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2863 | // device to device connection | 
|  | 2864 | if (patchDesc != 0) { | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2865 | if (patchDesc->mPatch.sources[0].id != patch->sources[0].id) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2866 | return BAD_VALUE; | 
|  | 2867 | } | 
|  | 2868 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2869 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 2870 | mAvailableInputDevices.getDeviceFromId(patch->sources[0].id); | 
| Eric Laurent | 58f8eb7 | 2014-09-12 16:19:41 -0700 | [diff] [blame] | 2871 | if (srcDeviceDesc == 0) { | 
|  | 2872 | return BAD_VALUE; | 
|  | 2873 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2874 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2875 | //update source and sink with our own data as the data passed in the patch may | 
|  | 2876 | // be incomplete. | 
|  | 2877 | struct audio_patch newPatch = *patch; | 
|  | 2878 | srcDeviceDesc->toAudioPortConfig(&newPatch.sources[0], &patch->sources[0]); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2879 |  | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2880 | for (size_t i = 0; i < patch->num_sinks; i++) { | 
|  | 2881 | if (patch->sinks[i].type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2882 | ALOGV("createAudioPatch() source device but one sink is not a device"); | 
|  | 2883 | return INVALID_OPERATION; | 
|  | 2884 | } | 
|  | 2885 |  | 
|  | 2886 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 2887 | mAvailableOutputDevices.getDeviceFromId(patch->sinks[i].id); | 
|  | 2888 | if (sinkDeviceDesc == 0) { | 
|  | 2889 | return BAD_VALUE; | 
|  | 2890 | } | 
|  | 2891 | sinkDeviceDesc->toAudioPortConfig(&newPatch.sinks[i], &patch->sinks[i]); | 
|  | 2892 |  | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2893 | // create a software bridge in PatchPanel if: | 
|  | 2894 | // - source and sink devices are on differnt HW modules OR | 
|  | 2895 | // - audio HAL version is < 3.0 | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2896 | if (!srcDeviceDesc->hasSameHwModuleAs(sinkDeviceDesc) || | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 2897 | (srcDeviceDesc->mModule->getHalVersionMajor() < 3)) { | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2898 | // support only one sink device for now to simplify output selection logic | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2899 | if (patch->num_sinks > 1) { | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2900 | return INVALID_OPERATION; | 
|  | 2901 | } | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2902 | SortedVector<audio_io_handle_t> outputs = | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2903 | getOutputsForDevice(sinkDeviceDesc->type(), mOutputs); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2904 | // if the sink device is reachable via an opened output stream, request to go via | 
|  | 2905 | // this output stream by adding a second source to the patch description | 
| Eric Laurent | 8838a38 | 2014-09-08 16:44:28 -0700 | [diff] [blame] | 2906 | audio_io_handle_t output = selectOutput(outputs, | 
|  | 2907 | AUDIO_OUTPUT_FLAG_NONE, | 
|  | 2908 | AUDIO_FORMAT_INVALID); | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2909 | if (output != AUDIO_IO_HANDLE_NONE) { | 
|  | 2910 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 2911 | if (outputDesc->isDuplicated()) { | 
|  | 2912 | return INVALID_OPERATION; | 
|  | 2913 | } | 
|  | 2914 | outputDesc->toAudioPortConfig(&newPatch.sources[1], &patch->sources[0]); | 
| Eric Laurent | 3bcf859 | 2015-04-03 12:13:24 -0700 | [diff] [blame] | 2915 | newPatch.sources[1].ext.mix.usecase.stream = AUDIO_STREAM_PATCH; | 
| Eric Laurent | 874c4287 | 2014-08-08 15:13:39 -0700 | [diff] [blame] | 2916 | newPatch.num_sources = 2; | 
|  | 2917 | } | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 2918 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2919 | } | 
|  | 2920 | // TODO: check from routing capabilities in config file and other conflicting patches | 
|  | 2921 |  | 
|  | 2922 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 2923 | if (index >= 0) { | 
|  | 2924 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 2925 | } | 
|  | 2926 |  | 
|  | 2927 | status_t status = mpClientInterface->createAudioPatch(&newPatch, | 
|  | 2928 | &afPatchHandle, | 
|  | 2929 | 0); | 
|  | 2930 | ALOGV("createAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 2931 | status, afPatchHandle); | 
|  | 2932 | if (status == NO_ERROR) { | 
|  | 2933 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 2934 | patchDesc = new AudioPatch(&newPatch, uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2935 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 2936 | } else { | 
|  | 2937 | patchDesc->mPatch = newPatch; | 
|  | 2938 | } | 
|  | 2939 | patchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 2940 | *handle = patchDesc->mHandle; | 
|  | 2941 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 2942 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2943 | } else { | 
|  | 2944 | ALOGW("createAudioPatch() patch panel could not connect device patch, error %d", | 
|  | 2945 | status); | 
|  | 2946 | return INVALID_OPERATION; | 
|  | 2947 | } | 
|  | 2948 | } else { | 
|  | 2949 | return BAD_VALUE; | 
|  | 2950 | } | 
|  | 2951 | } else { | 
|  | 2952 | return BAD_VALUE; | 
|  | 2953 | } | 
|  | 2954 | return NO_ERROR; | 
|  | 2955 | } | 
|  | 2956 |  | 
|  | 2957 | status_t AudioPolicyManager::releaseAudioPatch(audio_patch_handle_t handle, | 
|  | 2958 | uid_t uid) | 
|  | 2959 | { | 
|  | 2960 | ALOGV("releaseAudioPatch() patch %d", handle); | 
|  | 2961 |  | 
|  | 2962 | ssize_t index = mAudioPatches.indexOfKey(handle); | 
|  | 2963 |  | 
|  | 2964 | if (index < 0) { | 
|  | 2965 | return BAD_VALUE; | 
|  | 2966 | } | 
|  | 2967 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 2968 | ALOGV("releaseAudioPatch() mUidCached %d patchDesc->mUid %d uid %d", | 
|  | 2969 | mUidCached, patchDesc->mUid, uid); | 
|  | 2970 | if (patchDesc->mUid != mUidCached && uid != patchDesc->mUid) { | 
|  | 2971 | return INVALID_OPERATION; | 
|  | 2972 | } | 
|  | 2973 |  | 
|  | 2974 | struct audio_patch *patch = &patchDesc->mPatch; | 
|  | 2975 | patchDesc->mUid = mUidCached; | 
|  | 2976 | if (patch->sources[0].type == AUDIO_PORT_TYPE_MIX) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2977 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(patch->sources[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2978 | if (outputDesc == NULL) { | 
|  | 2979 | ALOGV("releaseAudioPatch() output not found for id %d", patch->sources[0].id); | 
|  | 2980 | return BAD_VALUE; | 
|  | 2981 | } | 
|  | 2982 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 2983 | setOutputDevice(outputDesc, | 
|  | 2984 | getNewOutputDevice(outputDesc, true /*fromCache*/), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2985 | true, | 
|  | 2986 | 0, | 
|  | 2987 | NULL); | 
|  | 2988 | } else if (patch->sources[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 2989 | if (patch->sinks[0].type == AUDIO_PORT_TYPE_MIX) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 2990 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(patch->sinks[0].id); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2991 | if (inputDesc == NULL) { | 
|  | 2992 | ALOGV("releaseAudioPatch() input not found for id %d", patch->sinks[0].id); | 
|  | 2993 | return BAD_VALUE; | 
|  | 2994 | } | 
|  | 2995 | setInputDevice(inputDesc->mIoHandle, | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 2996 | getNewInputDevice(inputDesc), | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 2997 | true, | 
|  | 2998 | NULL); | 
|  | 2999 | } else if (patch->sinks[0].type == AUDIO_PORT_TYPE_DEVICE) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3000 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3001 | ALOGV("releaseAudioPatch() patch panel returned %d patchHandle %d", | 
|  | 3002 | status, patchDesc->mAfPatchHandle); | 
|  | 3003 | removeAudioPatch(patchDesc->mHandle); | 
|  | 3004 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 3005 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3006 | } else { | 
|  | 3007 | return BAD_VALUE; | 
|  | 3008 | } | 
|  | 3009 | } else { | 
|  | 3010 | return BAD_VALUE; | 
|  | 3011 | } | 
|  | 3012 | return NO_ERROR; | 
|  | 3013 | } | 
|  | 3014 |  | 
|  | 3015 | status_t AudioPolicyManager::listAudioPatches(unsigned int *num_patches, | 
|  | 3016 | struct audio_patch *patches, | 
|  | 3017 | unsigned int *generation) | 
|  | 3018 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3019 | if (generation == NULL) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3020 | return BAD_VALUE; | 
|  | 3021 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3022 | *generation = curAudioPortGeneration(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3023 | return mAudioPatches.listAudioPatches(num_patches, patches); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3024 | } | 
|  | 3025 |  | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3026 | status_t AudioPolicyManager::setAudioPortConfig(const struct audio_port_config *config) | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3027 | { | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3028 | ALOGV("setAudioPortConfig()"); | 
|  | 3029 |  | 
|  | 3030 | if (config == NULL) { | 
|  | 3031 | return BAD_VALUE; | 
|  | 3032 | } | 
|  | 3033 | ALOGV("setAudioPortConfig() on port handle %d", config->id); | 
|  | 3034 | // Only support gain configuration for now | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3035 | if (config->config_mask != AUDIO_PORT_CONFIG_GAIN) { | 
|  | 3036 | return INVALID_OPERATION; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3037 | } | 
|  | 3038 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3039 | sp<AudioPortConfig> audioPortConfig; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3040 | if (config->type == AUDIO_PORT_TYPE_MIX) { | 
|  | 3041 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3042 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.getOutputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3043 | if (outputDesc == NULL) { | 
|  | 3044 | return BAD_VALUE; | 
|  | 3045 | } | 
| Eric Laurent | 84c7024 | 2014-06-23 08:46:27 -0700 | [diff] [blame] | 3046 | ALOG_ASSERT(!outputDesc->isDuplicated(), | 
|  | 3047 | "setAudioPortConfig() called on duplicated output %d", | 
|  | 3048 | outputDesc->mIoHandle); | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3049 | audioPortConfig = outputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3050 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3051 | sp<AudioInputDescriptor> inputDesc = mInputs.getInputFromId(config->id); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3052 | if (inputDesc == NULL) { | 
|  | 3053 | return BAD_VALUE; | 
|  | 3054 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3055 | audioPortConfig = inputDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3056 | } else { | 
|  | 3057 | return BAD_VALUE; | 
|  | 3058 | } | 
|  | 3059 | } else if (config->type == AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3060 | sp<DeviceDescriptor> deviceDesc; | 
|  | 3061 | if (config->role == AUDIO_PORT_ROLE_SOURCE) { | 
|  | 3062 | deviceDesc = mAvailableInputDevices.getDeviceFromId(config->id); | 
|  | 3063 | } else if (config->role == AUDIO_PORT_ROLE_SINK) { | 
|  | 3064 | deviceDesc = mAvailableOutputDevices.getDeviceFromId(config->id); | 
|  | 3065 | } else { | 
|  | 3066 | return BAD_VALUE; | 
|  | 3067 | } | 
|  | 3068 | if (deviceDesc == NULL) { | 
|  | 3069 | return BAD_VALUE; | 
|  | 3070 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3071 | audioPortConfig = deviceDesc; | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3072 | } else { | 
|  | 3073 | return BAD_VALUE; | 
|  | 3074 | } | 
|  | 3075 |  | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3076 | struct audio_port_config backupConfig; | 
|  | 3077 | status_t status = audioPortConfig->applyAudioPortConfig(config, &backupConfig); | 
|  | 3078 | if (status == NO_ERROR) { | 
|  | 3079 | struct audio_port_config newConfig; | 
|  | 3080 | audioPortConfig->toAudioPortConfig(&newConfig, config); | 
|  | 3081 | status = mpClientInterface->setAudioPortConfig(&newConfig, 0); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3082 | } | 
| Eric Laurent | a121f90 | 2014-06-03 13:32:54 -0700 | [diff] [blame] | 3083 | if (status != NO_ERROR) { | 
|  | 3084 | audioPortConfig->applyAudioPortConfig(&backupConfig); | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3085 | } | 
| Eric Laurent | e1715a4 | 2014-05-20 11:30:42 -0700 | [diff] [blame] | 3086 |  | 
|  | 3087 | return status; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3088 | } | 
|  | 3089 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3090 | void AudioPolicyManager::releaseResourcesForUid(uid_t uid) | 
|  | 3091 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3092 | clearAudioSources(uid); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3093 | clearAudioPatches(uid); | 
|  | 3094 | clearSessionRoutes(uid); | 
|  | 3095 | } | 
|  | 3096 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3097 | void AudioPolicyManager::clearAudioPatches(uid_t uid) | 
|  | 3098 | { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3099 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3100 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 3101 | if (patchDesc->mUid == uid) { | 
| Eric Laurent | 0add0fd | 2014-12-04 18:58:14 -0800 | [diff] [blame] | 3102 | releaseAudioPatch(mAudioPatches.keyAt(i), uid); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3103 | } | 
|  | 3104 | } | 
|  | 3105 | } | 
|  | 3106 |  | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3107 | void AudioPolicyManager::checkStrategyRoute(routing_strategy strategy, | 
|  | 3108 | audio_io_handle_t ouptutToSkip) | 
|  | 3109 | { | 
|  | 3110 | audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 3111 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs); | 
|  | 3112 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
|  | 3113 | if (mOutputs.keyAt(j) == ouptutToSkip) { | 
|  | 3114 | continue; | 
|  | 3115 | } | 
|  | 3116 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(j); | 
|  | 3117 | if (!isStrategyActive(outputDesc, (routing_strategy)strategy)) { | 
|  | 3118 | continue; | 
|  | 3119 | } | 
|  | 3120 | // If the default device for this strategy is on another output mix, | 
|  | 3121 | // invalidate all tracks in this strategy to force re connection. | 
|  | 3122 | // Otherwise select new device on the output mix. | 
|  | 3123 | if (outputs.indexOf(mOutputs.keyAt(j)) < 0) { | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 3124 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3125 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
|  | 3126 | mpClientInterface->invalidateStream((audio_stream_type_t)stream); | 
|  | 3127 | } | 
|  | 3128 | } | 
|  | 3129 | } else { | 
|  | 3130 | audio_devices_t newDevice = getNewOutputDevice(outputDesc, false /*fromCache*/); | 
|  | 3131 | setOutputDevice(outputDesc, newDevice, false); | 
|  | 3132 | } | 
|  | 3133 | } | 
|  | 3134 | } | 
|  | 3135 |  | 
|  | 3136 | void AudioPolicyManager::clearSessionRoutes(uid_t uid) | 
|  | 3137 | { | 
|  | 3138 | // remove output routes associated with this uid | 
|  | 3139 | SortedVector<routing_strategy> affectedStrategies; | 
|  | 3140 | for (ssize_t i = (ssize_t)mOutputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 3141 | sp<SessionRoute> route = mOutputRoutes.valueAt(i); | 
|  | 3142 | if (route->mUid == uid) { | 
|  | 3143 | mOutputRoutes.removeItemsAt(i); | 
|  | 3144 | if (route->mDeviceDescriptor != 0) { | 
|  | 3145 | affectedStrategies.add(getStrategy(route->mStreamType)); | 
|  | 3146 | } | 
|  | 3147 | } | 
|  | 3148 | } | 
|  | 3149 | // reroute outputs if necessary | 
|  | 3150 | for (size_t i = 0; i < affectedStrategies.size(); i++) { | 
|  | 3151 | checkStrategyRoute(affectedStrategies[i], AUDIO_IO_HANDLE_NONE); | 
|  | 3152 | } | 
|  | 3153 |  | 
|  | 3154 | // remove input routes associated with this uid | 
|  | 3155 | SortedVector<audio_source_t> affectedSources; | 
|  | 3156 | for (ssize_t i = (ssize_t)mInputRoutes.size() - 1; i >= 0; i--)  { | 
|  | 3157 | sp<SessionRoute> route = mInputRoutes.valueAt(i); | 
|  | 3158 | if (route->mUid == uid) { | 
|  | 3159 | mInputRoutes.removeItemsAt(i); | 
|  | 3160 | if (route->mDeviceDescriptor != 0) { | 
|  | 3161 | affectedSources.add(route->mSource); | 
|  | 3162 | } | 
|  | 3163 | } | 
|  | 3164 | } | 
|  | 3165 | // reroute inputs if necessary | 
|  | 3166 | SortedVector<audio_io_handle_t> inputsToClose; | 
|  | 3167 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 3168 | sp<AudioInputDescriptor> inputDesc = mInputs.valueAt(i); | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 3169 | if (affectedSources.indexOf(inputDesc->inputSource()) >= 0) { | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3170 | inputsToClose.add(inputDesc->mIoHandle); | 
|  | 3171 | } | 
|  | 3172 | } | 
|  | 3173 | for (size_t i = 0; i < inputsToClose.size(); i++) { | 
|  | 3174 | closeInput(inputsToClose[i]); | 
|  | 3175 | } | 
|  | 3176 | } | 
|  | 3177 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3178 | void AudioPolicyManager::clearAudioSources(uid_t uid) | 
|  | 3179 | { | 
|  | 3180 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 3181 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 3182 | if (sourceDesc->mUid == uid) { | 
|  | 3183 | stopAudioSource(mAudioSources.keyAt(i)); | 
|  | 3184 | } | 
|  | 3185 | } | 
|  | 3186 | } | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 3187 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3188 | status_t AudioPolicyManager::acquireSoundTriggerSession(audio_session_t *session, | 
|  | 3189 | audio_io_handle_t *ioHandle, | 
|  | 3190 | audio_devices_t *device) | 
|  | 3191 | { | 
| Glenn Kasten | f0c6d7d | 2016-02-26 10:44:04 -0800 | [diff] [blame] | 3192 | *session = (audio_session_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_SESSION); | 
|  | 3193 | *ioHandle = (audio_io_handle_t)mpClientInterface->newAudioUniqueId(AUDIO_UNIQUE_ID_USE_INPUT); | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 3194 | *device = getDeviceAndMixForInputSource(AUDIO_SOURCE_HOTWORD); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3195 |  | 
| François Gaffie | df37269 | 2015-03-19 10:43:27 +0100 | [diff] [blame] | 3196 | return mSoundTriggerSessions.acquireSession(*session, *ioHandle); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 3197 | } | 
|  | 3198 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3199 | status_t AudioPolicyManager::startAudioSource(const struct audio_port_config *source, | 
|  | 3200 | const audio_attributes_t *attributes, | 
| Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3201 | audio_patch_handle_t *handle, | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3202 | uid_t uid) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3203 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3204 | ALOGV("%s source %p attributes %p handle %p", __FUNCTION__, source, attributes, handle); | 
|  | 3205 | if (source == NULL || attributes == NULL || handle == NULL) { | 
|  | 3206 | return BAD_VALUE; | 
|  | 3207 | } | 
|  | 3208 |  | 
| Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3209 | *handle = AUDIO_PATCH_HANDLE_NONE; | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3210 |  | 
|  | 3211 | if (source->role != AUDIO_PORT_ROLE_SOURCE || | 
|  | 3212 | source->type != AUDIO_PORT_TYPE_DEVICE) { | 
|  | 3213 | ALOGV("%s INVALID_OPERATION source->role %d source->type %d", __FUNCTION__, source->role, source->type); | 
|  | 3214 | return INVALID_OPERATION; | 
|  | 3215 | } | 
|  | 3216 |  | 
|  | 3217 | sp<DeviceDescriptor> srcDeviceDesc = | 
|  | 3218 | mAvailableInputDevices.getDevice(source->ext.device.type, | 
|  | 3219 | String8(source->ext.device.address)); | 
|  | 3220 | if (srcDeviceDesc == 0) { | 
|  | 3221 | ALOGV("%s source->ext.device.type %08x not found", __FUNCTION__, source->ext.device.type); | 
|  | 3222 | return BAD_VALUE; | 
|  | 3223 | } | 
|  | 3224 | sp<AudioSourceDescriptor> sourceDesc = | 
|  | 3225 | new AudioSourceDescriptor(srcDeviceDesc, attributes, uid); | 
|  | 3226 |  | 
|  | 3227 | struct audio_patch dummyPatch; | 
|  | 3228 | sp<AudioPatch> patchDesc = new AudioPatch(&dummyPatch, uid); | 
|  | 3229 | sourceDesc->mPatchDesc = patchDesc; | 
|  | 3230 |  | 
|  | 3231 | status_t status = connectAudioSource(sourceDesc); | 
|  | 3232 | if (status == NO_ERROR) { | 
|  | 3233 | mAudioSources.add(sourceDesc->getHandle(), sourceDesc); | 
|  | 3234 | *handle = sourceDesc->getHandle(); | 
|  | 3235 | } | 
|  | 3236 | return status; | 
|  | 3237 | } | 
|  | 3238 |  | 
|  | 3239 | status_t AudioPolicyManager::connectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 3240 | { | 
|  | 3241 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 3242 |  | 
|  | 3243 | // make sure we only have one patch per source. | 
|  | 3244 | disconnectAudioSource(sourceDesc); | 
|  | 3245 |  | 
|  | 3246 | routing_strategy strategy = (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3247 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3248 | sp<DeviceDescriptor> srcDeviceDesc = sourceDesc->mDevice; | 
|  | 3249 |  | 
|  | 3250 | audio_devices_t sinkDevice = getDeviceForStrategy(strategy, true); | 
|  | 3251 | sp<DeviceDescriptor> sinkDeviceDesc = | 
|  | 3252 | mAvailableOutputDevices.getDevice(sinkDevice, String8("")); | 
|  | 3253 |  | 
|  | 3254 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 3255 | struct audio_patch *patch = &sourceDesc->mPatchDesc->mPatch; | 
|  | 3256 |  | 
|  | 3257 | if (srcDeviceDesc->getAudioPort()->mModule->getHandle() == | 
|  | 3258 | sinkDeviceDesc->getAudioPort()->mModule->getHandle() && | 
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 3259 | srcDeviceDesc->getAudioPort()->mModule->getHalVersionMajor() >= 3 && | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3260 | srcDeviceDesc->getAudioPort()->mGains.size() > 0) { | 
|  | 3261 | ALOGV("%s AUDIO_DEVICE_API_VERSION_3_0", __FUNCTION__); | 
|  | 3262 | //   create patch between src device and output device | 
|  | 3263 | //   create Hwoutput and add to mHwOutputs | 
|  | 3264 | } else { | 
|  | 3265 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(sinkDevice, mOutputs); | 
|  | 3266 | audio_io_handle_t output = | 
|  | 3267 | selectOutput(outputs, AUDIO_OUTPUT_FLAG_NONE, AUDIO_FORMAT_INVALID); | 
|  | 3268 | if (output == AUDIO_IO_HANDLE_NONE) { | 
|  | 3269 | ALOGV("%s no output for device %08x", __FUNCTION__, sinkDevice); | 
|  | 3270 | return INVALID_OPERATION; | 
|  | 3271 | } | 
|  | 3272 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
|  | 3273 | if (outputDesc->isDuplicated()) { | 
|  | 3274 | ALOGV("%s output for device %08x is duplicated", __FUNCTION__, sinkDevice); | 
|  | 3275 | return INVALID_OPERATION; | 
|  | 3276 | } | 
|  | 3277 | // create a special patch with no sink and two sources: | 
|  | 3278 | // - the second source indicates to PatchPanel through which output mix this patch should | 
|  | 3279 | // be connected as well as the stream type for volume control | 
|  | 3280 | // - the sink is defined by whatever output device is currently selected for the output | 
|  | 3281 | // though which this patch is routed. | 
|  | 3282 | patch->num_sinks = 0; | 
|  | 3283 | patch->num_sources = 2; | 
|  | 3284 | srcDeviceDesc->toAudioPortConfig(&patch->sources[0], NULL); | 
|  | 3285 | outputDesc->toAudioPortConfig(&patch->sources[1], NULL); | 
|  | 3286 | patch->sources[1].ext.mix.usecase.stream = stream; | 
|  | 3287 | status_t status = mpClientInterface->createAudioPatch(patch, | 
|  | 3288 | &afPatchHandle, | 
|  | 3289 | 0); | 
|  | 3290 | ALOGV("%s patch panel returned %d patchHandle %d", __FUNCTION__, | 
|  | 3291 | status, afPatchHandle); | 
|  | 3292 | if (status != NO_ERROR) { | 
|  | 3293 | ALOGW("%s patch panel could not connect device patch, error %d", | 
|  | 3294 | __FUNCTION__, status); | 
|  | 3295 | return INVALID_OPERATION; | 
|  | 3296 | } | 
|  | 3297 | uint32_t delayMs = 0; | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3298 | status = startSource(outputDesc, stream, sinkDevice, NULL, &delayMs); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3299 |  | 
|  | 3300 | if (status != NO_ERROR) { | 
|  | 3301 | mpClientInterface->releaseAudioPatch(sourceDesc->mPatchDesc->mAfPatchHandle, 0); | 
|  | 3302 | return status; | 
|  | 3303 | } | 
|  | 3304 | sourceDesc->mSwOutput = outputDesc; | 
|  | 3305 | if (delayMs != 0) { | 
|  | 3306 | usleep(delayMs * 1000); | 
|  | 3307 | } | 
|  | 3308 | } | 
|  | 3309 |  | 
|  | 3310 | sourceDesc->mPatchDesc->mAfPatchHandle = afPatchHandle; | 
|  | 3311 | addAudioPatch(sourceDesc->mPatchDesc->mHandle, sourceDesc->mPatchDesc); | 
|  | 3312 |  | 
|  | 3313 | return NO_ERROR; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3314 | } | 
|  | 3315 |  | 
| Glenn Kasten | 559d439 | 2016-03-29 13:42:57 -0700 | [diff] [blame] | 3316 | status_t AudioPolicyManager::stopAudioSource(audio_patch_handle_t handle __unused) | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3317 | { | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3318 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueFor(handle); | 
|  | 3319 | ALOGV("%s handle %d", __FUNCTION__, handle); | 
|  | 3320 | if (sourceDesc == 0) { | 
|  | 3321 | ALOGW("%s unknown source for handle %d", __FUNCTION__, handle); | 
|  | 3322 | return BAD_VALUE; | 
|  | 3323 | } | 
|  | 3324 | status_t status = disconnectAudioSource(sourceDesc); | 
|  | 3325 |  | 
|  | 3326 | mAudioSources.removeItem(handle); | 
|  | 3327 | return status; | 
|  | 3328 | } | 
|  | 3329 |  | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3330 | status_t AudioPolicyManager::setMasterMono(bool mono) | 
|  | 3331 | { | 
|  | 3332 | if (mMasterMono == mono) { | 
|  | 3333 | return NO_ERROR; | 
|  | 3334 | } | 
|  | 3335 | mMasterMono = mono; | 
|  | 3336 | // if enabling mono we close all offloaded devices, which will invalidate the | 
|  | 3337 | // corresponding AudioTrack. The AudioTrack client/MediaPlayer is responsible | 
|  | 3338 | // for recreating the new AudioTrack as non-offloaded PCM. | 
|  | 3339 | // | 
|  | 3340 | // If disabling mono, we leave all tracks as is: we don't know which clients | 
|  | 3341 | // and tracks are able to be recreated as offloaded. The next "song" should | 
|  | 3342 | // play back offloaded. | 
|  | 3343 | if (mMasterMono) { | 
|  | 3344 | Vector<audio_io_handle_t> offloaded; | 
|  | 3345 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3346 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
|  | 3347 | if (desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { | 
|  | 3348 | offloaded.push(desc->mIoHandle); | 
|  | 3349 | } | 
|  | 3350 | } | 
|  | 3351 | for (size_t i = 0; i < offloaded.size(); ++i) { | 
|  | 3352 | closeOutput(offloaded[i]); | 
|  | 3353 | } | 
|  | 3354 | } | 
|  | 3355 | // update master mono for all remaining outputs | 
|  | 3356 | for (size_t i = 0; i < mOutputs.size(); ++i) { | 
|  | 3357 | updateMono(mOutputs.keyAt(i)); | 
|  | 3358 | } | 
|  | 3359 | return NO_ERROR; | 
|  | 3360 | } | 
|  | 3361 |  | 
|  | 3362 | status_t AudioPolicyManager::getMasterMono(bool *mono) | 
|  | 3363 | { | 
|  | 3364 | *mono = mMasterMono; | 
|  | 3365 | return NO_ERROR; | 
|  | 3366 | } | 
|  | 3367 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3368 | status_t AudioPolicyManager::disconnectAudioSource(const sp<AudioSourceDescriptor>& sourceDesc) | 
|  | 3369 | { | 
|  | 3370 | ALOGV("%s handle %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 3371 |  | 
|  | 3372 | sp<AudioPatch> patchDesc = mAudioPatches.valueFor(sourceDesc->mPatchDesc->mHandle); | 
|  | 3373 | if (patchDesc == 0) { | 
|  | 3374 | ALOGW("%s source has no patch with handle %d", __FUNCTION__, | 
|  | 3375 | sourceDesc->mPatchDesc->mHandle); | 
|  | 3376 | return BAD_VALUE; | 
|  | 3377 | } | 
|  | 3378 | removeAudioPatch(sourceDesc->mPatchDesc->mHandle); | 
|  | 3379 |  | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 3380 | audio_stream_type_t stream = streamTypefromAttributesInt(&sourceDesc->mAttributes); | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 3381 | sp<SwAudioOutputDescriptor> swOutputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3382 | if (swOutputDesc != 0) { | 
|  | 3383 | stopSource(swOutputDesc, stream, false); | 
|  | 3384 | mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
|  | 3385 | } else { | 
|  | 3386 | sp<HwAudioOutputDescriptor> hwOutputDesc = sourceDesc->mHwOutput.promote(); | 
|  | 3387 | if (hwOutputDesc != 0) { | 
|  | 3388 | //   release patch between src device and output device | 
|  | 3389 | //   close Hwoutput and remove from mHwOutputs | 
|  | 3390 | } else { | 
|  | 3391 | ALOGW("%s source has neither SW nor HW output", __FUNCTION__); | 
|  | 3392 | } | 
|  | 3393 | } | 
|  | 3394 | return NO_ERROR; | 
|  | 3395 | } | 
|  | 3396 |  | 
|  | 3397 | sp<AudioSourceDescriptor> AudioPolicyManager::getSourceForStrategyOnOutput( | 
|  | 3398 | audio_io_handle_t output, routing_strategy strategy) | 
|  | 3399 | { | 
|  | 3400 | sp<AudioSourceDescriptor> source; | 
|  | 3401 | for (size_t i = 0; i < mAudioSources.size(); i++)  { | 
|  | 3402 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 3403 | routing_strategy sourceStrategy = | 
|  | 3404 | (routing_strategy) getStrategyForAttr(&sourceDesc->mAttributes); | 
|  | 3405 | sp<SwAudioOutputDescriptor> outputDesc = sourceDesc->mSwOutput.promote(); | 
|  | 3406 | if (sourceStrategy == strategy && outputDesc != 0 && outputDesc->mIoHandle == output) { | 
|  | 3407 | source = sourceDesc; | 
|  | 3408 | break; | 
|  | 3409 | } | 
|  | 3410 | } | 
|  | 3411 | return source; | 
| Eric Laurent | 554a277 | 2015-04-10 11:29:24 -0700 | [diff] [blame] | 3412 | } | 
|  | 3413 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3414 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3415 | // AudioPolicyManager | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3416 | // ---------------------------------------------------------------------------- | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3417 | uint32_t AudioPolicyManager::nextAudioPortGeneration() | 
|  | 3418 | { | 
|  | 3419 | return android_atomic_inc(&mAudioPortGeneration); | 
|  | 3420 | } | 
|  | 3421 |  | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3422 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
|  | 3423 | // Treblized audio policy xml config will be located in /odm/etc or /vendor/etc. | 
|  | 3424 | static const char *kConfigLocationList[] = | 
|  | 3425 | {"/odm/etc", "/vendor/etc", "/system/etc"}; | 
|  | 3426 | static const int kConfigLocationListSize = | 
|  | 3427 | (sizeof(kConfigLocationList) / sizeof(kConfigLocationList[0])); | 
|  | 3428 |  | 
|  | 3429 | static status_t deserializeAudioPolicyXmlConfig(AudioPolicyConfig &config) { | 
|  | 3430 | char audioPolicyXmlConfigFile[AUDIO_POLICY_XML_CONFIG_FILE_PATH_MAX_LENGTH]; | 
|  | 3431 | status_t ret; | 
|  | 3432 |  | 
|  | 3433 | for (int i = 0; i < kConfigLocationListSize; i++) { | 
|  | 3434 | PolicySerializer serializer; | 
|  | 3435 | snprintf(audioPolicyXmlConfigFile, | 
|  | 3436 | sizeof(audioPolicyXmlConfigFile), | 
|  | 3437 | "%s/%s", | 
|  | 3438 | kConfigLocationList[i], | 
|  | 3439 | AUDIO_POLICY_XML_CONFIG_FILE_NAME); | 
|  | 3440 | ret = serializer.deserialize(audioPolicyXmlConfigFile, config); | 
|  | 3441 | if (ret == NO_ERROR) { | 
|  | 3442 | break; | 
|  | 3443 | } | 
|  | 3444 | } | 
|  | 3445 | return ret; | 
|  | 3446 | } | 
|  | 3447 | #endif | 
|  | 3448 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3449 | AudioPolicyManager::AudioPolicyManager(AudioPolicyClientInterface *clientInterface) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3450 | : | 
|  | 3451 | #ifdef AUDIO_POLICY_TEST | 
|  | 3452 | Thread(false), | 
|  | 3453 | #endif //AUDIO_POLICY_TEST | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3454 | mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f), | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3455 | mA2dpSuspended(false), | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 3456 | mAudioPortGeneration(1), | 
|  | 3457 | mBeaconMuteRefCount(0), | 
|  | 3458 | mBeaconPlayingRefCount(0), | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3459 | mBeaconMuted(false), | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3460 | mTtsOutputAvailable(false), | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3461 | mMasterMono(false), | 
|  | 3462 | mMusicEffectOutput(AUDIO_IO_HANDLE_NONE) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3463 | { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3464 | mUidCached = getuid(); | 
|  | 3465 | mpClientInterface = clientInterface; | 
|  | 3466 |  | 
|  | 3467 | // TODO: remove when legacy conf file is removed. true on devices that use DRC on the | 
|  | 3468 | // DEVICE_CATEGORY_SPEAKER path to boost soft sounds, used to adjust volume curves accordingly. | 
|  | 3469 | // Note: remove also speaker_drc_enabled from global configuration of XML config file. | 
|  | 3470 | bool speakerDrcEnabled = false; | 
|  | 3471 |  | 
|  | 3472 | #ifdef USE_XML_AUDIO_POLICY_CONF | 
|  | 3473 | mVolumeCurves = new VolumeCurvesCollection(); | 
|  | 3474 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3475 | mDefaultOutputDevice, speakerDrcEnabled, | 
|  | 3476 | static_cast<VolumeCurvesCollection *>(mVolumeCurves)); | 
| Jaekyun Seok | 0d4a6af | 2017-02-17 17:10:17 +0900 | [diff] [blame] | 3477 | if (deserializeAudioPolicyXmlConfig(config) != NO_ERROR) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 3478 | #else | 
|  | 3479 | mVolumeCurves = new StreamDescriptorCollection(); | 
|  | 3480 | AudioPolicyConfig config(mHwModules, mAvailableOutputDevices, mAvailableInputDevices, | 
|  | 3481 | mDefaultOutputDevice, speakerDrcEnabled); | 
|  | 3482 | if ((ConfigParsingUtils::loadConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE, config) != NO_ERROR) && | 
|  | 3483 | (ConfigParsingUtils::loadConfig(AUDIO_POLICY_CONFIG_FILE, config) != NO_ERROR)) { | 
|  | 3484 | #endif | 
|  | 3485 | ALOGE("could not load audio policy configuration file, setting defaults"); | 
|  | 3486 | config.setDefault(); | 
|  | 3487 | } | 
|  | 3488 | // must be done after reading the policy (since conditionned by Speaker Drc Enabling) | 
|  | 3489 | mVolumeCurves->initializeVolumeCurves(speakerDrcEnabled); | 
|  | 3490 |  | 
|  | 3491 | // Once policy config has been parsed, retrieve an instance of the engine and initialize it. | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3492 | audio_policy::EngineInstance *engineInstance = audio_policy::EngineInstance::getInstance(); | 
|  | 3493 | if (!engineInstance) { | 
|  | 3494 | ALOGE("%s:  Could not get an instance of policy engine", __FUNCTION__); | 
|  | 3495 | return; | 
|  | 3496 | } | 
|  | 3497 | // Retrieve the Policy Manager Interface | 
|  | 3498 | mEngine = engineInstance->queryInterface<AudioPolicyManagerInterface>(); | 
|  | 3499 | if (mEngine == NULL) { | 
|  | 3500 | ALOGE("%s: Failed to get Policy Engine Interface", __FUNCTION__); | 
|  | 3501 | return; | 
|  | 3502 | } | 
|  | 3503 | mEngine->setObserver(this); | 
|  | 3504 | status_t status = mEngine->initCheck(); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 3505 | (void) status; | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3506 | ALOG_ASSERT(status == NO_ERROR, "Policy engine not initialized(err=%d)", status); | 
|  | 3507 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3508 | // mAvailableOutputDevices and mAvailableInputDevices now contain all attached devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3509 | // open all output streams needed to access attached devices | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3510 | audio_devices_t outputDeviceTypes = mAvailableOutputDevices.types(); | 
|  | 3511 | audio_devices_t inputDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3512 | for (size_t i = 0; i < mHwModules.size(); i++) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3513 | mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3514 | if (mHwModules[i]->mHandle == 0) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3515 | ALOGW("could not open HW module %s", mHwModules[i]->getName()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3516 | continue; | 
|  | 3517 | } | 
|  | 3518 | // open all output streams needed to access attached devices | 
|  | 3519 | // except for direct output streams that are only opened when they are actually | 
|  | 3520 | // required by an app. | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3521 | // This also validates mAvailableOutputDevices list | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3522 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3523 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3524 | const sp<IOProfile> outProfile = mHwModules[i]->mOutputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3525 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3526 | if (!outProfile->hasSupportedDevices()) { | 
|  | 3527 | ALOGW("Output profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3528 | continue; | 
|  | 3529 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3530 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_TTS) != 0) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 3531 | mTtsOutputAvailable = true; | 
|  | 3532 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3533 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3534 | if ((outProfile->getFlags() & AUDIO_OUTPUT_FLAG_DIRECT) != 0) { | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3535 | continue; | 
|  | 3536 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3537 | audio_devices_t profileType = outProfile->getSupportedDevicesType(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3538 | if ((profileType & mDefaultOutputDevice->type()) != AUDIO_DEVICE_NONE) { | 
|  | 3539 | profileType = mDefaultOutputDevice->type(); | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 3540 | } else { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3541 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3542 | // outputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3543 | profileType = outProfile->getSupportedDeviceForType(outputDeviceTypes); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3544 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3545 | if ((profileType & outputDeviceTypes) == 0) { | 
|  | 3546 | continue; | 
|  | 3547 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3548 | sp<SwAudioOutputDescriptor> outputDesc = new SwAudioOutputDescriptor(outProfile, | 
|  | 3549 | mpClientInterface); | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3550 | const DeviceVector &supportedDevices = outProfile->getSupportedDevices(); | 
|  | 3551 | const DeviceVector &devicesForType = supportedDevices.getDevicesFromType(profileType); | 
|  | 3552 | String8 address = devicesForType.size() > 0 ? devicesForType.itemAt(0)->mAddress | 
|  | 3553 | : String8(""); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3554 |  | 
|  | 3555 | outputDesc->mDevice = profileType; | 
|  | 3556 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3557 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 3558 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 3559 | config.format = outputDesc->mFormat; | 
|  | 3560 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3561 | status_t status = mpClientInterface->openOutput(outProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3562 | &output, | 
|  | 3563 | &config, | 
|  | 3564 | &outputDesc->mDevice, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3565 | address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3566 | &outputDesc->mLatency, | 
|  | 3567 | outputDesc->mFlags); | 
|  | 3568 |  | 
|  | 3569 | if (status != NO_ERROR) { | 
|  | 3570 | ALOGW("Cannot open output stream for device %08x on hw module %s", | 
|  | 3571 | outputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3572 | mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3573 | } else { | 
|  | 3574 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 3575 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 3576 | outputDesc->mFormat = config.format; | 
|  | 3577 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3578 | for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
|  | 3579 | ssize_t index = mAvailableOutputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3580 | // 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] | 3581 | if (index >= 0 && !mAvailableOutputDevices[index]->isAttached()) { | 
|  | 3582 | mAvailableOutputDevices[index]->attach(mHwModules[i]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3583 | } | 
|  | 3584 | } | 
|  | 3585 | if (mPrimaryOutput == 0 && | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3586 | outProfile->getFlags() & AUDIO_OUTPUT_FLAG_PRIMARY) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3587 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3588 | } | 
|  | 3589 | addOutput(output, outputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3590 | setOutputDevice(outputDesc, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3591 | outputDesc->mDevice, | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 3592 | true, | 
|  | 3593 | 0, | 
|  | 3594 | NULL, | 
|  | 3595 | address.string()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3596 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3597 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3598 | // open input streams needed to access attached devices to validate | 
|  | 3599 | // mAvailableInputDevices list | 
|  | 3600 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 3601 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3602 | const sp<IOProfile> inProfile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3603 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3604 | if (!inProfile->hasSupportedDevices()) { | 
|  | 3605 | ALOGW("Input profile contains no device on module %s", mHwModules[i]->getName()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3606 | continue; | 
|  | 3607 | } | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3608 | // chose first device present in profile's SupportedDevices also part of | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3609 | // inputDeviceTypes | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3610 | audio_devices_t profileType = inProfile->getSupportedDeviceForType(inputDeviceTypes); | 
|  | 3611 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3612 | if ((profileType & inputDeviceTypes) == 0) { | 
|  | 3613 | continue; | 
|  | 3614 | } | 
| Jean-Michel Trivi | 2f4fe9f | 2015-12-04 16:20:59 -0800 | [diff] [blame] | 3615 | sp<AudioInputDescriptor> inputDesc = | 
|  | 3616 | new AudioInputDescriptor(inProfile); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3617 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3618 | inputDesc->mDevice = profileType; | 
|  | 3619 |  | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3620 | // find the address | 
|  | 3621 | DeviceVector inputDevices = mAvailableInputDevices.getDevicesFromType(profileType); | 
|  | 3622 | //   the inputs vector must be of size 1, but we don't want to crash here | 
|  | 3623 | String8 address = inputDevices.size() > 0 ? inputDevices.itemAt(0)->mAddress | 
|  | 3624 | : String8(""); | 
|  | 3625 | ALOGV("  for input device 0x%x using address %s", profileType, address.string()); | 
|  | 3626 | ALOGE_IF(inputDevices.size() == 0, "Input device list is empty!"); | 
|  | 3627 |  | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3628 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3629 | config.sample_rate = inputDesc->mSamplingRate; | 
|  | 3630 | config.channel_mask = inputDesc->mChannelMask; | 
|  | 3631 | config.format = inputDesc->mFormat; | 
|  | 3632 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 3633 | status_t status = mpClientInterface->openInput(inProfile->getModuleHandle(), | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3634 | &input, | 
|  | 3635 | &config, | 
|  | 3636 | &inputDesc->mDevice, | 
| Jean-Michel Trivi | fd4c148 | 2014-08-06 16:02:28 -0700 | [diff] [blame] | 3637 | address, | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3638 | AUDIO_SOURCE_MIC, | 
|  | 3639 | AUDIO_INPUT_FLAG_NONE); | 
|  | 3640 |  | 
|  | 3641 | if (status == NO_ERROR) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3642 | const DeviceVector &supportedDevices = inProfile->getSupportedDevices(); | 
|  | 3643 | for (size_t k = 0; k  < supportedDevices.size(); k++) { | 
|  | 3644 | ssize_t index =  mAvailableInputDevices.indexOf(supportedDevices[k]); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3645 | // give a valid ID to an attached device once confirmed it is reachable | 
| Eric Laurent | 45aabc3 | 2015-08-06 09:11:13 -0700 | [diff] [blame] | 3646 | if (index >= 0) { | 
|  | 3647 | sp<DeviceDescriptor> devDesc = mAvailableInputDevices[index]; | 
|  | 3648 | if (!devDesc->isAttached()) { | 
|  | 3649 | devDesc->attach(mHwModules[i]); | 
|  | 3650 | devDesc->importAudioPort(inProfile); | 
|  | 3651 | } | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3652 | } | 
|  | 3653 | } | 
|  | 3654 | mpClientInterface->closeInput(input); | 
|  | 3655 | } else { | 
|  | 3656 | ALOGW("Cannot open input stream for device %08x on hw module %s", | 
|  | 3657 | inputDesc->mDevice, | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3658 | mHwModules[i]->getName()); | 
| Eric Laurent | d78f153 | 2014-09-16 16:38:20 -0700 | [diff] [blame] | 3659 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3660 | } | 
|  | 3661 | } | 
|  | 3662 | // make sure all attached devices have been allocated a unique ID | 
|  | 3663 | for (size_t i = 0; i  < mAvailableOutputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3664 | if (!mAvailableOutputDevices[i]->isAttached()) { | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3665 | ALOGW("Output device %08x unreachable", mAvailableOutputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3666 | mAvailableOutputDevices.remove(mAvailableOutputDevices[i]); | 
|  | 3667 | continue; | 
|  | 3668 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3669 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 3670 | mEngine->setDeviceConnectionState(mAvailableOutputDevices[i], | 
|  | 3671 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3672 | i++; | 
|  | 3673 | } | 
|  | 3674 | for (size_t i = 0; i  < mAvailableInputDevices.size();) { | 
| Paul McLean | e743a47 | 2015-01-28 11:07:31 -0800 | [diff] [blame] | 3675 | if (!mAvailableInputDevices[i]->isAttached()) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3676 | ALOGW("Input device %08x unreachable", mAvailableInputDevices[i]->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3677 | mAvailableInputDevices.remove(mAvailableInputDevices[i]); | 
|  | 3678 | continue; | 
|  | 3679 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 3680 | // The device is now validated and can be appended to the available devices of the engine | 
|  | 3681 | mEngine->setDeviceConnectionState(mAvailableInputDevices[i], | 
|  | 3682 | AUDIO_POLICY_DEVICE_STATE_AVAILABLE); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3683 | i++; | 
|  | 3684 | } | 
|  | 3685 | // make sure default device is reachable | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3686 | if (mDefaultOutputDevice == 0 || mAvailableOutputDevices.indexOf(mDefaultOutputDevice) < 0) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3687 | ALOGE("Default device %08x is unreachable", mDefaultOutputDevice->type()); | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3688 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3689 |  | 
|  | 3690 | ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output"); | 
|  | 3691 |  | 
|  | 3692 | updateDevicesAndOutputs(); | 
|  | 3693 |  | 
|  | 3694 | #ifdef AUDIO_POLICY_TEST | 
|  | 3695 | if (mPrimaryOutput != 0) { | 
|  | 3696 | AudioParameter outputCmd = AudioParameter(); | 
|  | 3697 | outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3698 | mpClientInterface->setParameters(mPrimaryOutput->mIoHandle, outputCmd.toString()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3699 |  | 
|  | 3700 | mTestDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 3701 | mTestSamplingRate = 44100; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3702 | mTestFormat = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 3703 | mTestChannels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3704 | mTestLatencyMs = 0; | 
|  | 3705 | mCurOutput = 0; | 
|  | 3706 | mDirectOutput = false; | 
|  | 3707 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 3708 | mTestOutputs[i] = 0; | 
|  | 3709 | } | 
|  | 3710 |  | 
|  | 3711 | const size_t SIZE = 256; | 
|  | 3712 | char buffer[SIZE]; | 
|  | 3713 | snprintf(buffer, SIZE, "AudioPolicyManagerTest"); | 
|  | 3714 | run(buffer, ANDROID_PRIORITY_AUDIO); | 
|  | 3715 | } | 
|  | 3716 | #endif //AUDIO_POLICY_TEST | 
|  | 3717 | } | 
|  | 3718 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3719 | AudioPolicyManager::~AudioPolicyManager() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3720 | { | 
|  | 3721 | #ifdef AUDIO_POLICY_TEST | 
|  | 3722 | exit(); | 
|  | 3723 | #endif //AUDIO_POLICY_TEST | 
|  | 3724 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3725 | mpClientInterface->closeOutput(mOutputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3726 | } | 
|  | 3727 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 3728 | mpClientInterface->closeInput(mInputs.keyAt(i)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3729 | } | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 3730 | mAvailableOutputDevices.clear(); | 
|  | 3731 | mAvailableInputDevices.clear(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 3732 | mOutputs.clear(); | 
|  | 3733 | mInputs.clear(); | 
|  | 3734 | mHwModules.clear(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3735 | } | 
|  | 3736 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3737 | status_t AudioPolicyManager::initCheck() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3738 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 3739 | return hasPrimaryOutput() ? NO_ERROR : NO_INIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3740 | } | 
|  | 3741 |  | 
|  | 3742 | #ifdef AUDIO_POLICY_TEST | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3743 | bool AudioPolicyManager::threadLoop() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3744 | { | 
|  | 3745 | ALOGV("entering threadLoop()"); | 
|  | 3746 | while (!exitPending()) | 
|  | 3747 | { | 
|  | 3748 | String8 command; | 
|  | 3749 | int valueInt; | 
|  | 3750 | String8 value; | 
|  | 3751 |  | 
|  | 3752 | Mutex::Autolock _l(mLock); | 
|  | 3753 | mWaitWorkCV.waitRelative(mLock, milliseconds(50)); | 
|  | 3754 |  | 
|  | 3755 | command = mpClientInterface->getParameters(0, String8("test_cmd_policy")); | 
|  | 3756 | AudioParameter param = AudioParameter(command); | 
|  | 3757 |  | 
|  | 3758 | if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR && | 
|  | 3759 | valueInt != 0) { | 
|  | 3760 | ALOGV("Test command %s received", command.string()); | 
|  | 3761 | String8 target; | 
|  | 3762 | if (param.get(String8("target"), target) != NO_ERROR) { | 
|  | 3763 | target = "Manager"; | 
|  | 3764 | } | 
|  | 3765 | if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) { | 
|  | 3766 | param.remove(String8("test_cmd_policy_output")); | 
|  | 3767 | mCurOutput = valueInt; | 
|  | 3768 | } | 
|  | 3769 | if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) { | 
|  | 3770 | param.remove(String8("test_cmd_policy_direct")); | 
|  | 3771 | if (value == "false") { | 
|  | 3772 | mDirectOutput = false; | 
|  | 3773 | } else if (value == "true") { | 
|  | 3774 | mDirectOutput = true; | 
|  | 3775 | } | 
|  | 3776 | } | 
|  | 3777 | if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) { | 
|  | 3778 | param.remove(String8("test_cmd_policy_input")); | 
|  | 3779 | mTestInput = valueInt; | 
|  | 3780 | } | 
|  | 3781 |  | 
|  | 3782 | if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) { | 
|  | 3783 | param.remove(String8("test_cmd_policy_format")); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3784 | int format = AUDIO_FORMAT_INVALID; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3785 | if (value == "PCM 16 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3786 | format = AUDIO_FORMAT_PCM_16_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3787 | } else if (value == "PCM 8 bits") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3788 | format = AUDIO_FORMAT_PCM_8_BIT; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3789 | } else if (value == "Compressed MP3") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3790 | format = AUDIO_FORMAT_MP3; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3791 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3792 | if (format != AUDIO_FORMAT_INVALID) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3793 | if (target == "Manager") { | 
|  | 3794 | mTestFormat = format; | 
|  | 3795 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3796 | AudioParameter outputParam = AudioParameter(); | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3797 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedFormats), format); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3798 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3799 | } | 
|  | 3800 | } | 
|  | 3801 | } | 
|  | 3802 | if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) { | 
|  | 3803 | param.remove(String8("test_cmd_policy_channels")); | 
|  | 3804 | int channels = 0; | 
|  | 3805 |  | 
|  | 3806 | if (value == "Channels Stereo") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3807 | channels =  AUDIO_CHANNEL_OUT_STEREO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3808 | } else if (value == "Channels Mono") { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3809 | channels =  AUDIO_CHANNEL_OUT_MONO; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3810 | } | 
|  | 3811 | if (channels != 0) { | 
|  | 3812 | if (target == "Manager") { | 
|  | 3813 | mTestChannels = channels; | 
|  | 3814 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3815 | AudioParameter outputParam = AudioParameter(); | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3816 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedChannels), channels); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3817 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3818 | } | 
|  | 3819 | } | 
|  | 3820 | } | 
|  | 3821 | if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) { | 
|  | 3822 | param.remove(String8("test_cmd_policy_sampleRate")); | 
|  | 3823 | if (valueInt >= 0 && valueInt <= 96000) { | 
|  | 3824 | int samplingRate = valueInt; | 
|  | 3825 | if (target == "Manager") { | 
|  | 3826 | mTestSamplingRate = samplingRate; | 
|  | 3827 | } else if (mTestOutputs[mCurOutput] != 0) { | 
|  | 3828 | AudioParameter outputParam = AudioParameter(); | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 3829 | outputParam.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), samplingRate); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3830 | mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString()); | 
|  | 3831 | } | 
|  | 3832 | } | 
|  | 3833 | } | 
|  | 3834 |  | 
|  | 3835 | if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) { | 
|  | 3836 | param.remove(String8("test_cmd_policy_reopen")); | 
|  | 3837 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3838 | mpClientInterface->closeOutput(mpClientInterface->closeOutput(mPrimaryOutput);); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3839 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3840 | audio_module_handle_t moduleHandle = mPrimaryOutput->getModuleHandle(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3841 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3842 | removeOutput(mPrimaryOutput->mIoHandle); | 
|  | 3843 | sp<SwAudioOutputDescriptor> outputDesc = new AudioOutputDescriptor(NULL, | 
|  | 3844 | mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3845 | outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3846 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 3847 | config.sample_rate = outputDesc->mSamplingRate; | 
|  | 3848 | config.channel_mask = outputDesc->mChannelMask; | 
|  | 3849 | config.format = outputDesc->mFormat; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3850 | audio_io_handle_t handle; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3851 | status_t status = mpClientInterface->openOutput(moduleHandle, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3852 | &handle, | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3853 | &config, | 
|  | 3854 | &outputDesc->mDevice, | 
|  | 3855 | String8(""), | 
|  | 3856 | &outputDesc->mLatency, | 
|  | 3857 | outputDesc->mFlags); | 
|  | 3858 | if (status != NO_ERROR) { | 
|  | 3859 | ALOGE("Failed to reopen hardware output stream, " | 
|  | 3860 | "samplingRate: %d, format %d, channels %d", | 
|  | 3861 | outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3862 | } else { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 3863 | outputDesc->mSamplingRate = config.sample_rate; | 
|  | 3864 | outputDesc->mChannelMask = config.channel_mask; | 
|  | 3865 | outputDesc->mFormat = config.format; | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3866 | mPrimaryOutput = outputDesc; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3867 | AudioParameter outputCmd = AudioParameter(); | 
|  | 3868 | outputCmd.addInt(String8("set_id"), 0); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3869 | mpClientInterface->setParameters(handle, outputCmd.toString()); | 
|  | 3870 | addOutput(handle, outputDesc); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3871 | } | 
|  | 3872 | } | 
|  | 3873 |  | 
|  | 3874 |  | 
|  | 3875 | mpClientInterface->setParameters(0, String8("test_cmd_policy=")); | 
|  | 3876 | } | 
|  | 3877 | } | 
|  | 3878 | return false; | 
|  | 3879 | } | 
|  | 3880 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3881 | void AudioPolicyManager::exit() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3882 | { | 
|  | 3883 | { | 
|  | 3884 | AutoMutex _l(mLock); | 
|  | 3885 | requestExit(); | 
|  | 3886 | mWaitWorkCV.signal(); | 
|  | 3887 | } | 
|  | 3888 | requestExitAndWait(); | 
|  | 3889 | } | 
|  | 3890 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 3891 | int AudioPolicyManager::testOutputIndex(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3892 | { | 
|  | 3893 | for (int i = 0; i < NUM_TEST_OUTPUTS; i++) { | 
|  | 3894 | if (output == mTestOutputs[i]) return i; | 
|  | 3895 | } | 
|  | 3896 | return 0; | 
|  | 3897 | } | 
|  | 3898 | #endif //AUDIO_POLICY_TEST | 
|  | 3899 |  | 
|  | 3900 | // --- | 
|  | 3901 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3902 | void AudioPolicyManager::addOutput(audio_io_handle_t output, const sp<SwAudioOutputDescriptor>& outputDesc) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3903 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3904 | outputDesc->setIoHandle(output); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3905 | mOutputs.add(output, outputDesc); | 
| Andy Hung | 2ddee19 | 2015-12-18 17:34:44 -0800 | [diff] [blame] | 3906 | updateMono(output); // update mono status when adding to output list | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3907 | selectOutputForMusicEffects(); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3908 | nextAudioPortGeneration(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3909 | } | 
|  | 3910 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3911 | void AudioPolicyManager::removeOutput(audio_io_handle_t output) | 
|  | 3912 | { | 
|  | 3913 | mOutputs.removeItem(output); | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 3914 | selectOutputForMusicEffects(); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3915 | } | 
|  | 3916 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3917 | void AudioPolicyManager::addInput(audio_io_handle_t input, const sp<AudioInputDescriptor>& inputDesc) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3918 | { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 3919 | inputDesc->setIoHandle(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3920 | mInputs.add(input, inputDesc); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 3921 | nextAudioPortGeneration(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 3922 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3923 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3924 | void AudioPolicyManager::findIoHandlesByAddress(const sp<SwAudioOutputDescriptor>& desc /*in*/, | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3925 | const audio_devices_t device /*in*/, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3926 | const String8& address /*in*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3927 | SortedVector<audio_io_handle_t>& outputs /*out*/) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3928 | sp<DeviceDescriptor> devDesc = | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3929 | desc->mProfile->getSupportedDeviceByAddress(device, address); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3930 | if (devDesc != 0) { | 
|  | 3931 | ALOGV("findIoHandlesByAddress(): adding opened output %d on same address %s", | 
|  | 3932 | desc->mIoHandle, address.string()); | 
|  | 3933 | outputs.add(desc->mIoHandle); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3934 | } | 
|  | 3935 | } | 
|  | 3936 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3937 | status_t AudioPolicyManager::checkOutputsForDevice(const sp<DeviceDescriptor>& devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3938 | audio_policy_dev_state_t state, | 
|  | 3939 | SortedVector<audio_io_handle_t>& outputs, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 3940 | const String8& address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3941 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3942 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3943 | sp<SwAudioOutputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3944 |  | 
|  | 3945 | if (audio_device_is_digital(device)) { | 
|  | 3946 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 3947 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 3948 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3949 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 3950 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3951 | // first list already open outputs that can be routed to this device | 
|  | 3952 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 3953 | desc = mOutputs.valueAt(i); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 3954 | if (!desc->isDuplicated() && (desc->supportedDevices() & device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3955 | if (!device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3956 | ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i)); | 
|  | 3957 | outputs.add(mOutputs.keyAt(i)); | 
|  | 3958 | } else { | 
|  | 3959 | ALOGV("  checking address match due to device 0x%x", device); | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 3960 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3961 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3962 | } | 
|  | 3963 | } | 
|  | 3964 | // then look for output profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 3965 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3966 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 3967 | { | 
|  | 3968 | if (mHwModules[i]->mHandle == 0) { | 
|  | 3969 | continue; | 
|  | 3970 | } | 
|  | 3971 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 3972 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3973 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3974 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 3975 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 3976 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 3977 | profiles.add(profile); | 
|  | 3978 | ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i); | 
|  | 3979 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3980 | } | 
|  | 3981 | } | 
|  | 3982 | } | 
|  | 3983 |  | 
| Eric Laurent | 7b279bb | 2015-12-14 10:18:23 -0800 | [diff] [blame] | 3984 | ALOGV("  found %zu profiles, %zu outputs", profiles.size(), outputs.size()); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3985 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3986 | if (profiles.isEmpty() && outputs.isEmpty()) { | 
|  | 3987 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 3988 | return BAD_VALUE; | 
|  | 3989 | } | 
|  | 3990 |  | 
|  | 3991 | // open outputs for matching profiles if needed. Direct outputs are also opened to | 
|  | 3992 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 3993 | 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] | 3994 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 3995 |  | 
|  | 3996 | // nothing to do if one output is already opened for this profile | 
|  | 3997 | size_t j; | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 3998 | for (j = 0; j < outputs.size(); j++) { | 
|  | 3999 | desc = mOutputs.valueFor(outputs.itemAt(j)); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4000 | if (!desc->isDuplicated() && desc->mProfile == profile) { | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4001 | // matching profile: save the sample rates, format and channel masks supported | 
|  | 4002 | // by the profile in our device descriptor | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4003 | if (audio_device_is_digital(device)) { | 
|  | 4004 | devDesc->importAudioPort(profile); | 
|  | 4005 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4006 | break; | 
|  | 4007 | } | 
|  | 4008 | } | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4009 | if (j != outputs.size()) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4010 | continue; | 
|  | 4011 | } | 
|  | 4012 |  | 
| Eric Laurent | 83b8808 | 2014-06-20 18:31:16 -0700 | [diff] [blame] | 4013 | ALOGV("opening output for device %08x with params %s profile %p", | 
|  | 4014 | device, address.string(), profile.get()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4015 | desc = new SwAudioOutputDescriptor(profile, mpClientInterface); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4016 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4017 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 4018 | config.sample_rate = desc->mSamplingRate; | 
|  | 4019 | config.channel_mask = desc->mChannelMask; | 
|  | 4020 | config.format = desc->mFormat; | 
|  | 4021 | config.offload_info.sample_rate = desc->mSamplingRate; | 
|  | 4022 | config.offload_info.channel_mask = desc->mChannelMask; | 
|  | 4023 | config.offload_info.format = desc->mFormat; | 
|  | 4024 | audio_io_handle_t output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4025 | status_t status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4026 | &output, | 
|  | 4027 | &config, | 
|  | 4028 | &desc->mDevice, | 
|  | 4029 | address, | 
|  | 4030 | &desc->mLatency, | 
|  | 4031 | desc->mFlags); | 
|  | 4032 | if (status == NO_ERROR) { | 
|  | 4033 | desc->mSamplingRate = config.sample_rate; | 
|  | 4034 | desc->mChannelMask = config.channel_mask; | 
|  | 4035 | desc->mFormat = config.format; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4036 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4037 | // Here is where the out_set_parameters() for card & device gets called | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4038 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4039 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 4040 | mpClientInterface->setParameters(output, String8(param)); | 
|  | 4041 | free(param); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4042 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4043 | updateAudioProfiles(device, output, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4044 | if (!profile->hasValidAudioProfile()) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4045 | ALOGW("checkOutputsForDevice() missing param"); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4046 | mpClientInterface->closeOutput(output); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4047 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4048 | } else if (profile->hasDynamicAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4049 | mpClientInterface->closeOutput(output); | 
| Phil Burk | 702b105 | 2016-03-02 16:38:26 -0800 | [diff] [blame] | 4050 | output = AUDIO_IO_HANDLE_NONE; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4051 | profile->pickAudioProfile(config.sample_rate, config.channel_mask, config.format); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4052 | config.offload_info.sample_rate = config.sample_rate; | 
|  | 4053 | config.offload_info.channel_mask = config.channel_mask; | 
|  | 4054 | config.offload_info.format = config.format; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4055 | status = mpClientInterface->openOutput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4056 | &output, | 
|  | 4057 | &config, | 
|  | 4058 | &desc->mDevice, | 
|  | 4059 | address, | 
|  | 4060 | &desc->mLatency, | 
|  | 4061 | desc->mFlags); | 
|  | 4062 | if (status == NO_ERROR) { | 
|  | 4063 | desc->mSamplingRate = config.sample_rate; | 
|  | 4064 | desc->mChannelMask = config.channel_mask; | 
|  | 4065 | desc->mFormat = config.format; | 
|  | 4066 | } else { | 
|  | 4067 | output = AUDIO_IO_HANDLE_NONE; | 
|  | 4068 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4069 | } | 
|  | 4070 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4071 | if (output != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4072 | addOutput(output, desc); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4073 | if (device_distinguishes_on_address(device) && address != "0") { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4074 | sp<AudioPolicyMix> policyMix; | 
|  | 4075 | if (mPolicyMixes.getAudioPolicyMix(address, policyMix) != NO_ERROR) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4076 | ALOGE("checkOutputsForDevice() cannot find policy for address %s", | 
|  | 4077 | address.string()); | 
|  | 4078 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4079 | policyMix->setOutput(desc); | 
| Jean-Michel Trivi | dacc06f | 2015-04-08 18:16:39 -0700 | [diff] [blame] | 4080 | desc->mPolicyMix = policyMix->getMix(); | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4081 |  | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 4082 | } else if (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) && | 
|  | 4083 | hasPrimaryOutput()) { | 
| Eric Laurent | c722f30 | 2014-12-10 11:21:49 -0800 | [diff] [blame] | 4084 | // no duplicated output for direct outputs and | 
|  | 4085 | // outputs used by dynamic policy mixes | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4086 | audio_io_handle_t duplicatedOutput = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4087 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4088 | // set initial stream volume for device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4089 | applyStreamVolumes(desc, device, 0, true); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4090 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4091 | //TODO: configure audio effect output stage here | 
|  | 4092 |  | 
|  | 4093 | // open a duplicating output thread for the new output and the primary output | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4094 | duplicatedOutput = | 
|  | 4095 | mpClientInterface->openDuplicateOutput(output, | 
|  | 4096 | mPrimaryOutput->mIoHandle); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4097 | if (duplicatedOutput != AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4098 | // add duplicated output descriptor | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4099 | sp<SwAudioOutputDescriptor> dupOutputDesc = | 
|  | 4100 | new SwAudioOutputDescriptor(NULL, mpClientInterface); | 
|  | 4101 | dupOutputDesc->mOutput1 = mPrimaryOutput; | 
|  | 4102 | dupOutputDesc->mOutput2 = desc; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4103 | dupOutputDesc->mSamplingRate = desc->mSamplingRate; | 
|  | 4104 | dupOutputDesc->mFormat = desc->mFormat; | 
|  | 4105 | dupOutputDesc->mChannelMask = desc->mChannelMask; | 
|  | 4106 | dupOutputDesc->mLatency = desc->mLatency; | 
|  | 4107 | addOutput(duplicatedOutput, dupOutputDesc); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4108 | applyStreamVolumes(dupOutputDesc, device, 0, true); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4109 | } else { | 
|  | 4110 | ALOGW("checkOutputsForDevice() could not open dup output for %d and %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4111 | mPrimaryOutput->mIoHandle, output); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4112 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4113 | removeOutput(output); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4114 | nextAudioPortGeneration(); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4115 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4116 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4117 | } | 
|  | 4118 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4119 | } else { | 
|  | 4120 | output = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4121 | } | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4122 | if (output == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4123 | ALOGW("checkOutputsForDevice() could not open output for device %x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4124 | profiles.removeAt(profile_index); | 
|  | 4125 | profile_index--; | 
|  | 4126 | } else { | 
|  | 4127 | outputs.add(output); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4128 | // Load digital format info only for digital devices | 
|  | 4129 | if (audio_device_is_digital(device)) { | 
|  | 4130 | devDesc->importAudioPort(profile); | 
|  | 4131 | } | 
| Jean-Michel Trivi | f17026d | 2014-08-10 14:30:48 -0700 | [diff] [blame] | 4132 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4133 | if (device_distinguishes_on_address(device)) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4134 | ALOGV("checkOutputsForDevice(): setOutputDevice(dev=0x%x, addr=%s)", | 
|  | 4135 | device, address.string()); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4136 | setOutputDevice(desc, device, true/*force*/, 0/*delay*/, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4137 | NULL/*patch handle*/, address.string()); | 
|  | 4138 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4139 | ALOGV("checkOutputsForDevice(): adding output %d", output); | 
|  | 4140 | } | 
|  | 4141 | } | 
|  | 4142 |  | 
|  | 4143 | if (profiles.isEmpty()) { | 
|  | 4144 | ALOGW("checkOutputsForDevice(): No output available for device %04x", device); | 
|  | 4145 | return BAD_VALUE; | 
|  | 4146 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4147 | } else { // Disconnect | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4148 | // check if one opened output is not needed any more after disconnecting one device | 
|  | 4149 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
|  | 4150 | desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4151 | if (!desc->isDuplicated()) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4152 | // exact match on device | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4153 | if (device_distinguishes_on_address(device) && | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4154 | (desc->supportedDevices() == device)) { | 
| keunyoung | 3190e67 | 2014-12-30 13:00:52 -0800 | [diff] [blame] | 4155 | findIoHandlesByAddress(desc, device, address, outputs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4156 | } else if (!(desc->supportedDevices() & mAvailableOutputDevices.types())) { | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4157 | ALOGV("checkOutputsForDevice(): disconnecting adding output %d", | 
|  | 4158 | mOutputs.keyAt(i)); | 
|  | 4159 | outputs.add(mOutputs.keyAt(i)); | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4160 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4161 | } | 
|  | 4162 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4163 | // Clear any profiles associated with the disconnected device. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4164 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 4165 | { | 
|  | 4166 | if (mHwModules[i]->mHandle == 0) { | 
|  | 4167 | continue; | 
|  | 4168 | } | 
|  | 4169 | for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) | 
|  | 4170 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4171 | sp<IOProfile> profile = mHwModules[i]->mOutputProfiles[j]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4172 | if (profile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4173 | ALOGV("checkOutputsForDevice(): " | 
|  | 4174 | "clearing direct output profile %zu on module %zu", j, i); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4175 | profile->clearAudioProfiles(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4176 | } | 
|  | 4177 | } | 
|  | 4178 | } | 
|  | 4179 | } | 
|  | 4180 | return NO_ERROR; | 
|  | 4181 | } | 
|  | 4182 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4183 | status_t AudioPolicyManager::checkInputsForDevice(const sp<DeviceDescriptor>& devDesc, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4184 | audio_policy_dev_state_t state, | 
|  | 4185 | SortedVector<audio_io_handle_t>& inputs, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4186 | const String8& address) | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4187 | { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4188 | audio_devices_t device = devDesc->type(); | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4189 | sp<AudioInputDescriptor> desc; | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4190 |  | 
|  | 4191 | if (audio_device_is_digital(device)) { | 
|  | 4192 | // erase all current sample rates, formats and channel masks | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 4193 | devDesc->clearAudioProfiles(); | 
| Eric Laurent | cc750d3 | 2015-06-25 11:48:20 -0700 | [diff] [blame] | 4194 | } | 
|  | 4195 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4196 | if (state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) { | 
|  | 4197 | // first list already open inputs that can be routed to this device | 
|  | 4198 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4199 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4200 | if (desc->mProfile->supportDevice(device)) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4201 | ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index)); | 
|  | 4202 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 4203 | } | 
|  | 4204 | } | 
|  | 4205 |  | 
|  | 4206 | // then look for input profiles that can be routed to this device | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4207 | SortedVector< sp<IOProfile> > profiles; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4208 | for (size_t module_idx = 0; module_idx < mHwModules.size(); module_idx++) | 
|  | 4209 | { | 
|  | 4210 | if (mHwModules[module_idx]->mHandle == 0) { | 
|  | 4211 | continue; | 
|  | 4212 | } | 
|  | 4213 | for (size_t profile_index = 0; | 
|  | 4214 | profile_index < mHwModules[module_idx]->mInputProfiles.size(); | 
|  | 4215 | profile_index++) | 
|  | 4216 | { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4217 | sp<IOProfile> profile = mHwModules[module_idx]->mInputProfiles[profile_index]; | 
|  | 4218 |  | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4219 | if (profile->supportDevice(device)) { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4220 | if (!device_distinguishes_on_address(device) || | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4221 | profile->supportDeviceAddress(address)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4222 | profiles.add(profile); | 
|  | 4223 | ALOGV("checkInputsForDevice(): adding profile %zu from module %zu", | 
|  | 4224 | profile_index, module_idx); | 
|  | 4225 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4226 | } | 
|  | 4227 | } | 
|  | 4228 | } | 
|  | 4229 |  | 
|  | 4230 | if (profiles.isEmpty() && inputs.isEmpty()) { | 
|  | 4231 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 4232 | return BAD_VALUE; | 
|  | 4233 | } | 
|  | 4234 |  | 
|  | 4235 | // open inputs for matching profiles if needed. Direct inputs are also opened to | 
|  | 4236 | // query for dynamic parameters and will be closed later by setDeviceConnectionState() | 
|  | 4237 | for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) { | 
|  | 4238 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4239 | sp<IOProfile> profile = profiles[profile_index]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4240 | // nothing to do if one input is already opened for this profile | 
|  | 4241 | size_t input_index; | 
|  | 4242 | for (input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4243 | desc = mInputs.valueAt(input_index); | 
|  | 4244 | if (desc->mProfile == profile) { | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4245 | if (audio_device_is_digital(device)) { | 
|  | 4246 | devDesc->importAudioPort(profile); | 
|  | 4247 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4248 | break; | 
|  | 4249 | } | 
|  | 4250 | } | 
|  | 4251 | if (input_index != mInputs.size()) { | 
|  | 4252 | continue; | 
|  | 4253 | } | 
|  | 4254 |  | 
|  | 4255 | ALOGV("opening input for device 0x%X with params %s", device, address.string()); | 
|  | 4256 | desc = new AudioInputDescriptor(profile); | 
|  | 4257 | desc->mDevice = device; | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4258 | audio_config_t config = AUDIO_CONFIG_INITIALIZER; | 
|  | 4259 | config.sample_rate = desc->mSamplingRate; | 
|  | 4260 | config.channel_mask = desc->mChannelMask; | 
|  | 4261 | config.format = desc->mFormat; | 
|  | 4262 | audio_io_handle_t input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | 322b4d2 | 2015-04-03 15:57:54 -0700 | [diff] [blame] | 4263 | status_t status = mpClientInterface->openInput(profile->getModuleHandle(), | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4264 | &input, | 
|  | 4265 | &config, | 
|  | 4266 | &desc->mDevice, | 
|  | 4267 | address, | 
|  | 4268 | AUDIO_SOURCE_MIC, | 
|  | 4269 | AUDIO_INPUT_FLAG_NONE /*FIXME*/); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4270 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4271 | if (status == NO_ERROR) { | 
|  | 4272 | desc->mSamplingRate = config.sample_rate; | 
|  | 4273 | desc->mChannelMask = config.channel_mask; | 
|  | 4274 | desc->mFormat = config.format; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4275 |  | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4276 | if (!address.isEmpty()) { | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4277 | char *param = audio_device_address_to_parameter(device, address); | 
|  | 4278 | mpClientInterface->setParameters(input, String8(param)); | 
|  | 4279 | free(param); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4280 | } | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 4281 | updateAudioProfiles(device, input, profile->getAudioProfiles()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4282 | if (!profile->hasValidAudioProfile()) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4283 | ALOGW("checkInputsForDevice() direct input missing param"); | 
|  | 4284 | mpClientInterface->closeInput(input); | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4285 | input = AUDIO_IO_HANDLE_NONE; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4286 | } | 
|  | 4287 |  | 
|  | 4288 | if (input != 0) { | 
|  | 4289 | addInput(input, desc); | 
|  | 4290 | } | 
|  | 4291 | } // endif input != 0 | 
|  | 4292 |  | 
| Eric Laurent | cf2c021 | 2014-07-25 16:20:43 -0700 | [diff] [blame] | 4293 | if (input == AUDIO_IO_HANDLE_NONE) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4294 | ALOGW("checkInputsForDevice() could not open input for device 0x%X", device); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4295 | profiles.removeAt(profile_index); | 
|  | 4296 | profile_index--; | 
|  | 4297 | } else { | 
|  | 4298 | inputs.add(input); | 
| Paul McLean | 9080a4c | 2015-06-18 08:24:02 -0700 | [diff] [blame] | 4299 | if (audio_device_is_digital(device)) { | 
|  | 4300 | devDesc->importAudioPort(profile); | 
|  | 4301 | } | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4302 | ALOGV("checkInputsForDevice(): adding input %d", input); | 
|  | 4303 | } | 
|  | 4304 | } // end scan profiles | 
|  | 4305 |  | 
|  | 4306 | if (profiles.isEmpty()) { | 
|  | 4307 | ALOGW("checkInputsForDevice(): No input available for device 0x%X", device); | 
|  | 4308 | return BAD_VALUE; | 
|  | 4309 | } | 
|  | 4310 | } else { | 
|  | 4311 | // Disconnect | 
|  | 4312 | // check if one opened input is not needed any more after disconnecting one device | 
|  | 4313 | for (size_t input_index = 0; input_index < mInputs.size(); input_index++) { | 
|  | 4314 | desc = mInputs.valueAt(input_index); | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4315 | if (!(desc->mProfile->supportDevice(mAvailableInputDevices.types()))) { | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4316 | ALOGV("checkInputsForDevice(): disconnecting adding input %d", | 
|  | 4317 | mInputs.keyAt(input_index)); | 
|  | 4318 | inputs.add(mInputs.keyAt(input_index)); | 
|  | 4319 | } | 
|  | 4320 | } | 
|  | 4321 | // Clear any profiles associated with the disconnected device. | 
|  | 4322 | for (size_t module_index = 0; module_index < mHwModules.size(); module_index++) { | 
|  | 4323 | if (mHwModules[module_index]->mHandle == 0) { | 
|  | 4324 | continue; | 
|  | 4325 | } | 
|  | 4326 | for (size_t profile_index = 0; | 
|  | 4327 | profile_index < mHwModules[module_index]->mInputProfiles.size(); | 
|  | 4328 | profile_index++) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4329 | sp<IOProfile> profile = mHwModules[module_index]->mInputProfiles[profile_index]; | 
| François Gaffie | a8ecc2c | 2015-11-09 16:10:40 +0100 | [diff] [blame] | 4330 | if (profile->supportDevice(device)) { | 
| Mark Salyzyn | beb9e30 | 2014-06-18 16:33:15 -0700 | [diff] [blame] | 4331 | ALOGV("checkInputsForDevice(): clearing direct input profile %zu on module %zu", | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4332 | profile_index, module_index); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 4333 | profile->clearAudioProfiles(); | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 4334 | } | 
|  | 4335 | } | 
|  | 4336 | } | 
|  | 4337 | } // end disconnect | 
|  | 4338 |  | 
|  | 4339 | return NO_ERROR; | 
|  | 4340 | } | 
|  | 4341 |  | 
|  | 4342 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4343 | void AudioPolicyManager::closeOutput(audio_io_handle_t output) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4344 | { | 
|  | 4345 | ALOGV("closeOutput(%d)", output); | 
|  | 4346 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4347 | sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueFor(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4348 | if (outputDesc == NULL) { | 
|  | 4349 | ALOGW("closeOutput() unknown output %d", output); | 
|  | 4350 | return; | 
|  | 4351 | } | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 4352 | mPolicyMixes.closeOutput(outputDesc); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 4353 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4354 | // look for duplicated outputs connected to the output being removed. | 
|  | 4355 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4356 | sp<SwAudioOutputDescriptor> dupOutputDesc = mOutputs.valueAt(i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4357 | if (dupOutputDesc->isDuplicated() && | 
|  | 4358 | (dupOutputDesc->mOutput1 == outputDesc || | 
|  | 4359 | dupOutputDesc->mOutput2 == outputDesc)) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4360 | sp<AudioOutputDescriptor> outputDesc2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4361 | if (dupOutputDesc->mOutput1 == outputDesc) { | 
|  | 4362 | outputDesc2 = dupOutputDesc->mOutput2; | 
|  | 4363 | } else { | 
|  | 4364 | outputDesc2 = dupOutputDesc->mOutput1; | 
|  | 4365 | } | 
|  | 4366 | // As all active tracks on duplicated output will be deleted, | 
|  | 4367 | // and as they were also referenced on the other output, the reference | 
|  | 4368 | // count for their stream type must be adjusted accordingly on | 
|  | 4369 | // the other output. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4370 | for (int j = 0; j < AUDIO_STREAM_CNT; j++) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4371 | int refCount = dupOutputDesc->mRefCount[j]; | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4372 | outputDesc2->changeRefCount((audio_stream_type_t)j,-refCount); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4373 | } | 
|  | 4374 | audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i); | 
|  | 4375 | ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput); | 
|  | 4376 |  | 
|  | 4377 | mpClientInterface->closeOutput(duplicatedOutput); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4378 | removeOutput(duplicatedOutput); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4379 | } | 
|  | 4380 | } | 
|  | 4381 |  | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4382 | nextAudioPortGeneration(); | 
|  | 4383 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4384 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4385 | if (index >= 0) { | 
|  | 4386 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4387 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4388 | mAudioPatches.removeItemsAt(index); | 
|  | 4389 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4390 | } | 
|  | 4391 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4392 | AudioParameter param; | 
|  | 4393 | param.add(String8("closing"), String8("true")); | 
|  | 4394 | mpClientInterface->setParameters(output, param.toString()); | 
|  | 4395 |  | 
|  | 4396 | mpClientInterface->closeOutput(output); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4397 | removeOutput(output); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4398 | mPreviousOutputs = mOutputs; | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4399 | } | 
|  | 4400 |  | 
|  | 4401 | void AudioPolicyManager::closeInput(audio_io_handle_t input) | 
|  | 4402 | { | 
|  | 4403 | ALOGV("closeInput(%d)", input); | 
|  | 4404 |  | 
|  | 4405 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
|  | 4406 | if (inputDesc == NULL) { | 
|  | 4407 | ALOGW("closeInput() unknown input %d", input); | 
|  | 4408 | return; | 
|  | 4409 | } | 
|  | 4410 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4411 | nextAudioPortGeneration(); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4412 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4413 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4414 | if (index >= 0) { | 
|  | 4415 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
| Glenn Kasten | fcddb0b | 2016-07-08 17:19:25 -0700 | [diff] [blame] | 4416 | (void) /*status_t status*/ mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 05b90f8 | 2014-08-27 15:32:29 -0700 | [diff] [blame] | 4417 | mAudioPatches.removeItemsAt(index); | 
|  | 4418 | mpClientInterface->onAudioPatchListUpdate(); | 
|  | 4419 | } | 
|  | 4420 |  | 
|  | 4421 | mpClientInterface->closeInput(input); | 
|  | 4422 | mInputs.removeItem(input); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4423 | } | 
|  | 4424 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4425 | SortedVector<audio_io_handle_t> AudioPolicyManager::getOutputsForDevice( | 
|  | 4426 | audio_devices_t device, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4427 | const SwAudioOutputCollection& openOutputs) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4428 | { | 
|  | 4429 | SortedVector<audio_io_handle_t> outputs; | 
|  | 4430 |  | 
|  | 4431 | ALOGVV("getOutputsForDevice() device %04x", device); | 
|  | 4432 | for (size_t i = 0; i < openOutputs.size(); i++) { | 
| Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4433 | ALOGVV("output %zu isDuplicated=%d device=%04x", | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 4434 | i, openOutputs.valueAt(i)->isDuplicated(), | 
|  | 4435 | openOutputs.valueAt(i)->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4436 | if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) { | 
|  | 4437 | ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i)); | 
|  | 4438 | outputs.add(openOutputs.keyAt(i)); | 
|  | 4439 | } | 
|  | 4440 | } | 
|  | 4441 | return outputs; | 
|  | 4442 | } | 
|  | 4443 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4444 | bool AudioPolicyManager::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4445 | SortedVector<audio_io_handle_t>& outputs2) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4446 | { | 
|  | 4447 | if (outputs1.size() != outputs2.size()) { | 
|  | 4448 | return false; | 
|  | 4449 | } | 
|  | 4450 | for (size_t i = 0; i < outputs1.size(); i++) { | 
|  | 4451 | if (outputs1[i] != outputs2[i]) { | 
|  | 4452 | return false; | 
|  | 4453 | } | 
|  | 4454 | } | 
|  | 4455 | return true; | 
|  | 4456 | } | 
|  | 4457 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4458 | void AudioPolicyManager::checkOutputForStrategy(routing_strategy strategy) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4459 | { | 
|  | 4460 | audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/); | 
|  | 4461 | audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/); | 
|  | 4462 | SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs); | 
|  | 4463 | SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs); | 
|  | 4464 |  | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4465 | // also take into account external policy-related changes: add all outputs which are | 
|  | 4466 | // associated with policies in the "before" and "after" output vectors | 
|  | 4467 | ALOGVV("checkOutputForStrategy(): policy related outputs"); | 
|  | 4468 | for (size_t i = 0 ; i < mPreviousOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4469 | const sp<SwAudioOutputDescriptor> desc = mPreviousOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4470 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4471 | srcOutputs.add(desc->mIoHandle); | 
|  | 4472 | ALOGVV(" previous outputs: adding %d", desc->mIoHandle); | 
|  | 4473 | } | 
|  | 4474 | } | 
|  | 4475 | for (size_t i = 0 ; i < mOutputs.size() ; i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4476 | const sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | fe472e2 | 2014-12-16 14:23:13 -0800 | [diff] [blame] | 4477 | if (desc != 0 && desc->mPolicyMix != NULL) { | 
|  | 4478 | dstOutputs.add(desc->mIoHandle); | 
|  | 4479 | ALOGVV(" new outputs: adding %d", desc->mIoHandle); | 
|  | 4480 | } | 
|  | 4481 | } | 
|  | 4482 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4483 | if (!vectorsEqual(srcOutputs,dstOutputs)) { | 
|  | 4484 | ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d", | 
|  | 4485 | strategy, srcOutputs[0], dstOutputs[0]); | 
|  | 4486 | // mute strategy while moving tracks from one output to another | 
|  | 4487 | for (size_t i = 0; i < srcOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4488 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueFor(srcOutputs[i]); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4489 | if (isStrategyActive(desc, strategy)) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4490 | setStrategyMute(strategy, true, desc); | 
|  | 4491 | setStrategyMute(strategy, false, desc, MUTE_TIME_MS, newDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4492 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 4493 | sp<AudioSourceDescriptor> source = | 
|  | 4494 | getSourceForStrategyOnOutput(srcOutputs[i], strategy); | 
|  | 4495 | if (source != 0){ | 
|  | 4496 | connectAudioSource(source); | 
|  | 4497 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4498 | } | 
|  | 4499 |  | 
|  | 4500 | // Move effects associated to this strategy from previous output to new output | 
|  | 4501 | if (strategy == STRATEGY_MEDIA) { | 
| Eric Laurent | 36829f9 | 2017-04-07 19:04:42 -0700 | [diff] [blame] | 4502 | selectOutputForMusicEffects(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4503 | } | 
|  | 4504 | // Move tracks associated to this strategy from previous output to new output | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4505 | for (int i = 0; i < AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4506 | if (getStrategy((audio_stream_type_t)i) == strategy) { | 
|  | 4507 | mpClientInterface->invalidateStream((audio_stream_type_t)i); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4508 | } | 
|  | 4509 | } | 
|  | 4510 | } | 
|  | 4511 | } | 
|  | 4512 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4513 | void AudioPolicyManager::checkOutputForAllStrategies() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4514 | { | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4515 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4516 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4517 | checkOutputForStrategy(STRATEGY_PHONE); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4518 | if (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4519 | checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4520 | checkOutputForStrategy(STRATEGY_SONIFICATION); | 
|  | 4521 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4522 | checkOutputForStrategy(STRATEGY_ACCESSIBILITY); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4523 | checkOutputForStrategy(STRATEGY_MEDIA); | 
|  | 4524 | checkOutputForStrategy(STRATEGY_DTMF); | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4525 | checkOutputForStrategy(STRATEGY_REROUTING); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4526 | } | 
|  | 4527 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4528 | void AudioPolicyManager::checkA2dpSuspend() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4529 | { | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4530 | audio_io_handle_t a2dpOutput = mOutputs.getA2dpOutput(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4531 | if (a2dpOutput == 0) { | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4532 | mA2dpSuspended = false; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4533 | return; | 
|  | 4534 | } | 
|  | 4535 |  | 
| Eric Laurent | 3a4311c | 2014-03-17 12:00:47 -0700 | [diff] [blame] | 4536 | bool isScoConnected = | 
| Eric Laurent | ddbc665 | 2014-11-13 15:13:44 -0800 | [diff] [blame] | 4537 | ((mAvailableInputDevices.types() & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & | 
|  | 4538 | ~AUDIO_DEVICE_BIT_IN) != 0) || | 
|  | 4539 | ((mAvailableOutputDevices.types() & AUDIO_DEVICE_OUT_ALL_SCO) != 0); | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4540 |  | 
|  | 4541 | // if suspended, restore A2DP output if: | 
|  | 4542 | //      ((SCO device is NOT connected) || | 
|  | 4543 | //       ((forced usage communication is NOT SCO) && (forced usage for record is NOT SCO) && | 
|  | 4544 | //        (phone state is NOT in call) && (phone state is NOT ringing))) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4545 | // | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4546 | // if not suspended, suspend A2DP output if: | 
|  | 4547 | //      (SCO device is connected) && | 
|  | 4548 | //       ((forced usage for communication is SCO) || (forced usage for record is SCO) || | 
|  | 4549 | //       ((phone state is in call) || (phone state is ringing))) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4550 | // | 
|  | 4551 | if (mA2dpSuspended) { | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4552 | if (!isScoConnected || | 
|  | 4553 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) != | 
|  | 4554 | AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 4555 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) != | 
|  | 4556 | AUDIO_POLICY_FORCE_BT_SCO) && | 
|  | 4557 | (mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4558 | (mEngine->getPhoneState() != AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4559 |  | 
|  | 4560 | mpClientInterface->restoreOutput(a2dpOutput); | 
|  | 4561 | mA2dpSuspended = false; | 
|  | 4562 | } | 
|  | 4563 | } else { | 
| Eric Laurent | f732e07 | 2016-08-03 19:30:28 -0700 | [diff] [blame] | 4564 | if (isScoConnected && | 
|  | 4565 | ((mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == | 
|  | 4566 | AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4567 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == | 
|  | 4568 | AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 4569 | (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4570 | (mEngine->getPhoneState() == AUDIO_MODE_RINGTONE))) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4571 |  | 
|  | 4572 | mpClientInterface->suspendOutput(a2dpOutput); | 
|  | 4573 | mA2dpSuspended = true; | 
|  | 4574 | } | 
|  | 4575 | } | 
|  | 4576 | } | 
|  | 4577 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4578 | audio_devices_t AudioPolicyManager::getNewOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 4579 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4580 | { | 
|  | 4581 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
|  | 4582 |  | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4583 | ssize_t index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4584 | if (index >= 0) { | 
|  | 4585 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4586 | if (patchDesc->mUid != mUidCached) { | 
|  | 4587 | ALOGV("getNewOutputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4588 | outputDesc->device(), outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4589 | return outputDesc->device(); | 
|  | 4590 | } | 
|  | 4591 | } | 
|  | 4592 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4593 | // 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] | 4594 | // 1: the strategy enforced audible is active and enforced on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4595 | //      use device for strategy enforced audible | 
|  | 4596 | // 2: we are in call or the strategy phone is active on the output: | 
|  | 4597 | //      use device for strategy phone | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4598 | // 3: the strategy for enforced audible is active but not enforced on the output: | 
|  | 4599 | //      use the device for strategy enforced audible | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4600 | // 4: the strategy sonification is active on the output: | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4601 | //      use device for strategy sonification | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4602 | // 5: the strategy accessibility is active on the output: | 
|  | 4603 | //      use device for strategy accessibility | 
| Jean-Michel Trivi | 5de234b | 2015-07-21 11:42:02 -0700 | [diff] [blame] | 4604 | // 6: the strategy "respectful" sonification is active on the output: | 
|  | 4605 | //      use device for strategy "respectful" sonification | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4606 | // 7: the strategy media is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4607 | //      use device for strategy media | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4608 | // 8: the strategy DTMF is active on the output: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4609 | //      use device for strategy DTMF | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4610 | // 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] | 4611 | //      use device for strategy t-t-s | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4612 | if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4613 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4614 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
|  | 4615 | } else if (isInCall() || | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4616 | isStrategyActive(outputDesc, STRATEGY_PHONE)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4617 | device = getDeviceForStrategy(STRATEGY_PHONE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4618 | } else if (isStrategyActive(outputDesc, STRATEGY_ENFORCED_AUDIBLE)) { | 
| Jon Eklund | 966095e | 2014-09-09 15:39:49 -0500 | [diff] [blame] | 4619 | device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4620 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4621 | device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4622 | } else if (isStrategyActive(outputDesc, STRATEGY_ACCESSIBILITY)) { | 
|  | 4623 | device = getDeviceForStrategy(STRATEGY_ACCESSIBILITY, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4624 | } else if (isStrategyActive(outputDesc, STRATEGY_SONIFICATION_RESPECTFUL)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4625 | device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4626 | } else if (isStrategyActive(outputDesc, STRATEGY_MEDIA)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4627 | device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4628 | } else if (isStrategyActive(outputDesc, STRATEGY_DTMF)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4629 | device = getDeviceForStrategy(STRATEGY_DTMF, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4630 | } else if (isStrategyActive(outputDesc, STRATEGY_TRANSMITTED_THROUGH_SPEAKER)) { | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4631 | device = getDeviceForStrategy(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, fromCache); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4632 | } else if (isStrategyActive(outputDesc, STRATEGY_REROUTING)) { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4633 | device = getDeviceForStrategy(STRATEGY_REROUTING, fromCache); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4634 | } | 
|  | 4635 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4636 | ALOGV("getNewOutputDevice() selected device %x", device); | 
|  | 4637 | return device; | 
|  | 4638 | } | 
|  | 4639 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4640 | audio_devices_t AudioPolicyManager::getNewInputDevice(const sp<AudioInputDescriptor>& inputDesc) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4641 | { | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4642 | audio_devices_t device = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4643 |  | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4644 | ssize_t index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4645 | if (index >= 0) { | 
|  | 4646 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 4647 | if (patchDesc->mUid != mUidCached) { | 
|  | 4648 | ALOGV("getNewInputDevice() device %08x forced by patch %d", | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 4649 | inputDesc->mDevice, inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4650 | return inputDesc->mDevice; | 
|  | 4651 | } | 
|  | 4652 | } | 
|  | 4653 |  | 
| Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 4654 | audio_source_t source = inputDesc->getHighestPrioritySource(true /*activeOnly*/); | 
|  | 4655 | if (isInCall()) { | 
|  | 4656 | device = getDeviceAndMixForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION); | 
|  | 4657 | } else if (source != AUDIO_SOURCE_DEFAULT) { | 
|  | 4658 | device = getDeviceAndMixForInputSource(source); | 
|  | 4659 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4660 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4661 | return device; | 
|  | 4662 | } | 
|  | 4663 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4664 | bool AudioPolicyManager::streamsMatchForvolume(audio_stream_type_t stream1, | 
|  | 4665 | audio_stream_type_t stream2) { | 
| Jean-Michel Trivi | 99bb2f9 | 2016-11-23 15:52:07 -0800 | [diff] [blame] | 4666 | return (stream1 == stream2); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4667 | } | 
|  | 4668 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4669 | uint32_t AudioPolicyManager::getStrategyForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4670 | return (uint32_t)getStrategy(stream); | 
|  | 4671 | } | 
|  | 4672 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4673 | audio_devices_t AudioPolicyManager::getDevicesForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4674 | // By checking the range of stream before calling getStrategy, we avoid | 
|  | 4675 | // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE | 
|  | 4676 | // 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] | 4677 | if (stream < (audio_stream_type_t) 0 || stream >= AUDIO_STREAM_PUBLIC_CNT) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4678 | return AUDIO_DEVICE_NONE; | 
|  | 4679 | } | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4680 | audio_devices_t devices = AUDIO_DEVICE_NONE; | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4681 | for (int curStream = 0; curStream < AUDIO_STREAM_FOR_POLICY_CNT; curStream++) { | 
|  | 4682 | if (!streamsMatchForvolume(stream, (audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4683 | continue; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4684 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4685 | routing_strategy curStrategy = getStrategy((audio_stream_type_t)curStream); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4686 | audio_devices_t curDevices = | 
| Eric Laurent | 447a87b | 2016-07-07 17:32:10 -0700 | [diff] [blame] | 4687 | getDeviceForStrategy((routing_strategy)curStrategy, false /*fromCache*/); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4688 | SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(curDevices, mOutputs); | 
|  | 4689 | for (size_t i = 0; i < outputs.size(); i++) { | 
|  | 4690 | sp<AudioOutputDescriptor> outputDesc = mOutputs.valueFor(outputs[i]); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 4691 | if (outputDesc->isStreamActive((audio_stream_type_t)curStream)) { | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4692 | curDevices |= outputDesc->device(); | 
|  | 4693 | } | 
|  | 4694 | } | 
|  | 4695 | devices |= curDevices; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4696 | } | 
| Jon Eklund | 11c9fb1 | 2014-06-23 14:47:03 -0500 | [diff] [blame] | 4697 |  | 
|  | 4698 | /*Filter SPEAKER_SAFE out of results, as AudioService doesn't know about it | 
|  | 4699 | and doesn't really need to.*/ | 
|  | 4700 | if (devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) { | 
|  | 4701 | devices |= AUDIO_DEVICE_OUT_SPEAKER; | 
|  | 4702 | devices &= ~AUDIO_DEVICE_OUT_SPEAKER_SAFE; | 
|  | 4703 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4704 | return devices; | 
|  | 4705 | } | 
|  | 4706 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4707 | routing_strategy AudioPolicyManager::getStrategy(audio_stream_type_t stream) const | 
|  | 4708 | { | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 4709 | ALOG_ASSERT(stream != AUDIO_STREAM_PATCH,"getStrategy() called for AUDIO_STREAM_PATCH"); | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4710 | return mEngine->getStrategyForStream(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4711 | } | 
|  | 4712 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4713 | uint32_t AudioPolicyManager::getStrategyForAttr(const audio_attributes_t *attr) { | 
|  | 4714 | // flags to strategy mapping | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4715 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 4716 | return (uint32_t) STRATEGY_TRANSMITTED_THROUGH_SPEAKER; | 
|  | 4717 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4718 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 4719 | return (uint32_t) STRATEGY_ENFORCED_AUDIBLE; | 
|  | 4720 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4721 | // usage to strategy mapping | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4722 | return static_cast<uint32_t>(mEngine->getStrategyForUsage(attr->usage)); | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 4723 | } | 
|  | 4724 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4725 | void AudioPolicyManager::handleNotificationRoutingForStream(audio_stream_type_t stream) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4726 | switch(stream) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4727 | case AUDIO_STREAM_MUSIC: | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4728 | checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL); | 
|  | 4729 | updateDevicesAndOutputs(); | 
|  | 4730 | break; | 
|  | 4731 | default: | 
|  | 4732 | break; | 
|  | 4733 | } | 
|  | 4734 | } | 
|  | 4735 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4736 | uint32_t AudioPolicyManager::handleEventForBeacon(int event) { | 
| Eric Laurent | 9459fb0 | 2015-08-12 18:36:32 -0700 | [diff] [blame] | 4737 |  | 
|  | 4738 | // skip beacon mute management if a dedicated TTS output is available | 
|  | 4739 | if (mTtsOutputAvailable) { | 
|  | 4740 | return 0; | 
|  | 4741 | } | 
|  | 4742 |  | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4743 | switch(event) { | 
|  | 4744 | case STARTING_OUTPUT: | 
|  | 4745 | mBeaconMuteRefCount++; | 
|  | 4746 | break; | 
|  | 4747 | case STOPPING_OUTPUT: | 
|  | 4748 | if (mBeaconMuteRefCount > 0) { | 
|  | 4749 | mBeaconMuteRefCount--; | 
|  | 4750 | } | 
|  | 4751 | break; | 
|  | 4752 | case STARTING_BEACON: | 
|  | 4753 | mBeaconPlayingRefCount++; | 
|  | 4754 | break; | 
|  | 4755 | case STOPPING_BEACON: | 
|  | 4756 | if (mBeaconPlayingRefCount > 0) { | 
|  | 4757 | mBeaconPlayingRefCount--; | 
|  | 4758 | } | 
|  | 4759 | break; | 
|  | 4760 | } | 
|  | 4761 |  | 
|  | 4762 | if (mBeaconMuteRefCount > 0) { | 
|  | 4763 | // any playback causes beacon to be muted | 
|  | 4764 | return setBeaconMute(true); | 
|  | 4765 | } else { | 
|  | 4766 | // no other playback: unmute when beacon starts playing, mute when it stops | 
|  | 4767 | return setBeaconMute(mBeaconPlayingRefCount == 0); | 
|  | 4768 | } | 
|  | 4769 | } | 
|  | 4770 |  | 
|  | 4771 | uint32_t AudioPolicyManager::setBeaconMute(bool mute) { | 
|  | 4772 | ALOGV("setBeaconMute(%d) mBeaconMuteRefCount=%d mBeaconPlayingRefCount=%d", | 
|  | 4773 | mute, mBeaconMuteRefCount, mBeaconPlayingRefCount); | 
|  | 4774 | // keep track of muted state to avoid repeating mute/unmute operations | 
|  | 4775 | if (mBeaconMuted != mute) { | 
|  | 4776 | // mute/unmute AUDIO_STREAM_TTS on all outputs | 
|  | 4777 | ALOGV("\t muting %d", mute); | 
|  | 4778 | uint32_t maxLatency = 0; | 
|  | 4779 | for (size_t i = 0; i < mOutputs.size(); i++) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4780 | sp<SwAudioOutputDescriptor> desc = mOutputs.valueAt(i); | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4781 | setStreamMute(AUDIO_STREAM_TTS, mute/*on*/, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4782 | desc, | 
| Jean-Michel Trivi | d9cfeb4 | 2014-09-22 16:51:34 -0700 | [diff] [blame] | 4783 | 0 /*delay*/, AUDIO_DEVICE_NONE); | 
|  | 4784 | const uint32_t latency = desc->latency() * 2; | 
|  | 4785 | if (latency > maxLatency) { | 
|  | 4786 | maxLatency = latency; | 
|  | 4787 | } | 
|  | 4788 | } | 
|  | 4789 | mBeaconMuted = mute; | 
|  | 4790 | return maxLatency; | 
|  | 4791 | } | 
|  | 4792 | return 0; | 
|  | 4793 | } | 
|  | 4794 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4795 | audio_devices_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 4796 | bool fromCache) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4797 | { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4798 | // Routing | 
|  | 4799 | // see if we have an explicit route | 
|  | 4800 | // scan the whole RouteMap, for each entry, convert the stream type to a strategy | 
|  | 4801 | // (getStrategy(stream)). | 
|  | 4802 | // if the strategy from the stream type in the RouteMap is the same as the argument above, | 
|  | 4803 | // and activity count is non-zero | 
|  | 4804 | // the device = the device from the descriptor in the RouteMap, and exit. | 
|  | 4805 | for (size_t routeIndex = 0; routeIndex < mOutputRoutes.size(); routeIndex++) { | 
|  | 4806 | sp<SessionRoute> route = mOutputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 28d09f0 | 2016-03-08 10:43:05 -0800 | [diff] [blame] | 4807 | routing_strategy routeStrategy = getStrategy(route->mStreamType); | 
|  | 4808 | if ((routeStrategy == strategy) && route->isActive()) { | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4809 | return route->mDeviceDescriptor->type(); | 
|  | 4810 | } | 
|  | 4811 | } | 
|  | 4812 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4813 | if (fromCache) { | 
|  | 4814 | ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x", | 
|  | 4815 | strategy, mDeviceForStrategy[strategy]); | 
|  | 4816 | return mDeviceForStrategy[strategy]; | 
|  | 4817 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 4818 | return mEngine->getDeviceForStrategy(strategy); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4819 | } | 
|  | 4820 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 4821 | void AudioPolicyManager::updateDevicesAndOutputs() | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4822 | { | 
|  | 4823 | for (int i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4824 | mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
|  | 4825 | } | 
|  | 4826 | mPreviousOutputs = mOutputs; | 
|  | 4827 | } | 
|  | 4828 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 4829 | uint32_t AudioPolicyManager::checkDeviceMuteStrategies(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4830 | audio_devices_t prevDevice, | 
|  | 4831 | uint32_t delayMs) | 
|  | 4832 | { | 
|  | 4833 | // mute/unmute strategies using an incompatible device combination | 
|  | 4834 | // if muting, wait for the audio in pcm buffer to be drained before proceeding | 
|  | 4835 | // if unmuting, unmute only after the specified delay | 
|  | 4836 | if (outputDesc->isDuplicated()) { | 
|  | 4837 | return 0; | 
|  | 4838 | } | 
|  | 4839 |  | 
|  | 4840 | uint32_t muteWaitMs = 0; | 
|  | 4841 | audio_devices_t device = outputDesc->device(); | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 4842 | bool shouldMute = outputDesc->isActive() && (popcount(device) >= 2); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4843 |  | 
|  | 4844 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
|  | 4845 | audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4846 | curDevice = curDevice & outputDesc->supportedDevices(); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4847 | bool mute = shouldMute && (curDevice & device) && (curDevice != device); | 
|  | 4848 | bool doMute = false; | 
|  | 4849 |  | 
|  | 4850 | if (mute && !outputDesc->mStrategyMutedByDevice[i]) { | 
|  | 4851 | doMute = true; | 
|  | 4852 | outputDesc->mStrategyMutedByDevice[i] = true; | 
|  | 4853 | } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){ | 
|  | 4854 | doMute = true; | 
|  | 4855 | outputDesc->mStrategyMutedByDevice[i] = false; | 
|  | 4856 | } | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4857 | if (doMute) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4858 | for (size_t j = 0; j < mOutputs.size(); j++) { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 4859 | sp<AudioOutputDescriptor> desc = mOutputs.valueAt(j); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4860 | // skip output if it does not share any device with current output | 
|  | 4861 | if ((desc->supportedDevices() & outputDesc->supportedDevices()) | 
|  | 4862 | == AUDIO_DEVICE_NONE) { | 
|  | 4863 | continue; | 
|  | 4864 | } | 
| Eric Laurent | 37ddbb4 | 2016-08-10 16:19:14 -0700 | [diff] [blame] | 4865 | ALOGVV("checkDeviceMuteStrategies() %s strategy %zu (curDevice %04x)", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4866 | mute ? "muting" : "unmuting", i, curDevice); | 
|  | 4867 | setStrategyMute((routing_strategy)i, mute, desc, mute ? 0 : delayMs); | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4868 | if (isStrategyActive(desc, (routing_strategy)i)) { | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4869 | if (mute) { | 
|  | 4870 | // FIXME: should not need to double latency if volume could be applied | 
|  | 4871 | // immediately by the audioflinger mixer. We must account for the delay | 
|  | 4872 | // between now and the next time the audioflinger thread for this output | 
|  | 4873 | // will process a buffer (which corresponds to one buffer size, | 
|  | 4874 | // usually 1/2 or 1/4 of the latency). | 
|  | 4875 | if (muteWaitMs < desc->latency() * 2) { | 
|  | 4876 | muteWaitMs = desc->latency() * 2; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4877 | } | 
|  | 4878 | } | 
|  | 4879 | } | 
|  | 4880 | } | 
|  | 4881 | } | 
|  | 4882 | } | 
|  | 4883 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4884 | // temporary mute output if device selection changes to avoid volume bursts due to | 
|  | 4885 | // different per device volumes | 
|  | 4886 | if (outputDesc->isActive() && (device != prevDevice)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4887 | uint32_t tempMuteWaitMs = outputDesc->latency() * 2; | 
|  | 4888 | // temporary mute duration is conservatively set to 4 times the reported latency | 
|  | 4889 | uint32_t tempMuteDurationMs = outputDesc->latency() * 4; | 
|  | 4890 | if (muteWaitMs < tempMuteWaitMs) { | 
|  | 4891 | muteWaitMs = tempMuteWaitMs; | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4892 | } | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4893 |  | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4894 | for (size_t i = 0; i < NUM_STRATEGIES; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 4895 | if (isStrategyActive(outputDesc, (routing_strategy)i)) { | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4896 | // make sure that we do not start the temporary mute period too early in case of | 
|  | 4897 | // delayed device change | 
|  | 4898 | setStrategyMute((routing_strategy)i, true, outputDesc, delayMs); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4899 | setStrategyMute((routing_strategy)i, false, outputDesc, | 
| Eric Laurent | dc46286 | 2016-07-19 12:29:53 -0700 | [diff] [blame] | 4900 | delayMs + tempMuteDurationMs, device); | 
| Eric Laurent | 9940113 | 2014-05-07 19:48:15 -0700 | [diff] [blame] | 4901 | } | 
|  | 4902 | } | 
|  | 4903 | } | 
|  | 4904 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4905 | // wait for the PCM output buffers to empty before proceeding with the rest of the command | 
|  | 4906 | if (muteWaitMs > delayMs) { | 
|  | 4907 | muteWaitMs -= delayMs; | 
|  | 4908 | usleep(muteWaitMs * 1000); | 
|  | 4909 | return muteWaitMs; | 
|  | 4910 | } | 
|  | 4911 | return 0; | 
|  | 4912 | } | 
|  | 4913 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4914 | uint32_t AudioPolicyManager::setOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4915 | audio_devices_t device, | 
|  | 4916 | bool force, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4917 | int delayMs, | 
| Jean-Michel Trivi | 0fb4775 | 2014-07-22 16:19:14 -0700 | [diff] [blame] | 4918 | audio_patch_handle_t *patchHandle, | 
|  | 4919 | const char* address) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4920 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4921 | ALOGV("setOutputDevice() device %04x delayMs %d", device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4922 | AudioParameter param; | 
|  | 4923 | uint32_t muteWaitMs; | 
|  | 4924 |  | 
|  | 4925 | if (outputDesc->isDuplicated()) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4926 | muteWaitMs = setOutputDevice(outputDesc->subOutput1(), device, force, delayMs); | 
|  | 4927 | muteWaitMs += setOutputDevice(outputDesc->subOutput2(), device, force, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4928 | return muteWaitMs; | 
|  | 4929 | } | 
|  | 4930 | // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current | 
|  | 4931 | // output profile | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4932 | if ((device != AUDIO_DEVICE_NONE) && | 
|  | 4933 | ((device & outputDesc->supportedDevices()) == 0)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4934 | return 0; | 
|  | 4935 | } | 
|  | 4936 |  | 
|  | 4937 | // filter devices according to output selected | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4938 | device = (audio_devices_t)(device & outputDesc->supportedDevices()); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4939 |  | 
|  | 4940 | audio_devices_t prevDevice = outputDesc->mDevice; | 
|  | 4941 |  | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4942 | ALOGV("setOutputDevice() prevDevice 0x%04x", prevDevice); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4943 |  | 
|  | 4944 | if (device != AUDIO_DEVICE_NONE) { | 
|  | 4945 | outputDesc->mDevice = device; | 
|  | 4946 | } | 
|  | 4947 | muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs); | 
|  | 4948 |  | 
|  | 4949 | // Do not change the routing if: | 
| Eric Laurent | b80a2a8 | 2014-10-27 16:07:59 -0700 | [diff] [blame] | 4950 | //      the requested device is AUDIO_DEVICE_NONE | 
|  | 4951 | //      OR the requested device is the same as current device | 
|  | 4952 | //  AND force is not specified | 
|  | 4953 | //  AND the output is connected by a valid audio patch. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4954 | // Doing this check here allows the caller to call setOutputDevice() without conditions | 
| Paul McLean | aa98119 | 2015-03-21 09:55:15 -0700 | [diff] [blame] | 4955 | if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && | 
|  | 4956 | !force && | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4957 | outputDesc->getPatchHandle() != 0) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4958 | ALOGV("setOutputDevice() setting same device 0x%04x or null device", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4959 | return muteWaitMs; | 
|  | 4960 | } | 
|  | 4961 |  | 
|  | 4962 | ALOGV("setOutputDevice() changing device"); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4963 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 4964 | // do the routing | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4965 | if (device == AUDIO_DEVICE_NONE) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 4966 | resetOutputDevice(outputDesc, delayMs, NULL); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4967 | } else { | 
| Eric Laurent | c40d969 | 2016-04-13 19:14:13 -0700 | [diff] [blame] | 4968 | DeviceVector deviceList; | 
|  | 4969 | if ((address == NULL) || (strlen(address) == 0)) { | 
|  | 4970 | deviceList = mAvailableOutputDevices.getDevicesFromType(device); | 
|  | 4971 | } else { | 
|  | 4972 | deviceList = mAvailableOutputDevices.getDevicesFromTypeAddr(device, String8(address)); | 
|  | 4973 | } | 
|  | 4974 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4975 | if (!deviceList.isEmpty()) { | 
|  | 4976 | struct audio_patch patch; | 
|  | 4977 | outputDesc->toAudioPortConfig(&patch.sources[0]); | 
|  | 4978 | patch.num_sources = 1; | 
|  | 4979 | patch.num_sinks = 0; | 
|  | 4980 | for (size_t i = 0; i < deviceList.size() && i < AUDIO_PATCH_PORTS_MAX; i++) { | 
|  | 4981 | deviceList.itemAt(i)->toAudioPortConfig(&patch.sinks[i]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4982 | patch.num_sinks++; | 
|  | 4983 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4984 | ssize_t index; | 
|  | 4985 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 4986 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 4987 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 4988 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4989 | } | 
|  | 4990 | sp< AudioPatch> patchDesc; | 
|  | 4991 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 4992 | if (index >= 0) { | 
|  | 4993 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 4994 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 4995 | } | 
|  | 4996 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 4997 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 4998 | &afPatchHandle, | 
|  | 4999 | delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5000 | ALOGV("setOutputDevice() createAudioPatch returned %d patchHandle %d" | 
|  | 5001 | "num_sources %d num_sinks %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5002 | status, afPatchHandle, patch.num_sources, patch.num_sinks); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5003 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5004 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5005 | patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5006 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 5007 | } else { | 
|  | 5008 | patchDesc->mPatch = patch; | 
|  | 5009 | } | 
|  | 5010 | patchDesc->mAfPatchHandle = afPatchHandle; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5011 | if (patchHandle) { | 
|  | 5012 | *patchHandle = patchDesc->mHandle; | 
|  | 5013 | } | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5014 | outputDesc->setPatchHandle(patchDesc->mHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5015 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5016 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5017 | } | 
|  | 5018 | } | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5019 |  | 
|  | 5020 | // inform all input as well | 
|  | 5021 | for (size_t i = 0; i < mInputs.size(); i++) { | 
|  | 5022 | const sp<AudioInputDescriptor>  inputDescriptor = mInputs.valueAt(i); | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5023 | if (!is_virtual_input_device(inputDescriptor->mDevice)) { | 
| bryant_liu | f5e7e79 | 2014-08-19 20:07:05 +0800 | [diff] [blame] | 5024 | AudioParameter inputCmd = AudioParameter(); | 
|  | 5025 | ALOGV("%s: inform input %d of device:%d", __func__, | 
|  | 5026 | inputDescriptor->mIoHandle, device); | 
|  | 5027 | inputCmd.addInt(String8(AudioParameter::keyRouting),device); | 
|  | 5028 | mpClientInterface->setParameters(inputDescriptor->mIoHandle, | 
|  | 5029 | inputCmd.toString(), | 
|  | 5030 | delayMs); | 
|  | 5031 | } | 
|  | 5032 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5033 | } | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5034 |  | 
|  | 5035 | // update stream volumes according to new device | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5036 | applyStreamVolumes(outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5037 |  | 
|  | 5038 | return muteWaitMs; | 
|  | 5039 | } | 
|  | 5040 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5041 | status_t AudioPolicyManager::resetOutputDevice(const sp<AudioOutputDescriptor>& outputDesc, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5042 | int delayMs, | 
|  | 5043 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5044 | { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5045 | ssize_t index; | 
|  | 5046 | if (patchHandle) { | 
|  | 5047 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 5048 | } else { | 
| Jean-Michel Trivi | ff155c6 | 2016-02-26 12:07:16 -0800 | [diff] [blame] | 5049 | index = mAudioPatches.indexOfKey(outputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5050 | } | 
|  | 5051 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5052 | return INVALID_OPERATION; | 
|  | 5053 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5054 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 5055 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, delayMs); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5056 | ALOGV("resetOutputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5057 | outputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5058 | removeAudioPatch(patchDesc->mHandle); | 
|  | 5059 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5060 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5061 | return status; | 
|  | 5062 | } | 
|  | 5063 |  | 
|  | 5064 | status_t AudioPolicyManager::setInputDevice(audio_io_handle_t input, | 
|  | 5065 | audio_devices_t device, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5066 | bool force, | 
|  | 5067 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5068 | { | 
|  | 5069 | status_t status = NO_ERROR; | 
|  | 5070 |  | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5071 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5072 | if ((device != AUDIO_DEVICE_NONE) && ((device != inputDesc->mDevice) || force)) { | 
|  | 5073 | inputDesc->mDevice = device; | 
|  | 5074 |  | 
|  | 5075 | DeviceVector deviceList = mAvailableInputDevices.getDevicesFromType(device); | 
|  | 5076 | if (!deviceList.isEmpty()) { | 
|  | 5077 | struct audio_patch patch; | 
|  | 5078 | inputDesc->toAudioPortConfig(&patch.sinks[0]); | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5079 | // AUDIO_SOURCE_HOTWORD is for internal use only: | 
|  | 5080 | // handled as AUDIO_SOURCE_VOICE_RECOGNITION by the audio HAL | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 5081 | if (patch.sinks[0].ext.mix.usecase.source == AUDIO_SOURCE_HOTWORD && | 
| Eric Laurent | 599c758 | 2015-12-07 18:05:55 -0800 | [diff] [blame] | 5082 | !inputDesc->isSoundTrigger()) { | 
| Eric Laurent | daf92cc | 2014-07-22 15:36:10 -0700 | [diff] [blame] | 5083 | patch.sinks[0].ext.mix.usecase.source = AUDIO_SOURCE_VOICE_RECOGNITION; | 
|  | 5084 | } | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5085 | patch.num_sinks = 1; | 
|  | 5086 | //only one input device for now | 
|  | 5087 | deviceList.itemAt(0)->toAudioPortConfig(&patch.sources[0]); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5088 | patch.num_sources = 1; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5089 | ssize_t index; | 
|  | 5090 | if (patchHandle && *patchHandle != AUDIO_PATCH_HANDLE_NONE) { | 
|  | 5091 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 5092 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5093 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5094 | } | 
|  | 5095 | sp< AudioPatch> patchDesc; | 
|  | 5096 | audio_patch_handle_t afPatchHandle = AUDIO_PATCH_HANDLE_NONE; | 
|  | 5097 | if (index >= 0) { | 
|  | 5098 | patchDesc = mAudioPatches.valueAt(index); | 
|  | 5099 | afPatchHandle = patchDesc->mAfPatchHandle; | 
|  | 5100 | } | 
|  | 5101 |  | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5102 | status_t status = mpClientInterface->createAudioPatch(&patch, | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5103 | &afPatchHandle, | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5104 | 0); | 
|  | 5105 | ALOGV("setInputDevice() createAudioPatch returned %d patchHandle %d", | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5106 | status, afPatchHandle); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5107 | if (status == NO_ERROR) { | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5108 | if (index < 0) { | 
| François Gaffie | 98cc191 | 2015-03-18 17:52:40 +0100 | [diff] [blame] | 5109 | patchDesc = new AudioPatch(&patch, mUidCached); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5110 | addAudioPatch(patchDesc->mHandle, patchDesc); | 
|  | 5111 | } else { | 
|  | 5112 | patchDesc->mPatch = patch; | 
|  | 5113 | } | 
|  | 5114 | patchDesc->mAfPatchHandle = afPatchHandle; | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5115 | if (patchHandle) { | 
|  | 5116 | *patchHandle = patchDesc->mHandle; | 
|  | 5117 | } | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5118 | inputDesc->setPatchHandle(patchDesc->mHandle); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5119 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5120 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5121 | } | 
|  | 5122 | } | 
|  | 5123 | } | 
|  | 5124 | return status; | 
|  | 5125 | } | 
|  | 5126 |  | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5127 | status_t AudioPolicyManager::resetInputDevice(audio_io_handle_t input, | 
|  | 5128 | audio_patch_handle_t *patchHandle) | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5129 | { | 
| Eric Laurent | 1f2f223 | 2014-06-02 12:01:23 -0700 | [diff] [blame] | 5130 | sp<AudioInputDescriptor> inputDesc = mInputs.valueFor(input); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5131 | ssize_t index; | 
|  | 5132 | if (patchHandle) { | 
|  | 5133 | index = mAudioPatches.indexOfKey(*patchHandle); | 
|  | 5134 | } else { | 
| Jean-Michel Trivi | 8c7cf3b | 2016-02-25 17:08:24 -0800 | [diff] [blame] | 5135 | index = mAudioPatches.indexOfKey(inputDesc->getPatchHandle()); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5136 | } | 
|  | 5137 | if (index < 0) { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5138 | return INVALID_OPERATION; | 
|  | 5139 | } | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5140 | sp< AudioPatch> patchDesc = mAudioPatches.valueAt(index); | 
|  | 5141 | status_t status = mpClientInterface->releaseAudioPatch(patchDesc->mAfPatchHandle, 0); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5142 | ALOGV("resetInputDevice() releaseAudioPatch returned %d", status); | 
| Glenn Kasten | a13cde9 | 2016-03-28 15:26:02 -0700 | [diff] [blame] | 5143 | inputDesc->setPatchHandle(AUDIO_PATCH_HANDLE_NONE); | 
| Eric Laurent | 6a94d69 | 2014-05-20 11:18:06 -0700 | [diff] [blame] | 5144 | removeAudioPatch(patchDesc->mHandle); | 
|  | 5145 | nextAudioPortGeneration(); | 
| Eric Laurent | b52c152 | 2014-05-20 11:27:36 -0700 | [diff] [blame] | 5146 | mpClientInterface->onAudioPatchListUpdate(); | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5147 | return status; | 
|  | 5148 | } | 
|  | 5149 |  | 
| Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 5150 | sp<IOProfile> AudioPolicyManager::getInputProfile(audio_devices_t device, | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5151 | const String8& address, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5152 | uint32_t& samplingRate, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5153 | audio_format_t& format, | 
|  | 5154 | audio_channel_mask_t& channelMask, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5155 | audio_input_flags_t flags) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5156 | { | 
|  | 5157 | // Choose an input profile based on the requested capture parameters: select the first available | 
|  | 5158 | // profile supporting all requested parameters. | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5159 | // | 
|  | 5160 | // TODO: perhaps isCompatibleProfile should return a "matching" score so we can return | 
|  | 5161 | // the best matching profile, not the first one. | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5162 |  | 
|  | 5163 | for (size_t i = 0; i < mHwModules.size(); i++) | 
|  | 5164 | { | 
|  | 5165 | if (mHwModules[i]->mHandle == 0) { | 
|  | 5166 | continue; | 
|  | 5167 | } | 
|  | 5168 | for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++) | 
|  | 5169 | { | 
| Eric Laurent | 1c333e2 | 2014-05-20 10:48:17 -0700 | [diff] [blame] | 5170 | sp<IOProfile> profile = mHwModules[i]->mInputProfiles[j]; | 
| Eric Laurent | d469296 | 2014-05-05 18:13:44 -0700 | [diff] [blame] | 5171 | // profile->log(); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5172 | if (profile->isCompatibleProfile(device, address, samplingRate, | 
| Glenn Kasten | cbd4802 | 2014-07-24 13:46:44 -0700 | [diff] [blame] | 5173 | &samplingRate /*updatedSamplingRate*/, | 
| Andy Hung | f129b03 | 2015-04-07 13:45:50 -0700 | [diff] [blame] | 5174 | format, | 
|  | 5175 | &format /*updatedFormat*/, | 
|  | 5176 | channelMask, | 
|  | 5177 | &channelMask /*updatedChannelMask*/, | 
|  | 5178 | (audio_output_flags_t) flags)) { | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5179 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5180 | return profile; | 
|  | 5181 | } | 
|  | 5182 | } | 
|  | 5183 | } | 
|  | 5184 | return NULL; | 
|  | 5185 | } | 
|  | 5186 |  | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5187 |  | 
|  | 5188 | audio_devices_t AudioPolicyManager::getDeviceAndMixForInputSource(audio_source_t inputSource, | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5189 | AudioMix **policyMix) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5190 | { | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5191 | audio_devices_t availableDeviceTypes = mAvailableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN; | 
|  | 5192 | audio_devices_t selectedDeviceFromMix = | 
|  | 5193 | mPolicyMixes.getDeviceAndMixForInputSource(inputSource, availableDeviceTypes, policyMix); | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5194 |  | 
| François Gaffie | 036e1e9 | 2015-03-19 10:16:24 +0100 | [diff] [blame] | 5195 | if (selectedDeviceFromMix != AUDIO_DEVICE_NONE) { | 
|  | 5196 | return selectedDeviceFromMix; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5197 | } | 
| Eric Laurent | c73ca6e | 2014-12-12 14:34:22 -0800 | [diff] [blame] | 5198 | return getDeviceForInputSource(inputSource); | 
|  | 5199 | } | 
|  | 5200 |  | 
|  | 5201 | audio_devices_t AudioPolicyManager::getDeviceForInputSource(audio_source_t inputSource) | 
|  | 5202 | { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5203 | for (size_t routeIndex = 0; routeIndex < mInputRoutes.size(); routeIndex++) { | 
|  | 5204 | sp<SessionRoute> route = mInputRoutes.valueAt(routeIndex); | 
| Eric Laurent | 8c7e6da | 2015-04-21 17:37:00 -0700 | [diff] [blame] | 5205 | if (inputSource == route->mSource && route->isActive()) { | 
| Paul McLean | 466dc8e | 2015-04-17 13:15:36 -0600 | [diff] [blame] | 5206 | return route->mDeviceDescriptor->type(); | 
|  | 5207 | } | 
|  | 5208 | } | 
|  | 5209 |  | 
|  | 5210 | return mEngine->getDeviceForInputSource(inputSource); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5211 | } | 
|  | 5212 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5213 | float AudioPolicyManager::computeVolume(audio_stream_type_t stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5214 | int index, | 
|  | 5215 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5216 | { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5217 | float volumeDB = mVolumeCurves->volIndexToDb(stream, Volume::getDeviceCategory(device), index); | 
| Jean-Michel Trivi | 3d8b4a4 | 2016-09-14 18:37:46 -0700 | [diff] [blame] | 5218 |  | 
|  | 5219 | // handle the case of accessibility active while a ringtone is playing: if the ringtone is much | 
|  | 5220 | // louder than the accessibility prompt, the prompt cannot be heard, thus masking the touch | 
|  | 5221 | // exploration of the dialer UI. In this situation, bring the accessibility volume closer to | 
|  | 5222 | // the ringtone volume | 
|  | 5223 | if ((stream == AUDIO_STREAM_ACCESSIBILITY) | 
|  | 5224 | && (AUDIO_MODE_RINGTONE == mEngine->getPhoneState()) | 
|  | 5225 | && isStreamActive(AUDIO_STREAM_RING, 0)) { | 
|  | 5226 | const float ringVolumeDB = computeVolume(AUDIO_STREAM_RING, index, device); | 
|  | 5227 | return ringVolumeDB - 4 > volumeDB ? ringVolumeDB - 4 : volumeDB; | 
|  | 5228 | } | 
|  | 5229 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5230 | // if a headset is connected, apply the following rules to ring tones and notifications | 
|  | 5231 | // to avoid sound level bursts in user's ears: | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5232 | // - always attenuate notifications volume by 6dB | 
|  | 5233 | // - attenuate ring tones volume by 6dB unless music is not playing and | 
|  | 5234 | // speaker is part of the select devices | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5235 | // - if music is playing, always limit the volume to current music volume, | 
|  | 5236 | // with a minimum threshold at -36dB so that notification is always perceived. | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5237 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5238 | if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 5239 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | | 
|  | 5240 | AUDIO_DEVICE_OUT_WIRED_HEADSET | | 
| Eric Laurent | 904d632 | 2017-03-17 17:20:47 -0700 | [diff] [blame] | 5241 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | | 
|  | 5242 | AUDIO_DEVICE_OUT_USB_HEADSET)) && | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5243 | ((stream_strategy == STRATEGY_SONIFICATION) | 
|  | 5244 | || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL) | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5245 | || (stream == AUDIO_STREAM_SYSTEM) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5246 | || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) && | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5247 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) && | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5248 | mVolumeCurves->canBeMuted(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5249 | // when the phone is ringing we must consider that music could have been paused just before | 
|  | 5250 | // by the music application and behave as if music was active if the last music track was | 
|  | 5251 | // just stopped | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5252 | if (isStreamActive(AUDIO_STREAM_MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) || | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5253 | mLimitRingtoneVolume) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5254 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5255 | audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5256 | float musicVolDB = computeVolume(AUDIO_STREAM_MUSIC, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5257 | mVolumeCurves->getVolumeIndex(AUDIO_STREAM_MUSIC, | 
|  | 5258 | musicDevice), | 
|  | 5259 | musicDevice); | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5260 | float minVolDB = (musicVolDB > SONIFICATION_HEADSET_VOLUME_MIN_DB) ? | 
|  | 5261 | musicVolDB : SONIFICATION_HEADSET_VOLUME_MIN_DB; | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5262 | if (volumeDB > minVolDB) { | 
|  | 5263 | volumeDB = minVolDB; | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5264 | ALOGV("computeVolume limiting volume to %f musicVol %f", minVolDB, musicVolDB); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5265 | } | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5266 | if (device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP | | 
|  | 5267 | AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES)) { | 
|  | 5268 | // on A2DP, also ensure notification volume is not too low compared to media when | 
|  | 5269 | // intended to be played | 
|  | 5270 | if ((volumeDB > -96.0f) && | 
|  | 5271 | (musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB > volumeDB)) { | 
|  | 5272 | ALOGV("computeVolume increasing volume for stream=%d device=0x%X from %f to %f", | 
|  | 5273 | stream, device, | 
|  | 5274 | volumeDB, musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB); | 
|  | 5275 | volumeDB = musicVolDB - SONIFICATION_A2DP_MAX_MEDIA_DIFF_DB; | 
|  | 5276 | } | 
|  | 5277 | } | 
| Eric Laurent | 6af1c1d | 2016-04-14 11:20:44 -0700 | [diff] [blame] | 5278 | } else if ((Volume::getDeviceForVolume(device) != AUDIO_DEVICE_OUT_SPEAKER) || | 
|  | 5279 | stream_strategy != STRATEGY_SONIFICATION) { | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5280 | volumeDB += SONIFICATION_HEADSET_VOLUME_FACTOR_DB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5281 | } | 
|  | 5282 | } | 
|  | 5283 |  | 
| Jean-Michel Trivi | 00a2096 | 2016-05-25 19:11:01 -0700 | [diff] [blame] | 5284 | return volumeDB; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5285 | } | 
|  | 5286 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5287 | status_t AudioPolicyManager::checkAndSetVolume(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5288 | int index, | 
|  | 5289 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5290 | audio_devices_t device, | 
|  | 5291 | int delayMs, | 
|  | 5292 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5293 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5294 | // do not change actual stream volume if the stream is muted | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5295 | if (outputDesc->mMuteCount[stream] != 0) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5296 | ALOGVV("checkAndSetVolume() stream %d muted count %d", | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5297 | stream, outputDesc->mMuteCount[stream]); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5298 | return NO_ERROR; | 
|  | 5299 | } | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5300 | audio_policy_forced_cfg_t forceUseForComm = | 
|  | 5301 | mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5302 | // do not change in call volume if bluetooth is connected and vice versa | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5303 | if ((stream == AUDIO_STREAM_VOICE_CALL && forceUseForComm == AUDIO_POLICY_FORCE_BT_SCO) || | 
|  | 5304 | (stream == AUDIO_STREAM_BLUETOOTH_SCO && forceUseForComm != AUDIO_POLICY_FORCE_BT_SCO)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5305 | ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm", | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5306 | stream, forceUseForComm); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5307 | return INVALID_OPERATION; | 
|  | 5308 | } | 
|  | 5309 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5310 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5311 | device = outputDesc->device(); | 
|  | 5312 | } | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5313 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5314 | float volumeDb = computeVolume(stream, index, device); | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5315 | if (outputDesc->isFixedVolume(device)) { | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5316 | volumeDb = 0.0f; | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5317 | } | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5318 |  | 
| Eric Laurent | ffbc80f | 2015-03-18 18:30:19 -0700 | [diff] [blame] | 5319 | outputDesc->setVolume(volumeDb, stream, device, delayMs, force); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5320 |  | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5321 | if (stream == AUDIO_STREAM_VOICE_CALL || | 
|  | 5322 | stream == AUDIO_STREAM_BLUETOOTH_SCO) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5323 | float voiceVolume; | 
|  | 5324 | // 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] | 5325 | if (stream == AUDIO_STREAM_VOICE_CALL) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5326 | voiceVolume = (float)index/(float)mVolumeCurves->getVolumeIndexMax(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5327 | } else { | 
|  | 5328 | voiceVolume = 1.0; | 
|  | 5329 | } | 
|  | 5330 |  | 
| Eric Laurent | 18fba84 | 2016-03-31 14:41:26 -0700 | [diff] [blame] | 5331 | if (voiceVolume != mLastVoiceVolume) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5332 | mpClientInterface->setVoiceVolume(voiceVolume, delayMs); | 
|  | 5333 | mLastVoiceVolume = voiceVolume; | 
|  | 5334 | } | 
|  | 5335 | } | 
|  | 5336 |  | 
|  | 5337 | return NO_ERROR; | 
|  | 5338 | } | 
|  | 5339 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5340 | void AudioPolicyManager::applyStreamVolumes(const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5341 | audio_devices_t device, | 
|  | 5342 | int delayMs, | 
|  | 5343 | bool force) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5344 | { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5345 | ALOGVV("applyStreamVolumes() for device %08x", device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5346 |  | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5347 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5348 | checkAndSetVolume((audio_stream_type_t)stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5349 | mVolumeCurves->getVolumeIndex((audio_stream_type_t)stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5350 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5351 | device, | 
|  | 5352 | delayMs, | 
|  | 5353 | force); | 
|  | 5354 | } | 
|  | 5355 | } | 
|  | 5356 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5357 | void AudioPolicyManager::setStrategyMute(routing_strategy strategy, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5358 | bool on, | 
|  | 5359 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5360 | int delayMs, | 
|  | 5361 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5362 | { | 
| Eric Laurent | 72249d5 | 2015-06-08 11:12:15 -0700 | [diff] [blame] | 5363 | ALOGVV("setStrategyMute() strategy %d, mute %d, output ID %d", | 
|  | 5364 | strategy, on, outputDesc->getId()); | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5365 | for (int stream = 0; stream < AUDIO_STREAM_FOR_POLICY_CNT; stream++) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5366 | if (getStrategy((audio_stream_type_t)stream) == strategy) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5367 | setStreamMute((audio_stream_type_t)stream, on, outputDesc, delayMs, device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5368 | } | 
|  | 5369 | } | 
|  | 5370 | } | 
|  | 5371 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5372 | void AudioPolicyManager::setStreamMute(audio_stream_type_t stream, | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5373 | bool on, | 
|  | 5374 | const sp<AudioOutputDescriptor>& outputDesc, | 
|  | 5375 | int delayMs, | 
|  | 5376 | audio_devices_t device) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5377 | { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5378 | if (device == AUDIO_DEVICE_NONE) { | 
|  | 5379 | device = outputDesc->device(); | 
|  | 5380 | } | 
|  | 5381 |  | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5382 | ALOGVV("setStreamMute() stream %d, mute %d, mMuteCount %d device %04x", | 
|  | 5383 | stream, on, outputDesc->mMuteCount[stream], device); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5384 |  | 
|  | 5385 | if (on) { | 
|  | 5386 | if (outputDesc->mMuteCount[stream] == 0) { | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5387 | if (mVolumeCurves->canBeMuted(stream) && | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5388 | ((stream != AUDIO_STREAM_ENFORCED_AUDIBLE) || | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5389 | (mEngine->getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_NONE))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5390 | checkAndSetVolume(stream, 0, outputDesc, device, delayMs); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5391 | } | 
|  | 5392 | } | 
|  | 5393 | // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored | 
|  | 5394 | outputDesc->mMuteCount[stream]++; | 
|  | 5395 | } else { | 
|  | 5396 | if (outputDesc->mMuteCount[stream] == 0) { | 
|  | 5397 | ALOGV("setStreamMute() unmuting non muted stream!"); | 
|  | 5398 | return; | 
|  | 5399 | } | 
|  | 5400 | if (--outputDesc->mMuteCount[stream] == 0) { | 
|  | 5401 | checkAndSetVolume(stream, | 
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 5402 | mVolumeCurves->getVolumeIndex(stream, device), | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5403 | outputDesc, | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5404 | device, | 
|  | 5405 | delayMs); | 
|  | 5406 | } | 
|  | 5407 | } | 
|  | 5408 | } | 
|  | 5409 |  | 
| Eric Laurent | e072087 | 2014-03-11 09:30:41 -0700 | [diff] [blame] | 5410 | void AudioPolicyManager::handleIncallSonification(audio_stream_type_t stream, | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5411 | bool starting, bool stateChange) | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5412 | { | 
| Eric Laurent | 87ffa39 | 2015-05-22 10:32:38 -0700 | [diff] [blame] | 5413 | if(!hasPrimaryOutput()) { | 
|  | 5414 | return; | 
|  | 5415 | } | 
|  | 5416 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5417 | // if the stream pertains to sonification strategy and we are in call we must | 
|  | 5418 | // mute the stream if it is low visibility. If it is high visibility, we must play a tone | 
|  | 5419 | // in the device used for phone strategy and play the tone if the selected device does not | 
|  | 5420 | // interfere with the device used for phone strategy | 
|  | 5421 | // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as | 
|  | 5422 | // many times as there are active tracks on the output | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5423 | const routing_strategy stream_strategy = getStrategy(stream); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5424 | if ((stream_strategy == STRATEGY_SONIFICATION) || | 
|  | 5425 | ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) { | 
| Eric Laurent | c75307b | 2015-03-17 15:29:32 -0700 | [diff] [blame] | 5426 | sp<SwAudioOutputDescriptor> outputDesc = mPrimaryOutput; | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5427 | ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d", | 
|  | 5428 | stream, starting, outputDesc->mDevice, stateChange); | 
|  | 5429 | if (outputDesc->mRefCount[stream]) { | 
|  | 5430 | int muteCount = 1; | 
|  | 5431 | if (stateChange) { | 
|  | 5432 | muteCount = outputDesc->mRefCount[stream]; | 
|  | 5433 | } | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5434 | if (audio_is_low_visibility(stream)) { | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5435 | ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount); | 
|  | 5436 | for (int i = 0; i < muteCount; i++) { | 
|  | 5437 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5438 | } | 
|  | 5439 | } else { | 
|  | 5440 | ALOGV("handleIncallSonification() high visibility"); | 
|  | 5441 | if (outputDesc->device() & | 
|  | 5442 | getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) { | 
|  | 5443 | ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount); | 
|  | 5444 | for (int i = 0; i < muteCount; i++) { | 
|  | 5445 | setStreamMute(stream, starting, mPrimaryOutput); | 
|  | 5446 | } | 
|  | 5447 | } | 
|  | 5448 | if (starting) { | 
| Eric Laurent | 3b73df7 | 2014-03-11 09:06:29 -0700 | [diff] [blame] | 5449 | mpClientInterface->startTone(AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION, | 
|  | 5450 | AUDIO_STREAM_VOICE_CALL); | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5451 | } else { | 
|  | 5452 | mpClientInterface->stopTone(); | 
|  | 5453 | } | 
|  | 5454 | } | 
|  | 5455 | } | 
|  | 5456 | } | 
|  | 5457 | } | 
|  | 5458 |  | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5459 | audio_stream_type_t AudioPolicyManager::streamTypefromAttributesInt(const audio_attributes_t *attr) | 
|  | 5460 | { | 
|  | 5461 | // flags to stream type mapping | 
|  | 5462 | if ((attr->flags & AUDIO_FLAG_AUDIBILITY_ENFORCED) == AUDIO_FLAG_AUDIBILITY_ENFORCED) { | 
|  | 5463 | return AUDIO_STREAM_ENFORCED_AUDIBLE; | 
|  | 5464 | } | 
|  | 5465 | if ((attr->flags & AUDIO_FLAG_SCO) == AUDIO_FLAG_SCO) { | 
|  | 5466 | return AUDIO_STREAM_BLUETOOTH_SCO; | 
|  | 5467 | } | 
| Jean-Michel Trivi | 79ad438 | 2015-01-29 10:49:39 -0800 | [diff] [blame] | 5468 | if ((attr->flags & AUDIO_FLAG_BEACON) == AUDIO_FLAG_BEACON) { | 
|  | 5469 | return AUDIO_STREAM_TTS; | 
|  | 5470 | } | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5471 |  | 
|  | 5472 | // usage to stream type mapping | 
|  | 5473 | switch (attr->usage) { | 
|  | 5474 | case AUDIO_USAGE_MEDIA: | 
|  | 5475 | case AUDIO_USAGE_GAME: | 
| Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5476 | case AUDIO_USAGE_ASSISTANT: | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5477 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5478 | return AUDIO_STREAM_MUSIC; | 
| Eric Laurent | 223fd5c | 2014-11-11 13:43:36 -0800 | [diff] [blame] | 5479 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5480 | return AUDIO_STREAM_ACCESSIBILITY; | 
| Jean-Michel Trivi | 5bd3f38 | 2014-06-13 16:06:54 -0700 | [diff] [blame] | 5481 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5482 | return AUDIO_STREAM_SYSTEM; | 
|  | 5483 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5484 | return AUDIO_STREAM_VOICE_CALL; | 
|  | 5485 |  | 
|  | 5486 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5487 | return AUDIO_STREAM_DTMF; | 
|  | 5488 |  | 
|  | 5489 | case AUDIO_USAGE_ALARM: | 
|  | 5490 | return AUDIO_STREAM_ALARM; | 
|  | 5491 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5492 | return AUDIO_STREAM_RING; | 
|  | 5493 |  | 
|  | 5494 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5495 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5496 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5497 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5498 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5499 | return AUDIO_STREAM_NOTIFICATION; | 
|  | 5500 |  | 
|  | 5501 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5502 | default: | 
|  | 5503 | return AUDIO_STREAM_MUSIC; | 
|  | 5504 | } | 
|  | 5505 | } | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5506 |  | 
| François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 5507 | bool AudioPolicyManager::isValidAttributes(const audio_attributes_t *paa) | 
|  | 5508 | { | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5509 | // has flags that map to a strategy? | 
|  | 5510 | if ((paa->flags & (AUDIO_FLAG_AUDIBILITY_ENFORCED | AUDIO_FLAG_SCO | AUDIO_FLAG_BEACON)) != 0) { | 
|  | 5511 | return true; | 
|  | 5512 | } | 
|  | 5513 |  | 
|  | 5514 | // has known usage? | 
|  | 5515 | switch (paa->usage) { | 
|  | 5516 | case AUDIO_USAGE_UNKNOWN: | 
|  | 5517 | case AUDIO_USAGE_MEDIA: | 
|  | 5518 | case AUDIO_USAGE_VOICE_COMMUNICATION: | 
|  | 5519 | case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING: | 
|  | 5520 | case AUDIO_USAGE_ALARM: | 
|  | 5521 | case AUDIO_USAGE_NOTIFICATION: | 
|  | 5522 | case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE: | 
|  | 5523 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST: | 
|  | 5524 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT: | 
|  | 5525 | case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED: | 
|  | 5526 | case AUDIO_USAGE_NOTIFICATION_EVENT: | 
|  | 5527 | case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY: | 
|  | 5528 | case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: | 
|  | 5529 | case AUDIO_USAGE_ASSISTANCE_SONIFICATION: | 
|  | 5530 | case AUDIO_USAGE_GAME: | 
| Eric Laurent | 275e8e9 | 2014-11-30 15:14:47 -0800 | [diff] [blame] | 5531 | case AUDIO_USAGE_VIRTUAL_SOURCE: | 
| Jean-Michel Trivi | 3686776 | 2016-12-29 12:03:28 -0800 | [diff] [blame] | 5532 | case AUDIO_USAGE_ASSISTANT: | 
| Eric Laurent | e83b55d | 2014-11-14 10:06:21 -0800 | [diff] [blame] | 5533 | break; | 
|  | 5534 | default: | 
|  | 5535 | return false; | 
|  | 5536 | } | 
|  | 5537 | return true; | 
|  | 5538 | } | 
|  | 5539 |  | 
| Chih-Hung Hsieh | e964d4e | 2016-08-09 14:31:32 -0700 | [diff] [blame] | 5540 | bool AudioPolicyManager::isStrategyActive(const sp<AudioOutputDescriptor>& outputDesc, | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5541 | routing_strategy strategy, uint32_t inPastMs, | 
|  | 5542 | nsecs_t sysTime) const | 
|  | 5543 | { | 
|  | 5544 | if ((sysTime == 0) && (inPastMs != 0)) { | 
|  | 5545 | sysTime = systemTime(); | 
|  | 5546 | } | 
| Eric Laurent | 794fde2 | 2016-03-11 09:50:45 -0800 | [diff] [blame] | 5547 | for (int i = 0; i < (int)AUDIO_STREAM_FOR_POLICY_CNT; i++) { | 
| François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 5548 | if (((getStrategy((audio_stream_type_t)i) == strategy) || | 
|  | 5549 | (NUM_STRATEGIES == strategy)) && | 
|  | 5550 | outputDesc->isStreamActive((audio_stream_type_t)i, inPastMs, sysTime)) { | 
|  | 5551 | return true; | 
|  | 5552 | } | 
|  | 5553 | } | 
|  | 5554 | return false; | 
|  | 5555 | } | 
|  | 5556 |  | 
| François Gaffie | 2110e04 | 2015-03-24 08:41:51 +0100 | [diff] [blame] | 5557 | audio_policy_forced_cfg_t AudioPolicyManager::getForceUse(audio_policy_force_use_t usage) | 
|  | 5558 | { | 
|  | 5559 | return mEngine->getForceUse(usage); | 
|  | 5560 | } | 
|  | 5561 |  | 
|  | 5562 | bool AudioPolicyManager::isInCall() | 
|  | 5563 | { | 
|  | 5564 | return isStateInCall(mEngine->getPhoneState()); | 
|  | 5565 | } | 
|  | 5566 |  | 
|  | 5567 | bool AudioPolicyManager::isStateInCall(int state) | 
|  | 5568 | { | 
|  | 5569 | return is_state_in_call(state); | 
|  | 5570 | } | 
|  | 5571 |  | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5572 | void AudioPolicyManager::cleanUpForDevice(const sp<DeviceDescriptor>& deviceDesc) | 
|  | 5573 | { | 
|  | 5574 | for (ssize_t i = (ssize_t)mAudioSources.size() - 1; i >= 0; i--)  { | 
|  | 5575 | sp<AudioSourceDescriptor> sourceDesc = mAudioSources.valueAt(i); | 
|  | 5576 | if (sourceDesc->mDevice->equals(deviceDesc)) { | 
|  | 5577 | ALOGV("%s releasing audio source %d", __FUNCTION__, sourceDesc->getHandle()); | 
|  | 5578 | stopAudioSource(sourceDesc->getHandle()); | 
|  | 5579 | } | 
|  | 5580 | } | 
|  | 5581 |  | 
|  | 5582 | for (ssize_t i = (ssize_t)mAudioPatches.size() - 1; i >= 0; i--)  { | 
|  | 5583 | sp<AudioPatch> patchDesc = mAudioPatches.valueAt(i); | 
|  | 5584 | bool release = false; | 
|  | 5585 | for (size_t j = 0; j < patchDesc->mPatch.num_sources && !release; j++)  { | 
|  | 5586 | const struct audio_port_config *source = &patchDesc->mPatch.sources[j]; | 
|  | 5587 | if (source->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5588 | source->ext.device.type == deviceDesc->type()) { | 
|  | 5589 | release = true; | 
|  | 5590 | } | 
|  | 5591 | } | 
|  | 5592 | for (size_t j = 0; j < patchDesc->mPatch.num_sinks && !release; j++)  { | 
|  | 5593 | const struct audio_port_config *sink = &patchDesc->mPatch.sinks[j]; | 
|  | 5594 | if (sink->type == AUDIO_PORT_TYPE_DEVICE && | 
|  | 5595 | sink->ext.device.type == deviceDesc->type()) { | 
|  | 5596 | release = true; | 
|  | 5597 | } | 
|  | 5598 | } | 
|  | 5599 | if (release) { | 
|  | 5600 | ALOGV("%s releasing patch %u", __FUNCTION__, patchDesc->mHandle); | 
|  | 5601 | releaseAudioPatch(patchDesc->mHandle, patchDesc->mUid); | 
|  | 5602 | } | 
|  | 5603 | } | 
|  | 5604 | } | 
|  | 5605 |  | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5606 | // Modify the list of surround sound formats supported. | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5607 | void AudioPolicyManager::filterSurroundFormats(FormatVector *formatsPtr) { | 
|  | 5608 | FormatVector &formats = *formatsPtr; | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5609 | // TODO Set this based on Config properties. | 
|  | 5610 | const bool alwaysForceAC3 = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5611 |  | 
|  | 5612 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5613 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5614 | ALOGD("%s: forced use = %d", __FUNCTION__, forceUse); | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5615 |  | 
|  | 5616 | // Analyze original support for various formats. | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5617 | bool supportsAC3 = false; | 
|  | 5618 | bool supportsOtherSurround = false; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5619 | bool supportsIEC61937 = false; | 
|  | 5620 | for (size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) { | 
|  | 5621 | audio_format_t format = formats[formatIndex]; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5622 | switch (format) { | 
|  | 5623 | case AUDIO_FORMAT_AC3: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5624 | supportsAC3 = true; | 
|  | 5625 | break; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5626 | case AUDIO_FORMAT_E_AC3: | 
|  | 5627 | case AUDIO_FORMAT_DTS: | 
|  | 5628 | case AUDIO_FORMAT_DTS_HD: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5629 | supportsOtherSurround = true; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5630 | break; | 
|  | 5631 | case AUDIO_FORMAT_IEC61937: | 
|  | 5632 | supportsIEC61937 = true; | 
|  | 5633 | break; | 
|  | 5634 | default: | 
|  | 5635 | break; | 
|  | 5636 | } | 
|  | 5637 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5638 |  | 
|  | 5639 | // Modify formats based on surround preferences. | 
|  | 5640 | // If NEVER, remove support for surround formats. | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5641 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5642 | if (supportsAC3 || supportsOtherSurround || supportsIEC61937) { | 
|  | 5643 | // Remove surround sound related formats. | 
|  | 5644 | for (size_t formatIndex = 0; formatIndex < formats.size(); ) { | 
|  | 5645 | audio_format_t format = formats[formatIndex]; | 
|  | 5646 | switch(format) { | 
|  | 5647 | case AUDIO_FORMAT_AC3: | 
|  | 5648 | case AUDIO_FORMAT_E_AC3: | 
|  | 5649 | case AUDIO_FORMAT_DTS: | 
|  | 5650 | case AUDIO_FORMAT_DTS_HD: | 
|  | 5651 | case AUDIO_FORMAT_IEC61937: | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5652 | formats.removeAt(formatIndex); | 
|  | 5653 | break; | 
|  | 5654 | default: | 
|  | 5655 | formatIndex++; // keep it | 
|  | 5656 | break; | 
|  | 5657 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5658 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5659 | supportsAC3 = false; | 
|  | 5660 | supportsOtherSurround = false; | 
|  | 5661 | supportsIEC61937 = false; | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5662 | } | 
| Phil Burk | 07ac114 | 2016-03-25 13:39:29 -0700 | [diff] [blame] | 5663 | } else { // AUTO or ALWAYS | 
|  | 5664 | // Most TVs support AC3 even if they do not report it in the EDID. | 
|  | 5665 | if ((alwaysForceAC3 || (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS)) | 
|  | 5666 | && !supportsAC3) { | 
|  | 5667 | formats.add(AUDIO_FORMAT_AC3); | 
|  | 5668 | supportsAC3 = true; | 
|  | 5669 | } | 
|  | 5670 |  | 
|  | 5671 | // If ALWAYS, add support for raw surround formats if all are missing. | 
|  | 5672 | // This assumes that if any of these formats are reported by the HAL | 
|  | 5673 | // then the report is valid and should not be modified. | 
|  | 5674 | if ((forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) | 
|  | 5675 | && !supportsOtherSurround) { | 
|  | 5676 | formats.add(AUDIO_FORMAT_E_AC3); | 
|  | 5677 | formats.add(AUDIO_FORMAT_DTS); | 
|  | 5678 | formats.add(AUDIO_FORMAT_DTS_HD); | 
|  | 5679 | supportsOtherSurround = true; | 
|  | 5680 | } | 
|  | 5681 |  | 
|  | 5682 | // Add support for IEC61937 if any raw surround supported. | 
|  | 5683 | // The HAL could do this but add it here, just in case. | 
|  | 5684 | if ((supportsAC3 || supportsOtherSurround) && !supportsIEC61937) { | 
|  | 5685 | formats.add(AUDIO_FORMAT_IEC61937); | 
|  | 5686 | supportsIEC61937 = true; | 
|  | 5687 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5688 | } | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5689 | } | 
|  | 5690 |  | 
|  | 5691 | // Modify the list of channel masks supported. | 
|  | 5692 | void AudioPolicyManager::filterSurroundChannelMasks(ChannelsVector *channelMasksPtr) { | 
|  | 5693 | ChannelsVector &channelMasks = *channelMasksPtr; | 
|  | 5694 | audio_policy_forced_cfg_t forceUse = mEngine->getForceUse( | 
|  | 5695 | AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND); | 
|  | 5696 |  | 
|  | 5697 | // If NEVER, then remove support for channelMasks > stereo. | 
|  | 5698 | if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER) { | 
|  | 5699 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); ) { | 
|  | 5700 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5701 | if (channelMask & ~AUDIO_CHANNEL_OUT_STEREO) { | 
|  | 5702 | ALOGI("%s: force NEVER, so remove channelMask 0x%08x", __FUNCTION__, channelMask); | 
|  | 5703 | channelMasks.removeAt(maskIndex); | 
|  | 5704 | } else { | 
|  | 5705 | maskIndex++; | 
|  | 5706 | } | 
|  | 5707 | } | 
|  | 5708 | // If ALWAYS, then make sure we at least support 5.1 | 
|  | 5709 | } else if (forceUse == AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) { | 
|  | 5710 | bool supports5dot1 = false; | 
|  | 5711 | // Are there any channel masks that can be considered "surround"? | 
|  | 5712 | for (size_t maskIndex = 0; maskIndex < channelMasks.size(); maskIndex++) { | 
|  | 5713 | audio_channel_mask_t channelMask = channelMasks[maskIndex]; | 
|  | 5714 | if ((channelMask & AUDIO_CHANNEL_OUT_5POINT1) == AUDIO_CHANNEL_OUT_5POINT1) { | 
|  | 5715 | supports5dot1 = true; | 
|  | 5716 | break; | 
|  | 5717 | } | 
|  | 5718 | } | 
|  | 5719 | // If not then add 5.1 support. | 
|  | 5720 | if (!supports5dot1) { | 
|  | 5721 | channelMasks.add(AUDIO_CHANNEL_OUT_5POINT1); | 
|  | 5722 | ALOGI("%s: force ALWAYS, so adding channelMask for 5.1 surround", __FUNCTION__); | 
|  | 5723 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5724 | } | 
|  | 5725 | } | 
|  | 5726 |  | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5727 | void AudioPolicyManager::updateAudioProfiles(audio_devices_t device, | 
|  | 5728 | audio_io_handle_t ioHandle, | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5729 | AudioProfileVector &profiles) | 
|  | 5730 | { | 
|  | 5731 | String8 reply; | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5732 |  | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5733 | // Format MUST be checked first to update the list of AudioProfile | 
|  | 5734 | if (profiles.hasDynamicFormat()) { | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5735 | reply = mpClientInterface->getParameters( | 
|  | 5736 | ioHandle, String8(AudioParameter::keyStreamSupportedFormats)); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5737 | ALOGV("%s: supported formats %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5738 | AudioParameter repliedParameters(reply); | 
|  | 5739 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5740 | String8(AudioParameter::keyStreamSupportedFormats), reply) != NO_ERROR) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5741 | ALOGE("%s: failed to retrieve format, bailing out", __FUNCTION__); | 
|  | 5742 | return; | 
|  | 5743 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5744 | FormatVector formats = formatsFromString(reply.string()); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5745 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5746 | filterSurroundFormats(&formats); | 
| Phil Burk | 00eeb32 | 2016-03-31 12:41:00 -0700 | [diff] [blame] | 5747 | } | 
| Phil Burk | 09bc461 | 2016-02-24 15:58:15 -0800 | [diff] [blame] | 5748 | profiles.setFormats(formats); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5749 | } | 
|  | 5750 | const FormatVector &supportedFormats = profiles.getSupportedFormats(); | 
|  | 5751 |  | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5752 | for (size_t formatIndex = 0; formatIndex < supportedFormats.size(); formatIndex++) { | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5753 | audio_format_t format = supportedFormats[formatIndex]; | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5754 | ChannelsVector channelMasks; | 
|  | 5755 | SampleRateVector samplingRates; | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5756 | AudioParameter requestedParameters; | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5757 | requestedParameters.addInt(String8(AudioParameter::keyFormat), format); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5758 |  | 
|  | 5759 | if (profiles.hasDynamicRateFor(format)) { | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5760 | reply = mpClientInterface->getParameters( | 
|  | 5761 | ioHandle, | 
|  | 5762 | requestedParameters.toString() + ";" + | 
|  | 5763 | AudioParameter::keyStreamSupportedSamplingRates); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5764 | ALOGV("%s: supported sampling rates %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5765 | AudioParameter repliedParameters(reply); | 
|  | 5766 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5767 | String8(AudioParameter::keyStreamSupportedSamplingRates), reply) == NO_ERROR) { | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5768 | samplingRates = samplingRatesFromString(reply.string()); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5769 | } | 
|  | 5770 | } | 
|  | 5771 | if (profiles.hasDynamicChannelsFor(format)) { | 
|  | 5772 | reply = mpClientInterface->getParameters(ioHandle, | 
|  | 5773 | requestedParameters.toString() + ";" + | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5774 | AudioParameter::keyStreamSupportedChannels); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5775 | ALOGV("%s: supported channel masks %s", __FUNCTION__, reply.string()); | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5776 | AudioParameter repliedParameters(reply); | 
|  | 5777 | if (repliedParameters.get( | 
| Mikhail Naganov | 388360c | 2016-10-17 17:09:41 -0700 | [diff] [blame] | 5778 | String8(AudioParameter::keyStreamSupportedChannels), reply) == NO_ERROR) { | 
| Eric Laurent | 62e4bc5 | 2016-02-02 18:37:28 -0800 | [diff] [blame] | 5779 | channelMasks = channelMasksFromString(reply.string()); | 
| Phil Burk | 0709b0a | 2016-03-31 12:54:57 -0700 | [diff] [blame] | 5780 | if (device == AUDIO_DEVICE_OUT_HDMI) { | 
|  | 5781 | filterSurroundChannelMasks(&channelMasks); | 
|  | 5782 | } | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5783 | } | 
|  | 5784 | } | 
| Eric Laurent | 20eb3a4 | 2016-01-26 18:39:17 -0800 | [diff] [blame] | 5785 | profiles.addProfileFromHal(new AudioProfile(format, channelMasks, samplingRates)); | 
| François Gaffie | 112b0af | 2015-11-19 16:13:25 +0100 | [diff] [blame] | 5786 | } | 
|  | 5787 | } | 
| Eric Laurent | d60560a | 2015-04-10 11:31:20 -0700 | [diff] [blame] | 5788 |  | 
| Eric Laurent | e552edb | 2014-03-10 17:42:56 -0700 | [diff] [blame] | 5789 | }; // namespace android |